2015-05-28 64 views
0

我正在嘗試使用ImageView在手機中顯示圖像。 這裏是我的ImageActivity活動:ImageView中的空指針異常

package com.example.apptest1; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ImageView; 

public class ImageActivity extends Activity { 

private static final int REQUEST_CODE = 0; 
private Bitmap bitmap; 
private ImageView imageView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_image); 
    ImageView imageView = (ImageView) findViewById(R.id.result); 

} 

public void pickImage(View View) { 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 
    startActivityForResult(intent, REQUEST_CODE); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     InputStream stream = null; 
     if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) 
      try { 
      // recyle unused bitmaps 
      if (bitmap != null) { 
       bitmap.recycle(); 
      } 
      stream = getContentResolver().openInputStream(data.getData()); 
      bitmap = BitmapFactory.decodeStream(stream); 

      imageView.setImageBitmap(bitmap); 
      } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      } finally { 
      if (stream != null) 
       try { 
       stream.close(); 
       } catch (IOException e) { 
       e.printStackTrace(); 
       } 
      } 
     } 



} 

這裏是activity_image.xml

<LinearLayout 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="${relativePackage}.${activityClass}" 
android:orientation="vertical"> 


<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="pickImage" 
    android:text="Button" > 
</Button> 

<ImageView 
    android:id="@+id/result" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
</ImageView> 


</LinearLayout> 

但是當我嘗試運行應用程序,它會顯示以下logcat的和突然停止 logcat的:

05-28 23:11:48.226: E/AndroidRuntime(6115): FATAL EXCEPTION: main 
05-28 23:11:48.226: E/AndroidRuntime(6115): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/1242 flg=0x1 }} to activity {com.example.apptest1/com.example.apptest1.ImageActivity}: java.lang.NullPointerException 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3525) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3568) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at android.app.ActivityThread.access$1100(ActivityThread.java:162) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at android.os.Handler.dispatchMessage(Handler.java:107) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at android.os.Looper.loop(Looper.java:194) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at android.app.ActivityThread.main(ActivityThread.java:5371) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at java.lang.reflect.Method.invoke(Method.java:525) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:833) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at dalvik.system.NativeStart.main(Native Method) 
05-28 23:11:48.226: E/AndroidRuntime(6115): Caused by: java.lang.NullPointerException 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at com.example.apptest1.ImageActivity.onActivityResult(ImageActivity.java:48) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at android.app.Activity.dispatchActivityResult(Activity.java:5311) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3521) 
05-28 23:11:48.226: E/AndroidRuntime(6115):  ... 11 more 

我已經打了勾,並檢查ImageView爲空,但無法弄清楚爲什麼/如何。有人請清除我的懷疑。謝謝提前

回答

3

簡單的範圍問題。

此行

ImageView imageView = (ImageView) findViewById(R.id.result); 

必須

imageView = (ImageView) findViewById(R.id.result); 

爲什麼?因爲你現在這樣做的方式意味着你已經聲明瞭一個新的imageView,而不是使用之前已經聲明的那個。您已經將原先聲明的imageView保留下來,並且創建了一個全新的完全無用的版本,並在onCreate()完成後消失。當你打電話給imageView.setImageBitmap(bitmap);時,你會得到一個NPE,因爲它試圖使用你的類中聲明的imageView。

ImageView imageView = (ImageView) findViewById(R.id.result); 

隱藏全局imageView成員 - :

+0

非常感謝..我怎麼會錯過那個愚蠢的事情.. – ghostrider

1

onCreate方法,則需要通過創建一個局部變量。 所以,不要低於onCreate: -

imageView = (ImageView) findViewById(R.id.result); 

而空指針應該去。