我无法在模拟器中执行给定的代码,并且崩溃时会发出错误,如下面的日志所示。这是我的活动
import android.app.TabActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
@SuppressWarnings("deprecation")
public class Second extends TabActivity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
setupTab(new TextView(this), "SRK/T");
setupTab(new TextView(this), "SRK II");
setupTab(new TextView(this), "HOLLADAY");
setupTab(new TextView(this), "BINKHORST");
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return null;
}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
日志文件
01-30 16:53:07.266: E/AndroidRuntime(382): FATAL EXCEPTION: main
01-30 16:53:07.266: E/AndroidRuntime(382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iolcalci/com.example.iolcalci.Second}: java.lang.NullPointerException
01-30 16:53:07.266: E/AndroidRuntime(382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-30 16:53:07.266: E/AndroidRuntime(382): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-30 16:53:07.266: E/AndroidRuntime(382): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-30 16:53:07.266: E/AndroidRuntime(382): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-30 16:53:07.266: E/AndroidRuntime(382): at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 16:53:07.266: E/AndroidRuntime(382): at android.os.Looper.loop(Looper.java:123)
01-30 16:53:07.266: E/AndroidRuntime(382): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-30 16:53:07.266: E/AndroidRuntime(382): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 16:53:07.266: E/AndroidRuntime(382): at java.lang.reflect.Method.invoke(Method.java:507)
01-30 16:53:07.266: E/AndroidRuntime(382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-30 16:53:07.266: E/AndroidRuntime(382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-30 16:53:07.266: E/AndroidRuntime(382): at dalvik.system.NativeStart.main(Native Method)
01-30 16:53:07.266: E/AndroidRuntime(382): Caused by: java.lang.NullPointerException
01-30 16:53:07.266: E/AndroidRuntime(382): at android.widget.TabHost$FactoryContentStrategy.getContentView(TabHost.java:625)
01-30 16:53:07.266: E/AndroidRuntime(382): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
01-30 16:53:07.266: E/AndroidRuntime(382): at android.widget.TabHost.addTab(TabHost.java:216)
01-30 16:53:07.266: E/AndroidRuntime(382): at com.example.iolcalci.Second.setupTab(Second.java:35)
01-30 16:53:07.266: E/AndroidRuntime(382): at com.example.iolcalci.Second.onCreate(Second.java:21)
01-30 16:53:07.266: E/AndroidRuntime(382): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-30 16:53:07.266: E/AndroidRuntime(382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-30 16:53:07.266: E/AndroidRuntime(382): ... 11 more
01-30 16:53:09.026: E/InputDispatcher(60): channel '40730f58 com.example.iolcalci/com.example.iolcalci.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
01-30 16:53:09.026: E/InputDispatcher(60): channel '40730f58 com.example.iolcalci/com.example.iolcalci.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
我无法找到问题。初学者请帮帮我
答案 0 :(得分:1)
请注意错误:
01-30 16:53:07.266: E/AndroidRuntime(382): Caused by: java.lang.NullPointerException
01-30 16:53:07.266: E/AndroidRuntime(382): at android.widget.TabHost$FactoryContentStrategy.getContentView(TabHost.java:625)
然后注意你的代码:
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return null;
}
}
此return null
导致异常。