ForceClose error in android TabWidget application
Force close error in TAB widget application I am using the tutorial at http://developer.android.com/resources/tuto开发者_运维问答rials/views/hello-tabwidget.html to create TAB view. I want to display different list in each tab. I have created each list in separate activity and calling it by creating new instance of intent and then pass it to setcontent(); Following is the code for TabWidget.java
package com.TabWidget;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class TabWidget extends TabActivity {
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
Resources res = getResources();
Intent aIntent = new Intent(this, a.class);
Intent bIntent = new Intent(this, b.class);
Intent cIntent = new Intent(this, c.class);
mTabHost.addTab(mTabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.icon)).setContent(aIntent));
mTabHost.addTab(mTabHost.newTabSpec("B").setIndicator("B", res.getDrawable(R.drawable.icon)).setContent(bIntent));
mTabHost.addTab(mTabHost.newTabSpec("C").setIndicator("C", res.getDrawable(R.drawable.icon)).setContent(cIntent));
mTabHost.setCurrentTab(1);
}
}
A.java
package com.TabWidget;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class A extends Activity {
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list=(ListView)findViewById(R.id.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, COUNTRIES);
list.setAdapter(adapter);
setContentView(R.layout.tab1);
}
}
Main .xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="@layout/tab1"/>
<include layout="@layout/tab2"/>
<include layout="@layout/tab3"/>
</FrameLayout>
</LinearLayout>
</TabHost>
I am novice to android development and interested in UI development. Thanks in advance.
In A.onCreate()
you're trying to find a view before setting any content with setContentView()
. So, list
is null and you're getting a NullPointerException
(NPE) at line
list.setAdapter(adapter);
Check this out in the LogCat, to learn how to find problems. LogCat is a priceless tool.
精彩评论