Android - java.lang.RuntimeException:无法启动接收器

时间:2015-11-08 17:11:43

标签: android nullpointerexception broadcastreceiver runtimeexception getresource

我用自定义应用程序类(MyApplication.java)和BroadcastReceiver类(ConnectivityReceiver.java)编写了一个应用程序。

但是,我有时会收到用户提出的错误报告

java.lang.RuntimeException: Unable to start receiver my.package.background.ConnectivityReceiver: 
java.lang.RuntimeException: Unable to create application my.package.MyApplication: 

我不知道为什么会出现这种异常。

这是详细的堆栈跟踪和相关文件。

栈跟踪

java.lang.RuntimeException: Unable to start receiver my.package.background.ConnectivityReceiver: 
java.lang.RuntimeException: Unable to create application my.package.MyApplication: 
java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2287)
at android.app.ActivityThread.access$1600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to create application my.package.MyApplication: java.lang.NullPointerException
at android.app.LoadedApk.makeApplication(LoadedApk.java:514)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2267)
... 10 more
Caused by: java.lang.NullPointerException
at my.package.MyApplication.setLanguage(MyApplication.java:137)
at my.package.MyApplication.onCreate(MyApplication.java:83)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014)
at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
... 11 more
java.lang.RuntimeException: Unable to create application my.package.MyApplication: java.lang.NullPointerException
at android.app.LoadedApk.makeApplication(LoadedApk.java:514)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2267)
at android.app.ActivityThread.access$1600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at my.package.MyApplication.setLanguage(MyApplication.java:137)
at my.package.MyApplication.onCreate(MyApplication.java:83)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014)
at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
... 11 more
java.lang.NullPointerException
at my.package.MyApplication.setLanguage(MyApplication.java:137)
at my.package.MyApplication.onCreate(MyApplication.java:83)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014)
at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2267)
at android.app.ActivityThread.access$1600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)

MyApplication.java

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate(); 
        //onCreate(MyApplication.java:83) refer to next line  
        setLanguage();
    }

    private void setLanguage() {       
        //setLanguage(MyApplication.java:137) refer to nextline
        Configuration config = getResources().getConfiguration();
        Log.i("MyApplication", "current language setting is: " + config.locale.getLanguage());    
    }
}

ConnectivityReceiver.java

public class ConnectivityReceiver extends BroadcastReceiver {
    public ConnectivityReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(this.getClass().getSimpleName(), "onReceive");
    }
}

的AndroidManifest.xml

    ...
    <receiver android:name=".package.background.ConnectivityReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>
    ...

0 个答案:

没有答案