Android アプリケーションで TabActivity を使用しました。次のシナリオを除いて、すべて正常に動作します。
- ホームボタンでアプリを閉じる
- 電話を切ります(スタンバイモード)
- しばらくして電話の電源を入れ、アプリに戻りたい
- アプリが NullPointerException でクラッシュする
いくつかの詳細情報: AsyncTask を使用し、さらに Webviews を使用していて、アプリが最後の状態を回復できないためだと思いますが、どうすれば解決できますか? AsynTask は onCreate() でトリガーされます。私も使用するWebviews。
解決策の 1 つは、ユーザーが [戻る] ボタンまたは [ホーム] ボタンを押すたびにアプリ全体を強制終了することですが、これは実際にはユーザー フレンドリーではありません。誰かが代替案を知っていますか?
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