扩展应用程序示例导致程序崩溃

时间:2012-11-24 00:49:35

标签: java api android

我按照这个例子:

http://www.intridea.com/blog/2011/5/24/how-to-use-application-object-of-android

onCreate() MyApplication期间似乎崩溃了 singleton = this

public class MyApplication extends Application {

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}
private static MyApplication singleton;

@Override
public void onCreate() {
    super.onCreate();
    Log.v("MyApplication", "Hi from my application");
    singleton = this;
    Log.v("MyApplication", "End onCreat of myApplicatoin");

}
@Override
public void onLowMemory() {
    super.onLowMemory();
}

@Override
public void onTerminate() {
    super.onTerminate();
}

public MyApplication getInstance(){
    return singleton;
}
public boolean isNetworkAvailable() {
    return true;
}


}

MainActivity的代码:

MyApplication mApplication = (MyApplication) getApplicationContext();

以下是logcat消息:

11-23 16:43:01.398: V/MyApplication(30957): Hi from my application
11-23 16:43:01.398: E/AndroidRuntime(30957): FATAL EXCEPTION: main
11-23 16:43:01.398: E/AndroidRuntime(30957): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.jimmyc.fall12.OpenClass/com.jimmyc.fall12.OpenClass.MainInterface}: java.lang.NullPointerException
11-23 16:43:01.398: E/AndroidRuntime(30957):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at android.os.Looper.loop(Looper.java:137)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at android.app.ActivityThread.main(ActivityThread.java:5039)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at java.lang.reflect.Method.invokeNative(Native Method)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at java.lang.reflect.Method.invoke(Method.java:511)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at dalvik.system.NativeStart.main(Native Method)
11-23 16:43:01.398: E/AndroidRuntime(30957): Caused by: java.lang.NullPointerException
11-23 16:43:01.398: E/AndroidRuntime(30957):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at com.jimmyc.fall12.OpenClass.MainInterface.<init>(MainInterface.java:75)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at java.lang.Class.newInstanceImpl(Native Method)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at java.lang.Class.newInstance(Class.java:1319)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
11-23 16:43:01.398: E/AndroidRuntime(30957):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
> 11-23 16:43:01.398: E/AndroidRuntime(30957):  ... 11 more

有什么想法吗?

MainInterface的第74,75,76行

  

74:////////////////访问全局功能

     

75:MyApplication mApplication =(MyApplication)getApplicationContext();

     

76://

1 个答案:

答案 0 :(得分:2)

您需要在mApplication中初始化onCreate()

MyApplication mApplication;

public void onCreate(Bundle savedInstanceState) {
    ...
    mApplication = (MyApplication) getApplicationContext();