Toast 在 Samsung Galaxy S5 上导致 NPE Android 5

Toast cause NPE on Samsung Galaxy S5 with Android 5

这是启动画面的代码(这不是我的愿望,而是客户)-我所做的唯一一件事-只是用加载数据替换一些代码到这个 activity:

public class SplashActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.API_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    GetAll getAllProducts = retrofit.create(GetAll.class);
    Call<ArrayList<Products>> call = getAllProducts.getAll(Constants.PARTNER_ID,
            WaterFragment.getMD5Hash(Constants.PARTNER_ID + Constants.API_KEY));
    call.enqueue(new Callback<ArrayList<Products>>() {
        @Override
        public void onResponse(Response<ArrayList<Products>> response, Retrofit retrofit) {
            if (response.body() != null) {
                ActiveAndroid.beginTransaction();
                try {
                    ProductsDMC product;
                    for (Products p : response.body()) {
                        if (new Select().from(ProductsDMC.class).where("product_id = ?", p.id).executeSingle() != null) {
                            product = new Select().from(ProductsDMC.class).where("product_id = ?", p.id).executeSingle();
                            product.price = p.price;
                        } else {
                            product = new ProductsDMC(p.id, p.article, p.title, p.price, p.amount, p.category_id,
                                    p.brand_id, p.description, p.photo[0], p.option != null ? p.option.bottle_price : "0");
                        }
                        product.save();
                    }
                    ActiveAndroid.setTransactionSuccessful();
                } finally {
                    ActiveAndroid.endTransaction();
                }
            }else{
                Toast.makeText(SplashActivity.this, "Try later!", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFailure (Throwable t){
            Toast.makeText(SplashActivity.this, "Check your connection!", Toast.LENGTH_SHORT).show();
            Log.d("LoloPolo", t.getMessage().toString());
        }
    });
    Intent intent = new Intent(this, ListOrdersActivity.class);
    startActivity(intent);
    finish();
}
}

今晚我的 crashlytic 发送有关应用程序崩溃的信息:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
   at ru.luny.aqualuxe.activity.SplashActivity.onFailure(SplashActivity.java:142)
   at retrofit.ExecutorCallAdapterFactory$ExecutorCallback.run(ExecutorCallAdapterFactory.java:94)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:5832)
   at java.lang.reflect.Method.invoke(Method.java)

是关于这个的:

Toast.makeText(SplashActivity.this, "Try later!", Toast.LENGTH_SHORT).show();

我真的不明白 - Toast.makeText().show 怎么会导致 NPE))

你能帮帮我吗?

编辑

这似乎很奇怪,但 crashlytics 向我显示了错误代码字符串的错误位置。 这与吐司无关,而是关于从异常中获取消息的调试登录。

NPE 归因于 Log.d("LoloPolo", t.getMessage().toString()); 是个大问题。并不总是 getMessage() returns 一个有效的对象。打印 t 应该就足够了。例如

Log.d("LoloPolo", "Error",  t);

(看看here)

那些回调也不会在 UI Thread 上 运行。我不会用它们来展示 `Toast。

问题是t.getMessage().toString()

您在 onFailure (Throwable t) 部分获得 NPE。对于测试用例添加 ""+

 @Override
    public void onFailure (Throwable t){
        Toast.makeText(SplashActivity.this, "Check your connection!", Toast.LENGTH_SHORT).show();
         Log.d("LoloPolo",""+ t.getMessage().toString());
    }