我Activity
获得了TabHost
。 XML文件是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="70sp"
android:layout_alignParentBottom="true" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@android:id/tabs" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listDictionary"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listFavourites"
android:layout_width="match_parent"
android:layout_height="356dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/wikibilim" />
<TextView
android:id="@+id/textFavourite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wiki" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
在开始MainActivity
方法的onCreate
中,我写道:
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
// TabHost.TabSpec tabSpec;
View indicator1 = getLayoutInflater().inflate(R.layout.tab1, null);
im1 = (ImageView) indicator1.findViewById(R.id.imageView1);
View indicator2 = getLayoutInflater().inflate(R.layout.tab2, null);
im2 = (ImageView) indicator2.findViewById(R.id.imageView1);
View indicator3 = getLayoutInflater().inflate(R.layout.tab3, null);
im3 = (ImageView) indicator3.findViewById(R.id.imageView1);
TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec("tag1");
tabSpec.setIndicator(indicator1);
tabSpec.setContent(R.id.tab1);
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("tag2");
tabSpec.setIndicator(indicator2);
tabSpec.setContent(R.id.tab2);
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("tag3");
tabSpec.setIndicator(indicator3);
tabSpec.setContent(R.id.tab3);
tabHost.addTab(tabSpec);
dialog = new ProgressDialog(this);
dialog.setMessage(getString(R.string.loading));
dialog.setCanceledOnTouchOutside(false);
dictionaryList = (ListView) this.findViewById(R.id.listDictionary);
我很容易使用异步任务将数据上传到dictionaryList
。之后我将监听器添加到TabHost
:
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
if (tabId.equals("tag2")) {
View view = tabHost.getChildAt(tabHost.getCurrentTab());
favouritesList = (ListView) view.findViewById(R.id.listFavourites);
Toast.makeText(getApplicationContext(), favouritesList.toString(), Toast.LENGTH_LONG).show();
}
}
});
当它试图获得favouritesList
时,会出现NullPointerException
。我做错了什么?
logcat的:
02-19 21:40:52.360: E/AndroidRuntime(25828): FATAL EXCEPTION: main
02-19 21:40:52.360: E/AndroidRuntime(25828): java.lang.NullPointerException
02-19 21:40:52.360: E/AndroidRuntime(25828): at kz.wikibilim.sozdik.MainActivity$1.onTabChanged(MainActivity.java:117)
02-19 21:40:52.360: E/AndroidRuntime(25828): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:402)
02-19 21:40:52.360: E/AndroidRuntime(25828): at android.widget.TabHost.setCurrentTab(TabHost.java:387)
02-19 21:40:52.360: E/AndroidRuntime(25828): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
02-19 21:40:52.360: E/AndroidRuntime(25828): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:560)
02-19 21:40:52.360: E/AndroidRuntime(25828): at android.view.View.performClick(View.java:4223)
02-19 21:40:52.360: E/AndroidRuntime(25828): at android.view.View$PerformClick.run(View.java:17275)
02-19 21:40:52.360: E/AndroidRuntime(25828): at android.os.Handler.handleCallback(Handler.java:615)
02-19 21:40:52.360: E/AndroidRuntime(25828): at android.os.Handler.dispatchMessage(Handler.java:92)
02-19 21:40:52.360: E/AndroidRuntime(25828): at android.os.Looper.loop(Looper.java:137)
02-19 21:40:52.360: E/AndroidRuntime(25828): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-19 21:40:52.360: E/AndroidRuntime(25828): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 21:40:52.360: E/AndroidRuntime(25828): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 21:40:52.360: E/AndroidRuntime(25828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
02-19 21:40:52.360: E/AndroidRuntime(25828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
02-19 21:40:52.360: E/AndroidRuntime(25828): at dalvik.system.NativeStart.main(Native Method)
我已经找到了如何获取此listFavourites
和设置的适配器。当我开始申请时,它开始很好,没有错误。但是,当我点击第二个标签(ListView
所在的位置)时,它会再次给我NullPointerException
答案 0 :(得分:0)
使用indicator2
代替view
将favouritesList
ListView初始化为:
favouritesList = (ListView) indicator2.findViewById(R.id.listFavourites);
答案 1 :(得分:0)
我发现了自己的错误。它位于我的适配器中,将在我的ListView
中设置。在getView()
方法中,我忘了写return view
,但仍然有return null
。无论如何,谢谢你的关注。