【问题标题】:Toast cause NPE on Samsung Galaxy S5 with Android 5 [duplicate]Toast 导致带有 Android 5 的三星 Galaxy S5 上的 NPE [重复]
【发布时间】:2016-02-01 08:23:08
【问题描述】:

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

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$3.onFailure(SplashActivity.java:142)
   at retrofit.ExecutorCallAdapterFactory$ExecutorCallback$2.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 显示错误代码字符串的错误位置。 这不是 toast,而是 debug-loggin 从异常中获取消息。

【问题讨论】:

    标签: java android samsung-mobile android-developer-api


    【解决方案1】:

    问题是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());
        }
    

    【讨论】:

      【解决方案2】:

      NPE 是由于Log.d("LoloPolo", t.getMessage().toString()); 造成的,这是一个很大的问题。 getMessage() 并非总是返回有效对象。打印t 就足够了。例如

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

      (看看here

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-07
        • 1970-01-01
        • 2015-09-04
        • 2015-02-15
        • 1970-01-01
        • 2015-08-24
        • 1970-01-01
        • 2012-01-24
        相关资源
        最近更新 更多