2013-11-03 30 views
0

我想將自定義對話框的區域設置爲所選圖像。如果我設置整個應用程序的背景圖像,下面的代碼將與一些重新安排一起工作。出於某種原因,當我將它移動到設定自定義對話框我得到以下錯誤的一個區域:關於選擇圖像的NPE

錯誤:

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 

缺少什麼我在這裏?我讀過這可以通過設置意圖來解決 - 但我已經在做這個。任何幫助,將不勝感激。

在此先感謝

CODE:

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); 
      } 
     } 
    } 

} 
+2

什麼是133行? – hichris123

回答

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的問題。

+0

當我做這個改變,它變得顯而易見,它失敗的線是:imageview.setImageDrawable(res);是否可以設置放置在自定義對話框中的圖像視圖? – BigBug

+0

那麼'imageview' null?您應該在對話框中完成圖像的整個設置(您已經在此創建對話框的代碼)。 – Szymon

+1

我明白爲什麼了!謝謝一百萬:) – BigBug

1

hichris123問正確的問題:什麼是133行?這是因爲該行的堆棧跟蹤:

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

NullPointerExceptions的真的很簡單,和一點點對細節的關注將解決這些問題。你不能在null上調用方法,或者引用它的字段。

如果,例如,線133是這樣一個:

  Uri selectedImage = imageReturnedIntent.getData(); 

那麼唯一可能的空實體將成爲imageReturnedIntent - 你要麼需要處理的地方是空不同的情況下,或找出爲什麼它的爲空,並防止它爲空。在任何一種情況下,理解爲什麼它爲空都是很好的。

但要記住的是:使用堆棧跟蹤;它會告訴你究竟哪一行遇到null。知道該行通常直接指向空實體;那麼你有責任找出如何處理null。

+0

正確,但我設置的意圖已在本文中提到:http://stackoverflow.com/questions/10473823/android-get-image-from-gallery-into-imageview下面的方法這篇文章:http://paragchauhan2010.blogspot.ca/2012/05/choose-image-from-gallary-and-display.html ...我的問題是更多我做錯了什麼。它應該被設置 - 正如我所提到的,我可以設置整個應用程序的背景圖像,而不是自定義對話框的背景圖像......我不確定區別在哪裏,或者爲什麼意圖在製作時爲空這個改變? – BigBug