在使用移动相机拍摄该图像后,尝试使用ImageView
绘制图像时,我获得了 FATAL EXCEPTION 。
异常:
06-04 21:11:10.390: D/AndroidRuntime(19857): Shutting down VM
06-04 21:11:10.414: W/dalvikvm(19857): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-04 21:11:10.437: E/AndroidRuntime(19857): FATAL EXCEPTION: main
06-04 21:11:10.437: E/AndroidRuntime(19857): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.nirbhaya/com.example.nirbhaya.MMS}: java.lang.NullPointerException
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.os.Looper.loop(Looper.java:130)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-04 21:11:10.437: E/AndroidRuntime(19857): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 21:11:10.437: E/AndroidRuntime(19857): at java.lang.reflect.Method.invoke(Method.java:507)
06-04 21:11:10.437: E/AndroidRuntime(19857): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-04 21:11:10.437: E/AndroidRuntime(19857): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-04 21:11:10.437: E/AndroidRuntime(19857): at dalvik.system.NativeStart.main(Native Method)
06-04 21:11:10.437: E/AndroidRuntime(19857): Caused by: java.lang.NullPointerException
06-04 21:11:10.437: E/AndroidRuntime(19857): at com.example.nirbhaya.MMS.onActivityResult(MMS.java:124)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-04 21:11:10.437: E/AndroidRuntime(19857): ... 11 more
和代码:: 包com.example.nirbhaya;
public class MMS extends Activity implements OnClickListener {
int TAKE_PHOTO_CODE = 0;
public static int count=0;
EditText preLoc,comeby;
Button ok,capture;
String photo;
String dir;
boolean GPS,flag;
String cityName=null;
String SubThorugh = null;
Intent i;
ImageView iv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mms);
preLoc = (EditText)findViewById(R.id.etPreLoc1);
comeby = (EditText)findViewById(R.id.etComing1);
//ok = (Button)findViewById(R.id.bOK1);
capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.btnCapture:
capturePicture();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
Toast.makeText(getApplicationContext(), "photo saved as: "+photo, Toast.LENGTH_LONG).show();
Bitmap photo = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(photo);
}
}
private void capturePicture() {
//here,we are making a folder named picFolder to store pics taken by the camera using this application
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
// here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.
count++;
String file = dir+count+".jpg";
photo = file;
File newfile = new File(file);
try {
newfile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
}
在添加以下代码后仍然出现以下错误
iv =(ImageView)findViewById(R.id.imageView1);
06-04 22:15:46.515: W/dalvikvm(22711): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-04 22:15:46.546: E/AndroidRuntime(22711): FATAL EXCEPTION: main
06-04 22:15:46.546: E/AndroidRuntime(22711): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.nirbhaya/com.example.nirbhaya.MMS}: java.lang.NullPointerException
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.os.Looper.loop(Looper.java:130)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-04 22:15:46.546: E/AndroidRuntime(22711): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 22:15:46.546: E/AndroidRuntime(22711): at java.lang.reflect.Method.invoke(Method.java:507)
06-04 22:15:46.546: E/AndroidRuntime(22711): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-04 22:15:46.546: E/AndroidRuntime(22711): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-04 22:15:46.546: E/AndroidRuntime(22711): at dalvik.system.NativeStart.main(Native Method)
06-04 22:15:46.546: E/AndroidRuntime(22711): Caused by: java.lang.NullPointerException
06-04 22:15:46.546: E/AndroidRuntime(22711): at com.example.nirbhaya.MMS.onActivityResult(MMS.java:125)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-04 22:15:46.546: E/AndroidRuntime(22711): ... 11 more
06-04 22:15:46.921: D/dalvikvm(22711): GC_CONCURRENT freed 268K, 48% free 2956K/5639K, external 583K/945K, paused 3ms+4ms
答案 0 :(得分:2)
iv
为空。您需要从XML或类似的东西创建它。
例如
iv = (ImageView) findViewById(R.id.my_image_view);
假设你在R.layout.mms