首次安装后,Android应用程序类实例为空

时间:2015-09-07 10:25:14

标签: java android

这似乎是一个特殊情况,现在似乎只出现在运行API 23的Nexus 6上,而不是仿真器或(一对)4.4设备上。我使用我的应用程序类作为Singleton,通常它不会导致任何问题。但不知何故,在Nexus上,它会在应用程序类的任何引用上崩溃,因为它总是返回null。之后,应用程序运行正常,因此这仅在初始安装后(当应用程序被删除或之前未安装时)发生。

public class MyApp extends Application {
   private static MyApp APP_INSTANCE;
   public static MyApp getInstance() { return APP_INSTANCE; }

   @Override
   public void onCreate() {
      super.onCreate();
      APP_INSTANCE = this;
      // rest of code
   }
}

因此,无论是MyApp.getInstance()中的MainActivity,还是MainFragmentonCreateonStart中的onResume,都会崩溃甚至在应用程序运行之后。

调试时不会发生问题。似乎活动正在减慢,因此有时间创建应用程序类的实例,但是当我跳过所有初始调用以获取MainActivity中的实例然后尝试稍后获取它(之后)登录我的MainFragment仍然是null

此外,我尝试记录整个过程,我看到应用程序类的初始化已完成,因为我的HttpClients已设置并正常工作,但onCreate未被调用。

所以......任何人都知道造成这种情况的原因是什么?

添加了初始安装和第二次启动的StackTrace

初始安装

    09-07 12:46:43.240    2741-2741/com.myapp.d I/Process﹕ Sending signal. PID: 2741 SIG: 9
09-07 12:50:55.150    7068-7068/com.myapp.d W/System﹕ ClassLoader referenced unknown path: /data/app/com.myapp.d-1/lib/arm
09-07 12:50:56.346    7068-7068/com.myapp.d W/ClientFactory﹕ Returning UNSAFE client
09-07 12:50:56.360    7068-7068/com.myapp.d I/ClientFactory﹕ Supported CipherSuites: [...ciphersuits here...]
09-07 12:50:56.362    7068-7068/com.myapp.d I/MyApp﹕ Returning App Instance
09-07 12:50:56.362    7068-7068/com.myapp.d I/MainActivity﹕ Init UI
09-07 12:50:56.389    7068-7068/com.myapp.d I/AppCompatViewInflater﹕ app:theme is now deprecated. Please move to using android:theme instead.
09-07 12:50:56.446    7068-7068/com.myapp.d I/MyApp﹕ Returning App Instance
09-07 12:50:56.512    7068-7068/com.myapp.d I/MainActivity﹕ Check Startup
09-07 12:50:56.512    7068-7068/com.myapp.d E/MainActivity﹕ initApp(): start
09-07 12:50:56.512    7068-7068/com.myapp.d D/AndroidRuntime﹕ Shutting down VM
09-07 12:50:56.513    7068-7068/com.myapp.d E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.myapp.d, PID: 7068
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.d/com.myapp.ui.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.myapp.MyApp.isFirstBoot()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.myapp.MyApp.isFirstBoot()' on a null object reference
            at com.myapp.ui.MainActivity.initApp(MainActivity.java:173)
            at com.myapp.ui.MainActivity.onCreate(MainActivity.java:116)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

第二次启动

09-07 12:54:07.970    7068-7068/com.myapp.d I/Process﹕ Sending signal. PID: 7068 SIG: 9
09-07 12:54:10.386    8420-8420/com.myapp.d W/System﹕ ClassLoader referenced unknown path: /data/app/com.myapp.d-1/lib/arm
09-07 12:54:10.397    8420-8420/com.myapp.d W/ClientFactory﹕ Returning UNSAFE client
09-07 12:54:10.409    8420-8420/com.myapp.d I/ClientFactory﹕ Supported CipherSuites: [...ciphersuites here...]
09-07 12:54:10.410    8420-8420/com.myapp.d I/MyApp﹕ onCreate()
09-07 12:54:10.766    8420-8420/com.myapp.d I/CrashlyticsCore﹕ Initializing Crashlytics 2.3.4.74
09-07 12:54:10.811    8420-8420/com.myapp.d I/MyApp﹕ Returning App Instance
09-07 12:54:10.811    8420-8420/com.myapp.d I/MainActivity﹕ Init UI
09-07 12:54:10.833    8420-8420/com.myapp.d I/AppCompatViewInflater﹕ app:theme is now deprecated. Please move to using android:theme instead.
09-07 12:54:10.883    8420-8426/com.myapp.d W/art﹕ Suspending all threads took: 5.763ms
09-07 12:54:10.887    8420-8420/com.myapp.d I/MyApp﹕ Returning App Instance
09-07 12:54:10.953    8420-8420/com.myapp.d I/MainActivity﹕ Check Startup
09-07 12:54:10.953    8420-8420/com.myapp.d E/MainActivity﹕ initApp(): start
09-07 12:54:10.954    8420-8420/com.myapp.d I/MyApp﹕ Check for first boot: false
09-07 12:54:10.954    8420-8420/com.myapp.d I/MyApp﹕ Check for updated app: false
09-07 12:54:10.959    8420-8420/com.myapp.d D//SpiceManager.java:489﹕ 12:54:10.959 main adding request to request queue
09-07 12:54:10.959    8420-8420/com.myapp.d E/MainActivity﹕ initApp(): autoLogin
09-07 12:54:10.960    8420-8420/com.myapp.d E/MainActivity﹕ initApp(): end

和Manifest文件一样,没什么特别的,但以防万一

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp">

    <!-- all my permissions: nothing special -->

    <application
        android:name="com.myapp.MyApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app.label"
        android:theme="@style/AppTheme">

        <activity
            android:name=".ui.MainActivity"
            android:configChanges="orientation"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Other activities and services below -->

        <!-- And API KEYS go here -->

    </application>

</manifest>

0 个答案:

没有答案