setText中的空指针异常

时间:2018-01-24 06:24:44

标签: java android nullpointerexception textview filenames

我正在尝试使用mediastore获取文件的名称,并使用setText()在文本视图中显示它,但setText()在运行的应用程序上提供空指针异常。 我从mediastore获取名称,将其保存为字符串并将字符串传递给setText()。有人可以帮忙吗?我是初学者。

case R.id.action_properties:
                AlertDialog.Builder builder = new AlertDialog.Builder(PhotosActivity.this);
                LayoutInflater inflater = this.getLayoutInflater();
                builder.setMessage("Properties");
                builder.setView(R.layout.properties);
                String [] projection={MediaStore.Images.Media.DATA};
                uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                cursor = getApplication().getContentResolver().query(uri, projection, null, null, null);// Order-by clause (ascending by name)
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                TextView displayname = (TextView) findViewById(R.id.displayname);
                displayname.setText(cursor.getString(column_index));
                builder.setView(inflater.inflate(R.layout.properties, null));
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert12 = builder.create();
                alert12.show();
            // location found
                return true;

LOGCAT:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                at com.example.dell_1.myapp3.ImageViewer.PhotosActivity.onOptionsItemSelected(PhotosActivity.java:302)
                                                                                at android.app.Activity.onMenuItemSelected(Activity.java:3245)
                                                                                at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
                                                                                at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
                                                                                at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
                                                                                at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
                                                                                at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
                                                                                at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:206)
                                                                                at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:776)
                                                                                at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                                at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:88)
                                                                                at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                                at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                                at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:127)
                                                                                at android.widget.AdapterView.performItemClick(AdapterView.java:310)
                                                                                at android.widget.AbsListView.performItemClick(AbsListView.java:1155)
                                                                                at android.widget.AbsListView$PerformClick.run(AbsListView.java:3133)
                                                                                at android.widget.AbsListView$3.run(AbsListView.java:4048)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:1)

像这样使用

    AlertDialog.Builder builder = new AlertDialog.Builder(PhotosActivity.this);
    View dialogView = inflater.inflate(R.layout.properties, null);
    builder.setView(dialogView);

    TextView editText = (TextView) dialogView.findViewById(R.id.yourtextview);