我的布局有两个 XML 文件 - main.xml 和 test.xml。下面是 test.xml 中的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_objoptions"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
</ListView>
</LinearLayout>
当我尝试获取 ListView 的实例时
findViewById(R.id.menu_objoptions)
它返回空值。这是为什么?
编辑:这是相关的java代码:
public void objClick(String objid, long X, long Y) {
final String objID = objid;
final long x = X;
final long y = Y;
try {
mHandle.runOnUiThread(new Runnable() {
@Override
public void run() {
PopupWindow popUp;
LinearLayout layout;
ListView mainListView;
ArrayAdapter<String> listAdapter;
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
listAdapter = new ArrayAdapter<String>(mHandle, R.layout.menu_objoptions_row, planetList);
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );
popUp = new PopupWindow(mHandle);
layout = new LinearLayout(mHandle);
layout.setOrientation(LinearLayout.VERTICAL);
mainListView = (ListView) mHandle.findViewById( R.id.menu_objoptions );
mainListView.setAdapter( listAdapter );
layout.addView(mainListView);
popUp.setContentView(layout);
popUp.setOutsideTouchable(true);
popUp.showAtLocation(rlmain, Gravity.NO_GRAVITY, 10, 10);
popUp.update((int)x,(int)y, 300, 80);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
NullPointerException 在这一行被抛出:
mainListView.setAdapter( listAdapter );