1

我已經看過了很多其他的問題就到這裏之後,從onActivityResult交付結果異常在我的Android應用程序,但沒有似乎是我的幫助。我正在寫作和應用程序,你按下按鈕打開相機。這工作正常,但只要我去保存圖片崩潰,我得到的錯誤。 這裏是我的代碼迄今我得到一個NullPointerException異常和故障使用相機

public class MainActivity extends Activity { 

    private int TAKE_PICTURE = 1337; 
    private Uri outputFileUri; 
    private int screenHeight; 
    private int screenWidth; 
    private ImageView background; 
    private String ORIGINAL_FILE = "photo.jpg"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button take = (Button)findViewById(R.id.take); 
     Button share = (Button)findViewById(R.id.share); 
     take.setOnClickListener(new takeInstr(this)); 
     ImageView background = (ImageView)findViewById(R.id.background); 
    } 

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


    public void startCamera() { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File file = new File(Environment.getExternalStorageDirectory(), ORIGINAL_FILE); 
     outputFileUri = Uri.fromFile(file); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
     startActivityForResult(intent, TAKE_PICTURE); 
    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == TAKE_PICTURE && resultCode != RESULT_CANCELED) { 
      if (data != null) { 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inJustDecodeBounds = true; 
       String file = Environment.getExternalStorageDirectory() + "/test.jpg"; 
       BitmapFactory.decodeFile(file, options); 
       int imageHeight = options.outHeight; 
       int imageWidth = options.outWidth; 
       String imageType = options.outMimeType; 
       DisplayMetrics metrics = this.getResources().getDisplayMetrics(); 
       screenHeight = metrics.heightPixels; 
       screenWidth = metrics.widthPixels; 
       int heightRatio = Math.round((float) imageHeight/(float) screenHeight); 
       int widthRatio = Math.round((float) imageWidth/(float) screenWidth); 
       options.inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
       options.inJustDecodeBounds = false; 
       background.setImageBitmap(BitmapFactory.decodeFile(file, options)); 
      } 
      else { 


      } 
     } 
    } 
} 

package com.example.photoshare; 



import android.view.View; 
import android.view.View.OnClickListener; 

public class takeInstr implements OnClickListener { 
    private MainActivity mainActivity; 
    public takeInstr(MainActivity mainActivity) { 
     this.mainActivity = mainActivity; 
    } 
    @Override 
    public void onClick(View v) { 
     mainActivity.startCamera(); 

    } 

} 

`

我的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<ImageView 
    android:id="@+id/background" 
    android:src="@drawable/great_wall" 
    android:layout_width="fill_parent" 
    android:scaleType="fitXY" 
    android:layout_height="fill_parent" /> 

<LinearLayout 
    android:id="@+id/buttonLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:gravity="center_horizontal" 
    android:weightSum="1.0" > 

    <Button 
     android:id="@+id/take" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:text="@string/take" /> 

    <Button 
     android:id="@+id/share" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:text="@string/share" /> 
</LinearLayout> 

,這裏是我的logcat消息

02-18 12:42:56.266: W/dalvikvm(627): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
02-18 12:42:56.357: E/AndroidRuntime(627): FATAL EXCEPTION: main 
02-18 12:42:56.357: E/AndroidRuntime(627): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=null} to activity {com.example.photoshare/com.example.photoshare.MainActivity}: java.lang.NullPointerException 
02-18 12:42:56.357: E/AndroidRuntime(627): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980) 
02-18 12:42:56.357: E/AndroidRuntime(627): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023) 
02-18 12:42:56.357: E/AndroidRuntime(627): at android.app.ActivityThread.access$1100(ActivityThread.java:123) 
02-18 12:42:56.357: E/AndroidRuntime(627): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177) 
02-18 12:42:56.357: E/AndroidRuntime(627): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-18 12:42:56.357: E/AndroidRuntime(627): at android.os.Looper.loop(Looper.java:137) 
02-18 12:42:56.357: E/AndroidRuntime(627): at android.app.ActivityThread.main(ActivityThread.java:4424) 
02-18 12:42:56.357: E/AndroidRuntime(627): at java.lang.reflect.Method.invokeNative(Native Method) 
02-18 12:42:56.357: E/AndroidRuntime(627): at java.lang.reflect.Method.invoke(Method.java:511) 
02-18 12:42:56.357: E/AndroidRuntime(627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-18 12:42:56.357: E/AndroidRuntime(627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-18 12:42:56.357: E/AndroidRuntime(627): at dalvik.system.NativeStart.main(Native Method) 
02-18 12:42:56.357: E/AndroidRuntime(627): Caused by: java.lang.NullPointerException 
02-18 12:42:56.357: E/AndroidRuntime(627): at com.example.photoshare.MainActivity.onActivityResult(MainActivity.java:69) 
02-18 12:42:56.357: E/AndroidRuntime(627): at android.app.Activity.dispatchActivityResult(Activity.java:4649) 
02-18 12:42:56.357: E/AndroidRuntime(627): at android.app.ActivityThread.deliverResults(ActivityThread.java:2976) 
02-18 12:42:56.357: E/AndroidRuntime(627): ... 11 more 
02-18 12:42:56.686: I/dalvikvm(627): threadid=3: reacting to signal 3 
02-18 12:42:56.726: I/dalvikvm(627): Wrote stack traces to '/data/anr/traces.txt' 
02-18 12:42:57.047: I/dalvikvm(627): threadid=3: reacting to signal 3 
02-18 12:42:57.086: I/dalvikvm(627): Wrote stack traces to '/data/anr/traces.txt' 
02-18 12:42:59.716: I/Process(627): Sending signal. PID: 627 SIG: 9 

我知道我應該做一些什麼時候它返回null,但我不知道該怎麼做,所有的幫助將不勝感激。

+1

'產生的原因:java.lang.ArrayIndexOutOfBoundsException:長度= 1;在com.example.photoshare.MainActivity.onActivityResult(MainActivity.java:75)指數= 1'這不是一個NullPointerException異常...線什麼是75? – Sam 2013-02-18 17:25:36

+0

MainActivity的第75行是什麼?它不是一個空指針... – WarrenFaith 2013-02-18 17:25:38

+0

聽起來像你遇到了堆分配問題(就在崩潰之前Dalvik報告3%的內存空閒)。這會阻止Dalvik將圖像正確傳送到您的應用程序。 – DigCamara 2013-02-18 17:31:56

回答

1

有在你的代碼兩個不同的東西,可能是零。第一個可能是BitmapFactory無法解碼您提供的文件的問題。你也確定你只想解碼test.jpg而不是相機拍攝的那個?

第二個可能是您的background視圖的問題。基於發射類型的活動,它是might happenonActivityResult()之前onCreate()稱爲這將導致background變量仍然爲空。

爲了深入挖掘,應先分配解碼位圖到一個新的變量,看看它是否爲空或不是,並檢查background是空...

+0

謝謝你的幫助。我會試一試,看看它是否能解決問題。 – user2084037 2013-02-19 03:02:40