uriString nullPointer错误

时间:2016-12-21 17:31:03

标签: android image android-intent uri

在活动A中,它有listView和一个图标,用于意图活动B.

 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { //listview
            public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
                Expenses o = (Expenses) obj.getItem(position);
                Uri image = o.getImage();
                Intent intent = new Intent(QuickExpenses.this,AddExpenses.class);
                intent.putExtra("image",image.toString());
                startActivity(intent);
            }
        });

  @Override
    public boolean onOptionsItemSelected(MenuItem item) { // get action bar icon
    switch (item.getItemId()) {
            case R.id.action_add_task:
                mClickedPosition = -1;
                Intent intent = new Intent(QuickExpenses.this, AddExpenses.class);
                startActivityForResult(intent, PROJECT_REQUEST_CODE);
                return true;
    }
}

活动B

 if(getIntent().getExtras()!=null) { //if  has value pass from A
            Uri imageUri=Uri.parse(getIntent().getStringExtra("image"));
            if(imageUri!=null) {
                imageView.setImageURI(imageUri);
            }
            else {
              Toast.makeText(getApplication(),"null",Toast.LENGTH_SHORT).show();
            }
        }

日志

12-22 01:26:12.217  25588-25588/com.example.tony.monthlyexpenses E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tony.monthlyexpenses/com.example.tony.monthlyexpenses.AddExpenses}: java.lang.NullPointerException: uriString
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2372)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
            at android.app.ActivityThread.access$600(ActivityThread.java:169)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1388)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5433)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:924)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException: uriString

我按下listview,一切似乎都很好,我可以看到图像显示在imageview中,但是当我点击图标时,应用程序崩溃了。我添加了if(imageUri!=null)但它仍然崩溃了。为什么会这样?

我遵循此https://stackoverflow.com/a/25171292/5156075

错误指向Uri imageUri=Uri.parse(getIntent().getStringExtra("image"));

1 个答案:

答案 0 :(得分:2)

更新支票。您正在尝试解析可以为null的String,因此在将其解析为uri之前,请检查空值。

String image = getIntent().getExtras().getString("image");
if(image!= null) {
Uri imageUri=Uri.parse(image);
}