2017-07-31 66 views
0

根據Playstore ANR &崩潰應用程序在創建SplashScreen時拋出空指針。我檢查了功能,看起來不錯。不是所有的手機都崩潰了。應用程序在啓動畫面上崩潰投擲空指針

我SplashActivity.java

package com.example.app; 

import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Handler; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.Html; 
import android.util.Log; 

public class SplashActivity extends AppCompatActivity { 

    String path = ""; 
    String banner = ""; 
    String title = ""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_splash); 

     if(getIntent().getExtras() != null){ 
      Log.e("splash", "from push link"); 
      path = getIntent().getExtras().getString("link"); 
      banner = getIntent().getExtras().getString("bannerImage"); 
      title = getIntent().getExtras().getString("title"); 
      String link = String.valueOf(Html.fromHtml(Html.fromHtml(path).toString())); 
      String imgurl = String.valueOf(Html.fromHtml(Html.fromHtml(banner).toString())); 
      Log.e("link----", "link--" + link); 

      SharedPreferences pref = getApplicationContext().getSharedPreferences("Push", MODE_PRIVATE); 
      SharedPreferences.Editor editor = pref.edit(); 
      String pushtitle = pref.getString("pushtitle", ""); 

      StringBuilder sb = new StringBuilder(); 
      sb.append(pushtitle).append(title).append(","); 
      Log.e("pushtitle===========", sb.toString()); 
      editor.putString("pushtitle", sb.toString()); 

      String links = pref.getString("link", ""); 
      StringBuilder sb2 = new StringBuilder(); 
      sb2.append(links).append(link).append(","); 
      Log.e("link===========", sb2.toString()); 
      editor.putString("link", sb2.toString()); 

      String imgurlstr = pref.getString("imgurl", ""); 
      StringBuilder sb3 = new StringBuilder(); 
      sb3.append(imgurlstr).append(imgurl).append(","); 
      Log.e("imgurl===========", sb3.toString()); 
      editor.putString("imgurl", sb3.toString()); 

      editor.commit(); 
     } 
     int secondsDelayed = 1; 
     new Handler().postDelayed(new Runnable() { 
      public void run() { 
       Intent intent = new Intent(SplashActivity.this, MainActivity.class); 
       if (path != "") { 
        Log.e("splash", "path==" + path); 
        intent.putExtra("link", String.valueOf(Html.fromHtml(Html.fromHtml(path).toString()))); 
       } 
       startActivity(intent); 
       finish(); 
      } 
     }, secondsDelayed * 1000); 
    } 
} 

的Play商店中的崩潰報告如下。 顯示java.lang.NullPointerException com.example.app.SplashActivity.onCreate

java.lang.RuntimeException: at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2984) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3045) at android.app.ActivityThread.-wrap14 (ActivityThread.java) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1642) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6776) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1496) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1386) Caused by: java.lang.NullPointerException:
at java.io.StringReader. (StringReader.java:50) at android.text.HtmlToSpannedConverter.convert (Html.java:758) at android.text.Html.fromHtml (Html.java:245) at android.text.Html.fromHtml (Html.java:188) at com.example.app.SplashActivity.onCreate (SplashActivity.java:27)
at android.app.Activity.performCreate (Activity.java:6955) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2927

編輯:調試應用程序,我得到了以下錯誤:第一次會議已經開始

日誌消息將遵循...

