0

我想将自定义对话框的区域设置为所选图像。如果我正在设置整个应用程序的背景图像,则下面的代码可以进行一些重新排列。出于某种原因,当我移动它以设置自定义对话框的区域时,我收到以下错误:

错误:

11-03 18:19:05.216: E/AndroidRuntime(10428): FATAL EXCEPTION: main
11-03 18:19:05.216: E/AndroidRuntime(10428): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://media/external/images/media/700 }} to activity {com.example.pictures/com.example.pictures.MainActivity}: java.lang.NullPointerException
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.os.Looper.loop(Looper.java:130)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread.main(ActivityThread.java:3691)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at java.lang.reflect.Method.invokeNative(Native Method)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at java.lang.reflect.Method.invoke(Method.java:507)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at dalvik.system.NativeStart.main(Native Method)
11-03 18:19:05.216: E/AndroidRuntime(10428): Caused by: java.lang.NullPointerException
11-03 18:19:05.216: E/AndroidRuntime(10428):    at com.example.pictures.MainActivity.onActivityResult(MainActivity.java:133)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.Activity.dispatchActivityResult(Activity.java:3950)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
11-03 18:19:05.216: E/AndroidRuntime(10428):    ... 11 more

我在这里想念什么?我已经读过这可以通过设置意图来解决 - 但我已经在这样做了。任何帮助,将不胜感激。

提前致谢

代码:

public class MainActivity extends Activity {
    private static final int SELECT_PHOTO = 100;
    private Bitmap chosenBitmap; 
    final Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final DrawingView drawing = new DrawingView(this); 

        Button button = (Button) findViewById(R.id.buttonShowCustomDialog);

        // add button listener
        button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {
              final Dialog dialog = new Dialog(context);
            // custom dialog
            //dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });


            Button captureButton = (Button) dialog.findViewById(R.id.selectImage);
            captureButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                    photoPickerIntent.setType("image/*");
                    startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
                }
            });
            dialog.show();
          }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
        final Dialog dialog = new Dialog(context);

        switch(requestCode) { 
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                ImageView imageview= (ImageView)dialog.findViewById(R.id.imageview);
                chosenBitmap = yourSelectedImage; 
                Drawable res = new BitmapDrawable(yourSelectedImage);
                imageview.setImageDrawable(res);
            }
        }
    }

}
4

2 回答 2

1

hichris123 提出了正确的问题:第 133 行是什么?这是因为堆栈跟踪中的这一行:

11-03 18:19:05.216: E/AndroidRuntime(10428): 在 com.example.pictures.MainActivity.onActivityResult(MainActivity.java:133)

NullPointerExceptions 真的很简单,稍微注意一下细节就可以解决它们。您不能在 null 上调用方法,或引用其字段。

例如,如果第 133 行是这一行:

            Uri selectedImage = imageReturnedIntent.getData();

那么唯一可能的空实体将是 imageReturnedIntent - 您要么需要以不同方式处理它为空的情况,要么找出它为空的原因并防止它为空。在任何一种情况下,最好理解它为什么为空。

但要记住的是:使用堆栈跟踪;它准确地告诉你哪一行遇到了空值。知道那条线通常直接指向空实体;那么你有责任弄清楚如何处理空值。

于 2013-11-03T23:30:05.853 回答
1

当您执行该代码时:

InputStream imageStream = null;
try {
    imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

你实际上有一个例外,代码继续,你imageStream的仍然是空的。

您应该更改代码以适应这种情况并检查是否为空:

if (imageStream != null) {
    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
    ImageView imageview= (ImageView)dialog.findViewById(R.id.imageview);
    chosenBitmap = yourSelectedImage; 
    Drawable res = new BitmapDrawable(yourSelectedImage);
    imageview.setImageDrawable(res);
}

或以其他适当的方式处理这种情况。

在这种情况下,这可能不是导致 NPE 的问题。

于 2013-11-03T23:26:31.607 回答