int*_*_32 5 layout android android-intent android-layout
我有2个活动.第二个.xml看起来像:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/TrainsListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
清单的一部分:
<activity android:name="TrainsActivity">
</activity>
Run Code Online (Sandbox Code Playgroud)
而我正在尝试获取TrainsListView:
mListView = (ListView) findViewById(R.id.TrainsListView);
Run Code Online (Sandbox Code Playgroud)
但在此mlistView为null之后.为什么?
Mar*_*k B 12
我猜这是在你的Activity的onCreate()方法中发生的,因为你在调用findViewById()之前调用了setContentView().如果情况并非如此,请显示更多代码.
如果findViewById()找不到您的目标ID,可能会出现以下情况:
[[在你的情况下,我怀疑#1或#2.]
SetContentView()inflate尚未调用(或)尚未将布局与搜索相关联.
SetContentView()(或inflate)使用不包含目标ID的布局调用,例如,您指定了错误的布局.
id在布局或代码中有一个拼写错误.
您导入了错误的R文件(如库组件的R文件),巧合的是,另一个R文件中也使用了相同的ID ).
您正在搜索错误的视图,例如您夸大了菜单布局并且没有将其附加到根布局,然后您尝试在根布局(findBiewById(..))中搜索id,以便搜索膨胀的菜单布局(menu.findBiewById(..)).