Android - findViewById(R.id.list) 返回 null

标签 android android-activity listactivity

我有一个在我的应用程序启动时调用的类。

public class MainActivity extends Activity implements NetworkEvent.

在这种情况下,

list = (ListView) findViewById(R.id.list);

完美运行。但是,如果我随后通过以下方式调用新 Intent :

   String[] names = object.names();

Intent myIntent = new Intent(MainActivity.this, SimpleList.class); myIntent.putExtra("姓名", 姓名); startActivityForResult(myIntent, 0);

其中 SimpleList 定义为:

public class SimpleList extends ListActivity implements NetworkEvent

然后当我打电话

   list=(ListView) findViewById(R.id.list);

Log.i("MyApp", "List:"+ list);

在 SimpleList 类中,列表为空 :(

怎么会?这两个类都在同一个包中。

谢谢。

最佳答案

我不确定为什么会发生在您的情况下,但是 ListView在您的布局 XML 中,必须定义为 <ListView android:id="@android:id/list" ,不是典型的 <ListView android:id="@+id/list" .如果你的类(class)扩展 ListActivity ,在这种情况下,您只需使用 getListView()访问列表。
看看这个:http://developer.android.com/reference/android/app/ListActivity.html

关于Android - findViewById(R.id.list) 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3782823/

相关文章:

android - FadingActionBar 与 ActionBarCompat

java - ExceptionInInitializerError - findLibrary 返回 null

android-intent - 无法从渲染器覆盖 OnActivityResult

android - 无法解析方法 getListView 和 setListAdapter (Android)

android - 在 ListView 中访问特定项目的布局

android - 搜索开始新 Activity 时如何结束旧 Activity

android - 在 Android 10 中从 Uri 获取绝对路径

android - 如何创建适合我的显示器尺寸的 AVD?

android - ListActivity 与 ListView

android - 如何为 Galaxy Note 8 创建特殊布局文件夹?