我正在尝试在其中一个标签中设置ListView,但在setAdapter中收到错误。我无法弄清问题是什么,请指导我。 这是代码
public class TabView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ListView listview1 = (ListView) findViewById(R.id.list1);
setContentView(R.layout.tview);
TabHost th = (TabHost) findViewById(android.R.id.tabhost);
th.setup();
List<String> list1strings = new ArrayList<String>();
list1strings.add("Programming Language");
list1strings.add("Database");
list1strings.add("Java technologies");
list1strings.add("Web Teechnologies");
list1strings.add("Application/Web Server");
list1strings.add("operating Systems");
list1strings.add("PHP framework");
list1strings.add("PHP CMS");
listview1.setAdapter(new ArrayAdapter<String>(TabView.this, android.R.layout.simple_list_item_1, list1strings));
th.addTab(th.newTabSpec("Tab1").setIndicator("Training").setContent(R.id.list1));
TabSpec specs = th.newTabSpec("Tab2");
specs.setContent(R.id.tab1);
specs.setIndicator("Workshops");
th.addTab(specs);
specs = th.newTabSpec("Tab 3");
specs.setContent(R.id.tab1);
specs.setIndicator("Reach Us");
th.addTab(specs);
}
}
这是Logcat,它在SetAdapter方法中显示错误,在我的代码中是第32行。
05-13 04:11:54.426: E/AndroidRuntime(1137): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.brainwave.industrialtraining/in.brainwave.industrialtraining.TabView}: java.lang.NullPointerException
05-13 04:11:54.426: E/AndroidRuntime(1137): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-13 04:11:54.426: E/AndroidRuntime(1137): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-13 04:11:54.426: E/AndroidRuntime(1137): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-13 04:11:54.426: E/AndroidRuntime(1137): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-13 04:11:54.426: E/AndroidRuntime(1137): at android.os.Handler.dispatchMessage(Handler.java:102)
05-13 04:11:54.426: E/AndroidRuntime(1137): at android.os.Looper.loop(Looper.java:136)
05-13 04:11:54.426: E/AndroidRuntime(1137): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-13 04:11:54.426: E/AndroidRuntime(1137): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 04:11:54.426: E/AndroidRuntime(1137): at java.lang.reflect.Method.invoke(Method.java:515)
05-13 04:11:54.426: E/AndroidRuntime(1137): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-13 04:11:54.426: E/AndroidRuntime(1137): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-13 04:11:54.426: E/AndroidRuntime(1137): at dalvik.system.NativeStart.main(Native Method)
05-13 04:11:54.426: E/AndroidRuntime(1137): Caused by: java.lang.NullPointerException
05-13 04:11:54.426: E/AndroidRuntime(1137): at in.brainwave.industrialtraining.TabView.onCreate(TabView.java:32)
05-13 04:11:54.426: E/AndroidRuntime(1137): at android.app.Activity.performCreate(Activity.java:5231)
05-13 04:11:54.426: E/AndroidRuntime(1137): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-13 04:11:54.426: E/AndroidRuntime(1137): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-13 04:11:54.426: E/AndroidRuntime(1137): ... 11 more
请指导我。
答案 0 :(得分:3)
您需要更改
之类的顺序setContentView(R.layout.tview);
ListView listview1 = (ListView) findViewById(R.id.list1);
在ListView
setContentView(R.layout.tview);
答案 1 :(得分:1)
如果要从xml访问任何布局,则首先必须执行
setContentView(R.layout.your_view);
之后,您可以访问
等视图ListView listview1 = (ListView) findViewById(R.id.list1);
在setContentView之前访问视图会给你nullPointer异常。