I/art (3818): Not late-enabling -Xcheck:jni (already on) W/art
(3818): Unexpected CPU variant for X86 using defaults: x86 W/System (3818): ClassLoader referenced unknown path: /data/app/com.example.app-1/lib/x86 D/FirebaseApp(3818): com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization. W/InstanceID/Rpc(3818): Found 10012 D/FirebaseApp( 3818): com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization. I/FA (3818): App measurement is starting up, version: 10084 I/FA (3818): To enable debug logging run: adb shell setprop log.tag.FA VERBOSE I/FA (3818): To enable faster debug mode event logging run: I/FA (3818): adb shell setprop debug.firebase.analytics.app com.example.app I/FirebaseInitProvider( 3818): FirebaseApp initialization successful W/art (3818): Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable E/splash (3818): from push link D/AndroidRuntime(3818): Shutting down VM E/AndroidRuntime(3818): FATAL EXCEPTION: main E/AndroidRuntime(3818): Process: com.example.app, PID: 3818 E/AndroidRuntime(3818): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.SplashActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference E/AndroidRuntime(3818): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) E/AndroidRuntime(3818): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) E/AndroidRuntime(3818): at android.app.ActivityThread.-wrap12(ActivityThread.java) E/AndroidRuntime(3818): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) E/AndroidRuntime(3818): at android.os.Handler.dispatchMessage(Handler.java:102) E/AndroidRuntime( 3818): at android.os.Looper.loop(Looper.java:154) E/AndroidRuntime( 3818): at android.app.ActivityThread.main(ActivityThread.java:6077) E/AndroidRuntime(3818): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(3818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) E/AndroidRuntime(3818): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) E/AndroidRuntime(3818): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference E/AndroidRuntime(3818): at java.io.StringReader.(StringReader.java:50) E/AndroidRuntime( 3818): at android.text.HtmlToSpannedConverter.convert(Html.java:758) E/AndroidRuntime(3818): at android.text.Html.fromHtml(Html.java:245) E/AndroidRuntime(3818): at android.text.Html.fromHtml(Html.java:188) E/AndroidRuntime(3818): at com.example.app.SplashActivity.onCreate(SplashActivity.java:27) E/AndroidRuntime(3818): at android.app.Activity.performCreate(Activity.java:6662) E/AndroidRuntime(3818): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) E/AndroidRuntime(3818): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) E/AndroidRuntime(3818): ... 9 more

+0

我想如果你從意向獲取的值是空或不是你不檢查,這就是拋出NPE。 – 7geeky

+0

我已經深入瞭解這個問題,並發現這一點「com.example.app.SplashActivity.onCreate(SplashActivity.java:27)」檢查此行String link = String.valueOf(Html.fromHtml(Html.fromHtml(path ).toString()));''我看到它被提及爲過時的代碼,你認爲這是導致棉花糖和牛軋糖的問題? –

+0

@ 7geeky請在調試應用程序後檢查問題的編輯更新 –

回答

0
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash); 

    if(getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")){ 
     try { 
      Log.e("splash", "from push link"); 

     path = getIntent().getExtras().getString("link"); 
     banner = getIntent().getExtras().getString("bannerImage"); 
     title = getIntent().getExtras().getString("title"); 
     String link = String.valueOf(Html.fromHtml(Html.fromHtml(path).toString())); 
     String imgurl = String.valueOf(Html.fromHtml(Html.fromHtml(banner).toString())); 
     Log.e("link----", "link--" + link); 

     SharedPreferences pref = getApplicationContext().getSharedPreferences("Push", MODE_PRIVATE); 
     SharedPreferences.Editor editor = pref.edit(); 
     String pushtitle = pref.getString("pushtitle", ""); 

     StringBuilder sb = new StringBuilder(); 
     sb.append(pushtitle).append(title).append(","); 
     Log.e("pushtitle===========", sb.toString()); 
     editor.putString("pushtitle", sb.toString()); 

     String links = pref.getString("link", ""); 
     StringBuilder sb2 = new StringBuilder(); 
     sb2.append(links).append(link).append(","); 
     Log.e("link===========", sb2.toString()); 
     editor.putString("link", sb2.toString()); 

     String imgurlstr = pref.getString("imgurl", ""); 
     StringBuilder sb3 = new StringBuilder(); 
     sb3.append(imgurlstr).append(imgurl).append(","); 
     Log.e("imgurl===========", sb3.toString()); 
     editor.putString("imgurl", sb3.toString()); 

     editor.commit(); 
     } catch (NullPointerException e) { 
      path = ""; 
      banner = ""; 
      title = ""; 
     } 
    }