0

我在我的 Android 应用程序中使用了 TabActivity。一切正常,除了以下情况:

  1. 我使用主页按钮关闭应用程序
  2. 我关闭手机(待机模式)
  3. 过了一会儿我打开手机,我想回到我的应用程序
  4. 应用程序因 NullPointerException 而崩溃

更多信息:我认为这是因为我使用 AsyncTask 以及 Webviews 和应用程序无法恢复上一个状态,但我该如何解决呢?AsynTask 在 onCreate() 中触发。我也使用的 Webviews。

一种解决方案可能是在用户每次点击返回或主页按钮时终止整个应用程序,但这并不是真正的用户友好。有人知道替代方案吗?

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;



public class BBGMainActivity extends TabActivity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bbg_main);
        setTabs() ;
    }

    private void setTabs()
    {
        addTab("News", R.drawable.tab_news, BBGWebViewActivity.class, "http://www.google.com");     
        addTab("Intern", R.drawable.tab_home, BBGPDFViewerActivity.class, "");      
        addTab("Offers", R.drawable.tab_offer, BBGWebViewWithHeaderActivity.class, "http://www.google.com");
        addTab("Service", R.drawable.tab_service, BBGJSONParsingActivity.class, "");
        addTab("Office", R.drawable.tab_offices, BBGListViewActivity.class, "");
    }


    private void addTab(String labelId, int drawableId, Class<?> c, String url)
    {
        TabHost tabHost = getTabHost();
        //tabHost.setup();
        Intent intent = new Intent(this, c);
        intent.putExtra("URL",url);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}

错误日志:

06-28 09:22:02.445: E/AndroidRuntime(32549): FATAL EXCEPTION: main
06-28 09:22:02.445: E/AndroidRuntime(32549): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.bbg/org.bbg.BBGMainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.bbg/org.bbg.BBGWebViewActivity}: java.lang.NullPointerException
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.os.Looper.loop(Looper.java:150)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.ActivityThread.main(ActivityThread.java:4385)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at java.lang.reflect.Method.invokeNative(Native Method)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at java.lang.reflect.Method.invoke(Method.java:507)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at dalvik.system.NativeStart.main(Native Method)
06-28 09:22:02.445: E/AndroidRuntime(32549): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.bbg/org.bbg.BBGWebViewActivity}: java.lang.NullPointerException
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1692)
06-28 09:21:44.557: D/dalvikvm(32384): GC_EXTERNAL_ALLOC freed 12K, 46% free 3148K/5767K, external 569K/583K, paused 228ms
06-28 09:22:02.445: D/AndroidRuntime(32549): Shutting down VM
06-28 09:22:02.445: W/dalvikvm(32549): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:656)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.widget.TabHost.setCurrentTab(TabHost.java:326)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.widget.TabHost.addTab(TabHost.java:216)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at org.bbg.BBGMainActivity.addTab(BBGMainActivity.java:53)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at org.bbg.BBGMainActivity.setTabs(BBGMainActivity.java:28)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at org.bbg.BBGMainActivity.onCreate(BBGMainActivity.java:23)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
06-28 09:22:02.445: E/AndroidRuntime(32549):    ... 11 more
06-28 09:22:02.445: E/AndroidRuntime(32549): Caused by: java.lang.NullPointerException
06-28 09:22:02.445: E/AndroidRuntime(32549):    at org.bbg.BBGWebViewActivity.onCreate(BBGWebViewActivity.java:61)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
06-28 09:22:02.445: E/AndroidRuntime(32549):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
06-28 09:22:02.445: E/AndroidRuntime(32549):    ... 22 more
4

0 回答 0