我跟着這個例子:擴展應用實例導致程序崩潰
http://www.intridea.com/blog/2011/5/24/how-to-use-application-object-of-android
好像它的過程中MyApplication
當 singleton = this
onCreate()
崩潰。從MainActivity
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;
}
}
代碼:
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
任何想法?
線74,75,MainInterface 76
74:////////////////訪問全局函數
75:所有MyApplication mApplication =( MyApplication)getApplicationContext();
76://
你能告訴'MainInterface.java' 75行,和周圍的方法? – Eric
它是一個活動的MainInterface變量。 – jimmyC