我有一个在我的应用程序启动时调用的类。
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/