uriString nullPointer错误

uriString nullPointer Error(uriString nullPointer错误)

本文介绍了uriString nullPointer错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在活动A中,它有一个列表视图和一个图标,用于活动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"));

推荐答案

更新您的检查。您正在尝试分析可以为NULL的字符串,因此在将其解析为URI之前,请检查是否有NULL值。

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

这篇关于uriString nullPointer错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:uriString nullPointer错误

基础教程推荐