java.lang.NullPointerException at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:32
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): java.lang.NullPointerException
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:321)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:321)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.os.Looper.loop(Looper.java:123)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at java.lang.reflect.Method.invoke(Method.java:521)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-01 17:29:35.613: ERROR/AndroidRuntime(23590): at dalvik.system.NativeStart.main(Native Method)
この問題は私を何日も困らせた.
まずコードを貼り付けます
//
final TabHost tabHost = (TabHost) this.findViewById(android.R.id.tabhost);
tabHost.setup(); // setup
// info, tab,
//java.lang.NullPointerException at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:321)
if(saleDetail.getInfoList().size() == 0){
TextView view = new TextView(context);
tabHost.addTab(tabHost.newTabSpec("")
.setContent(new TabFacroty(""))// ID layout
.setIndicator(view));
tabHost.getTabWidget().setVisibility(View.GONE);
return;
}
for(GoodsInfoBean info : saleDetail.getInfoList()){
TextView view = new TextView(context);
view.setLayoutParams(new LinearLayout.LayoutParams(-2, -2));
view.setText(info.getTitle());
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setGravity(Gravity.CENTER);
view.setTextColor(context.getResources().getColor(R.color.sale_tab_default));
view.setBackgroundResource(R.drawable.sale_detail_spec_selector);
tabHost.addTab(tabHost.newTabSpec("")
.setContent(new TabFacroty(info.getAlt()))// ID layout
.setIndicator(view));
}
場合
saleDetail.getInfoList() == 0 tab
次にspinnerをクリックしたりalertをポップアップしたりすると、上記のようにエラーが表示されます...
テストによりtab個数が1より大きい場合はエラーが報告されないことが分かった
それで...tabと関係があると推測してこのセグメントコードを加える.存在しない場合はデフォルトで非表示のtabを追加します
if(saleDetail.getInfoList().size() == 0){
、、、、、 これで問題を解決する