1

私はこの例に従いました:

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://

4

1 に答える 1

2

mApplicationで初期化する必要がありますonCreate()

MyApplication mApplication;

public void onCreate(Bundle savedInstanceState) {
    ...
    mApplication = (MyApplication) getApplicationContext();
于 2012-11-24T01:06:54.373 に答える