2013-08-21 163 views
-1

需要此幫助。看了很多帖子,但無法弄清楚。有人能幫助我嗎?拍攝照片後應用程序崩潰

錯誤 - [{ResultInfo誰= NULL,請求= 2,結果= -1,數據=意向{ ACT =直列數據DAT =內容://媒體/外部/圖像/媒體/ 26698(有 演員)}}]

public int GET_CAM_IMG=2; 
public int GET_GAL_IMG=1; 

img1.setOnClickListener(new OnClickListener() { 

    public void onClick(View v){ 
    CharSequence[] names = { "From Gallery", "From Camera" }; 
    new AlertDialog.Builder(context) 
      .setTitle("Select an option for updating your Profile Picture") 
      .setItems(names, new DialogInterface.OnClickListener() { 

      @Override 
       public void onClick(DialogInterface dialog, int pos) { 
        // TODO Auto-generated method stub 
        if (pos == 0) { 

         Intent i = new Intent(
           Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  


         startActivityForResult(i, GET_GAL_IMG); 


        } else { 


         Intent i = new Intent(
           android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

        /** 
         i.putExtra("crop", "true"); 
         i.putExtra("aspectX", 0); 
         i.putExtra("aspectY", 0); 
         i.putExtra("outputX", 200); 
         i.putExtra("outputY", 150);**/ 



         startActivityForResult(i, GET_CAM_IMG); 

        }}} 


      ) 
      .setNegativeButton(android.R.string.cancel, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int which) { 
         } 
        }).create().show(); 
    } 
    });} 








    @Override 
public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    super.onActivityResult(requestCode, resultCode, intent); 
    switch (requestCode) { 

    case 2://Camera 
     Log.d("take","pic"); 
     if (resultCode == -1) { 
      Uri selectimage=intent.getData(); 
      Log.d("take","pic"); 
      img1.setImageURI(selectimage); 




     } 
     break; 
    case 3://Selecting from Gallery 
     Log.d("view","pic"); 
     if (resultCode == -1) { 
      Bitmap bmp_image = null; 
       Bundle extras = intent.getExtras(); 
       bmp_image = (Bitmap) extras.get("data"); 



       img1.setImageBitmap(bmp_image); 

     } 
     break; 
    } 
} 

logcat的輸出:

08-21 12:46:46.269: W/dalvikvm(6566): threadid=1: thread exiting with uncaught exception (group=0x41096438) 
08-21 12:46:46.299: E/AndroidRuntime(6566): FATAL EXCEPTION: main 
08-21 12:46:46.299: E/AndroidRuntime(6566): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/26702 (has extras) }} to activity {com.inspectreport6784/com.inspectreport6784.NewInspection}: java.lang.NullPointerException 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3208) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3251) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at android.app.ActivityThread.access$1200(ActivityThread.java:143) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at android.os.Looper.loop(Looper.java:137) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at android.app.ActivityThread.main(ActivityThread.java:4961) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at java.lang.reflect.Method.invoke(Method.java:511) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at dalvik.system.NativeStart.main(Native Method) 
08-21 12:46:46.299: E/AndroidRuntime(6566): Caused by: java.lang.NullPointerException 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at com.inspectreport6784.NewInspection.onActivityResult(NewInspection.java:169) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at android.app.Activity.dispatchActivityResult(Activity.java:5363) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3204) 
08-21 12:46:46.299: E/AndroidRuntime(6566):  ... 11 more 
+0

我們需要來自logcat的堆棧跟蹤來提供幫助。 – Simon

回答

0

你有NullPointerException在線169。不知道是什麼行是...

我們知道你的代碼塊是:

Uri selectimage=intent.getData(); 
Log.d("take","pic"); 
img1.setImageURI(selectimage); 

因爲你ResultInfo顯示request=2, result=-1。這意味着你的空指針在那裏。

最可能的原因是img1爲空。我看到你在上面使用它,但是它在後面的範圍內是未知的。不知道如何或何時初始化,很難說。

這裏沒有太多可以發生的事情。唯一的另一種可能性是intent爲空,但我不算太多。你的ResultInfo清楚地顯示回來的意圖。


未來,您可以自己在logcat中找到這些東西。只要尋找類似:

Caused by: java.lang.NullPointerException 
    at com.inspectreport6784.NewInspection.onActivityResult(NewInspection.java:169) 

對於簡單的空指針,它很容易縮小。它爲您提供文件,類,方法和行。

+0

img1.setImageURI(selectimage);是第169行。我怎樣才能解決它。新的這個Java所以將欣賞所有的幫助 – Ahsan

+0

然後'img1'爲空。很可能它超出了範圍。嘗試將其設爲班級成員。您可能還想閱讀Java中的範圍。 – Geobits

+0

哎,它的工作,我把ImageView的IMG1 =(ImageView的)findViewById(R.id.img1);右上方img1.setUmageURI(選擇圖片);現在我必須找出方向。它在方向改變時崩潰,但至少我得到圖片。你的空指針幫助了我。直到現在還不知道它是什麼 – Ahsan

相關問題