0

I have used TabActivity in my Android Application. Everything works fine, except the following scenario:

  1. I close the app by using the home button
  2. I turn off the phone (standby mode)
  3. After a while I turn on the phone and I want to return to my app
  4. The app crashes with NullPointerException

Some more Infos: I think it is because I use AsyncTask and in addition Webviews and the app can't recover the last state, but how can I solve that? The AsynTask is triggered in the onCreate(). The Webviews I use, too.

One solution might be to to kill the whole app everytime a user hits the back or home button, but this is not really user friendly. Does anybody know an alternative?

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();
    }
}

Error Log:

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
  • Seems like your problem is actually in the `BBGWebViewActivity`: `at org.bbg.BBGWebViewActivity.onCreate(BBGWebViewActivity.java:61)` . – dmon Jun 28 '13 at 12:27
  • Possible. Do you have an idea how to solve that? It seems when I return from sleep/standby the webview can't be recovered. – John Hammer Jun 28 '13 at 12:48

0 Answers0