當我嘗試切換到MapActivity時,我獲得NullPointerException。當我想從活動切換到MapActivity時得到一個好奇的NullPointerException
我正在接收super.oncreate(savedInstanceState);調用,其中必須是中的第一行public void onCreate(Bundle savedInstanceState)方法。在調試器中,我可以看到,savedInstanceState確實爲null。
這是我用來切換到該活動的代碼,它在點擊按鈕時被調用。與另一個正常的活動,它工作得很好。它從開始內部活動和方法從OnTouchListener被調用。
public void buttonLiveTouch(View v){
Intent intent = new Intent(this, LiveActivity.class);
startActivity(intent);
}
文檔正常活動說
如果活動被以前被關閉後重新初始化,則該包包括它最近的onSaveInstanceState中(束)提供的數據。注意:否則它是空的。
所以我猜它的意圖是它的空,因爲MapActivity是「只是」普通活動的延伸。所以,我在監督什麼,有什麼不對?
錯誤日誌從logcat的:
03-28 09:09:54.744: E/AndroidRuntime(1172): FATAL EXCEPTION: main
03-28 09:09:54.744: E/AndroidRuntime(1172): java.lang.RuntimeException: Unable to start activity ComponentInfo{m.p/m.p.LiveActivity}: java.lang.NullPointerException
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.app.ActivityThread.access$500(ActivityThread.java:122)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.os.Looper.loop(Looper.java:132)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.app.ActivityThread.main(ActivityThread.java:4123)
03-28 09:09:54.744: E/AndroidRuntime(1172): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 09:09:54.744: E/AndroidRuntime(1172): at java.lang.reflect.Method.invoke(Method.java:491)
03-28 09:09:54.744: E/AndroidRuntime(1172): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-28 09:09:54.744: E/AndroidRuntime(1172): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-28 09:09:54.744: E/AndroidRuntime(1172): at dalvik.system.NativeStart.main(Native Method)
**03-28 09:09:54.744: E/AndroidRuntime(1172): Caused by: java.lang.NullPointerException**
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.app.ContextImpl.openFileOutput(ContextImpl.java:601)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:165)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android_maps_conflict_avoidance.com.google.common.io.android.AndroidPersistentStore.writeBlockX(AndroidPersistentStore.java:140)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android_maps_conflict_avoidance.com.google.common.io.android.AndroidPersistentStore.writeBlock(AndroidPersistentStore.java:169)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android_maps_conflict_avoidance.com.google.common.io.PreferenceStore.ensurePreferencesLoaded(PreferenceStore.java:178)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android_maps_conflict_avoidance.com.google.common.io.PreferenceStore.readPreference(PreferenceStore.java:86)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android_maps_conflict_avoidance.com.google.common.io.BasePersistentStore.readPreference(BasePersistentStore.java:41)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android_maps_conflict_avoidance.com.google.common.StaticUtil.readPreferenceAsDataInput(StaticUtil.java:271)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.loadOrRequestCookie(DataRequestDispatcher.java:493)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.<init>(DataRequestDispatcher.java:390)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.createInstance(DataRequestDispatcher.java:341)
03-28 09:09:54.744: E/AndroidRuntime(1172): at com.google.android.maps.MapActivity.createMap(MapActivity.java:548)
03-28 09:09:54.744: E/AndroidRuntime(1172): at com.google.android.maps.MapActivity.onCreate(MapActivity.java:422)
**03-28 09:09:54.744: E/AndroidRuntime(1172): at m.p.LiveActivity.onCreate(LiveActivity.java:20)**
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.app.Activity.performCreate(Activity.java:4397)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-28 09:09:54.744: E/AndroidRuntime(1172): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
03-28 09:09:54.744: E/AndroidRuntime(1172): ... 11 more
以防萬一,該類直到地步NullPointerException異常得到投擲
package m.p;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import m.p.R;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
public class LiveActivity extends MapActivity {
@Override
public void onCreate(Bundle savedInstanceState) { //line 19
super.onCreate(savedInstanceState); //line 20
setContentView(R.layout.live); //line 21
的代碼和我的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="m.p"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/logo_hg_03"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".SteuerungActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LiveActivity" />
<activity android:name=".DataActivity" />
</application>
de未來的讀者: 我沒有找到這個錯誤的原因是什麼,但這是我如何「解決」這個問題: 1)我刪除了avd 2)我重新安裝了sdk版本 3)我創建一個新的AVD
我從來沒有見過這個錯誤。你的代碼看起來很好。你在測試什麼? – CommonsWare 2012-03-28 11:33:22
即時通訊模擬器測試(3.2谷歌Api) – CAA 2012-03-28 11:44:58
它似乎不是第一個誰有這個錯誤...誰在 http:// www。androidside.com/bbs/board.php?bo_table=B49&wr_id=62163 別人似乎有這個問題...但據我所知,谷歌翻譯給我什麼,它是未解決的.... – CAA 2012-03-28 12:08:27