1

我从 HelloTabwidget 示例开始,它运行良好。现在我要做的是在每个选项卡内都有一个布局。这是代码的样子

myApp extends TabActivity {
  TabHost tabHost;
  onCreate() {
      setContentView (R.layout.main);
      tabHost = getTabHost();
      TabHost.tabspec spec = tabHost.newTabSpec(..).setIndicator(...).
               setContent(new Intent().setClass(...));
       tabHost.addTab(spec);
       :
  }
}

myTab1Actvity extends Activity {
  ListView myLV;
  EditText myET;

  onCreate() {
    setcontentView (TAB1_LAYOUT);

        myLV = findViewById(..);
  :
     myLV.setAdapter(aa);
     myET.setOnKeyListener(....)
    registerforContextMenu(myLV)
  }
}

当我在这里运行时是堆栈跟踪

 
ERROR/AndroidRuntime(691): java.lang.NullPointerException
ERROR/AndroidRuntime(691):     at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
ERROR/AndroidRuntime(691):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
ERROR/AndroidRuntime(691):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
ERROR/AndroidRuntime(691):     at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
ERROR/AndroidRuntime(691):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
ERROR/AndroidRuntime(691):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
ERROR/AndroidRuntime(691):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
ERROR/AndroidRuntime(691):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
ERROR/AndroidRuntime(691):     at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(691):     at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(691):     at android.app.ActivityThread.main(ActivityThread.java:4363)
ERROR/AndroidRuntime(691):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(691):     at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(691):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
ERROR/AndroidRuntime(691):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

代码的流程是否正常?基本上每个选项卡都有自己的活动,每个选项卡都有自己的布局。

TabHost 中似乎有问题 - 但我无法查明它。您的任何建议/问题都会非常有帮助。

4

1 回答 1

1

让我想起了“无限猴子定理”——无论如何,问题在于每个选项卡的布局文件中也有一个 TabHost。我使用的第一级(根)标签是 LinearLayout(而不是 TabHost)

于 2010-06-17T06:51:38.237 回答