android 开发Activity转换问题
错误:
05-19 04:08:45.885: E/AndroidRuntime(743): FATAL EXCEPTION: main
05-19 04:08:45.885: E/AndroidRuntime(743): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zhenglingkun.myfilemanage/com.zhenglingkun.myfilemanage.FileActivity}: java.lang.NullPointerException
05-19 04:08:45.885: E/AndroidRuntime(743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-19 04:08:45.885: E/AndroidRuntime(743): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-19 04:08:45.885: E/AndroidRuntime(743): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-19 04:08:45.885: E/AndroidRuntime(743): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-19 04:08:45.885: E/AndroidRuntime(743): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 04:08:45.885: E/AndroidRuntime(743): at android.os.Looper.loop(Looper.java:137)
05-19 04:08:45.885: E/AndroidRuntime(743): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-19 04:08:45.885: E/AndroidRuntime(743): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 04:08:45.885: E/AndroidRuntime(743): at java.lang.reflect.Method.invoke(Method.java:511)
05-19 04:08:45.885: E/AndroidRuntime(743): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-19 04:08:45.885: E/AndroidRuntime(743): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-19 04:08:45.885: E/AndroidRuntime(743): at dalvik.system.NativeStart.main(Native Method)
05-19 04:08:45.885: E/AndroidRuntime(743): Caused by: java.lang.NullPointerException
05-19 04:08:45.885: E/AndroidRuntime(743): at com.zhenglingkun.myfilemanage.FileActivity.onCreate(FileActivity.java:26)
05-19 04:08:45.885: E/AndroidRuntime(743): at android.app.Activity.performCreate(Activity.java:4465)
05-19 04:08:45.885: E/AndroidRuntime(743): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-19 04:08:45.885: E/AndroidRuntime(743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-19 04:08:45.885: E/AndroidRuntime(743): ... 11 more
描述:要实现从一个Activity跳转到另一个Activity,要跳转到的Activity代码如下:
public class FileActivity extends Activity {
//声明TabHost对象
TabHost mTabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得TabHost对象
//mTabHost = getTabHost();
mTabHost = (TabHost)this.findViewById(android.R.id.tabhost);
mTabHost.setup();
/* 为TabHost添加标签 */
//新建一个newTabSpec(newTabSpec)
//设置其标签和图标(setIndicator)
//设置内容(setContent)
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
.setIndicator("TAB 1"));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
.setIndicator("TAB 2"));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3")
.setIndicator("TAB 3"));
//设置TabHost的背景颜色
mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
//设置TabHost的背景图片资源
//mTabHost.setBackgroundResource(R.drawable.bg0);
//设置当前显示哪一个标签
mTabHost.setCurrentTab(0);
//标签切换事件处理,setOnTabChangedListener
mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
// TODO Auto-generated method stub
@Override
public void onTabChanged(String tabId)
{
Dialog dialog = new AlertDialog.Builder(FileActivity.this)
.setTitle("提示")
.setMessage("当前选中:"+tabId+"标签")
.setPositiveButton("确定",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.cancel();
}
}).create();//创建按钮
dialog.show();
}
});
}
}
[解决办法]
没前言后语看不出啊。。
貌似是fileTabHost对象没有初始化,把
//取得TabHost对象
//mTabHost = getTabHost();
下面这行的注释去掉看看。