当我想从Activity切换到MapActivity时,得到一个好奇的NullPointerException

时间:2012-03-28 09:23:24

标签: android mapactivity

当我尝试切换到MapActivity时,我获得了NullPointerException。

我在 super.oncreate(savedInstanceState); 调用中获取它,该调用必须是 public void onCreate(Bundle savedInstanceState)方法中的第一行。在我可以看到的调试器中,savedInstanceState确实为null。

这是我用来切换到该活动的代码,当点击Button时会调用它。使用另一个正常的Activity,它运行得很好。它来自启动Activity和Method,从 OnTouchListener 调用。

public void buttonLiveTouch(View v){
    Intent intent = new Intent(this, LiveActivity.class);
    startActivity(intent);
}

正常活动的文档说

  

如果在先前关闭之后重新初始化活动,则此Bundle包含最近在onSaveInstanceState(Bundle)中提供的数据。注意:否则为空。

所以我猜它的意图是null,因为MapActivity“只是”正常Activity的扩展。 所以,呃,我在监督什么,出了什么问题?

来自LogCat的ErrorLog:

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>

亲爱的未来读者: 我还没发现这个错误的原因是什么,但这就是我“解决”这个问题的方法: 1)我删除了avd 2)我重新安装了sdk版本 3)我创建了一个新的avd

1 个答案:

答案 0 :(得分:0)

取自MapActivity

的文档
  

这里我们初始化静态工厂,创建地图对象和   交通服务,但不要开始任何线程。子类应该   在调用之后创建(或膨胀)其MapView对象   super.onCreate()。

这意味着Map活动也会对bundle做一些逻辑。它在某种状态下显然会失败。我并不完全相信它与传入的包有关。我希望我能在堆栈跟踪中看到android_maps_conflict_avoidance.com.google.common.io.android.AndroidPersistentStore或其他内容的代码。