获得空指针,我不知道为什么

时间:2010-11-19 00:32:54

标签: android xml class layout nullpointerexception

我对某些代码有一个NPE,我不知道为什么。我确信这是微不足道的,但我似乎无法找到它 - 这就是我想要做的。

拍摄照片,检测脸部,如果没有脸部,则在ImageView上显示图像,并允许用户拍摄另一张照片。

这是我的代码:

if (facesFound < 1) {
    ImageView imageView = (ImageView) findViewById(R.id.false_view);
    TextView textView = (TextView) findViewById(R.id.badPicText);
    textView.setText(R.string.noFaceFive);
    imageView.setImageBitmap(bitmap565);
}

这是我的方法:

private void detectFaces() {
    //Here is the method I use to snap a photo then detect for faces:
}
public void processCameraImage(Intent intent) {
    setContentView(R.layout.detectlayout); // detectlayout
    ImageView imageView = (ImageView) findViewById(R.id.image_view); // image_view
    cameraBitmap = (Bitmap) intent.getExtras().get("data");
    imageView.setImageBitmap(cameraBitmap);
    detectFaces();
}

奇怪的是,当检测到一张脸时,它的效果很好 - 并将它们放在ImageView上,并说它确实如此

if (facesFound > 0){
    //face detection code
    //write the bmp to an output stream, compress
    ImageView imageView = (ImageView) findViewById(R.id.image_view);
    TextView textView = (TextView) findViewById(R.id.textViewGoodPic);
    textView.setText(R.string.yesFace);
    imageView.setImageBitmap(bitmap565);
}

任何人都知道我可能会出错的地方?

谢谢!

编辑:当应用检测到没有面孔时,应用会崩溃,我尝试将照片和一些文字写入ImageView

编辑:这是logcat

11-18 19:42:39.825: ERROR/AndroidRuntime(4244): FATAL EXCEPTION: main
11-18 19:42:39.825: ERROR/AndroidRuntime(4244): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.project.dtf/com.project.dtf.DTF}: java.lang.NullPointerException
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3589)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3631)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at android.app.ActivityThread.access$3000(ActivityThread.java:129)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2137)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at android.os.Looper.loop(Looper.java:143)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at android.app.ActivityThread.main(ActivityThread.java:4701)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at java.lang.reflect.Method.invoke(Method.java:521)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at dalvik.system.NativeStart.main(Native Method)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244): Caused by: java.lang.NullPointerException
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at com.project.dtf.DTF.detectFaces(DTF.java:130)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at com.project.dtf.DTF.processCameraImage(DTF.java:90)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at com.project.dtf.DTF.onActivityResult(DTF.java:57)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at android.app.Activity.dispatchActivityResult(Activity.java:3890)
11-18 19:42:39.825: ERROR/AndroidRuntime(4244):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3585)

编辑:仍有问题= /

以下是两个电话:

if (facesFound < 1) {
    setContentView(R.layout.detectfalse);
    ImageView imageView = (ImageView) findViewById(R.id.false_view);
    TextView textView = (TextView) findViewById(R.id.badPicText);
    textView.setText(R.string.noFace);
    imageView.setImageBitmap(bitmap565);
}

if (facesFound > 0) {
    showToast(this, "Face Detected!");
    //do stuff

    setContentView(R.layout.detectlayout);
    ImageView imageView = (ImageView) findViewById(R.id.image_view);
    TextView textView = (TextView) findViewById(R.id.textViewGoodPic);
    textView.setText(R.string.yesFace);
    imageView.setImageBitmap(bitmap565);
}

请注意,我在此方法中执行了setContentView

public void processCameraImage(Intent intent) {

    setContentView(R.layout.detectlayout); // detectlayout
    ImageView imageView = (ImageView) findViewById(R.id.image_view); // image_view
    cameraBitmap = (Bitmap) intent.getExtras().get("data");

    imageView.setImageBitmap(cameraBitmap);
    detectFaces();
}
facedetect类被调用之前,

是我出错的地方吗?

1 个答案:

答案 0 :(得分:1)

为什么使用两种布局?我几乎可以肯定你的问题是在没有调用setContentView(R.layout.blabla_whatever)的情况下使用findViewById(R.id.false_view),其中blabla_whatever.xml包含false_view

你为什么不这样做:

ImageView imageView = (ImageView) findViewById(R.id.image_view);    
TextView textView = (TextView) findViewById(R.id.textViewGoodPic);
if (facesFound > 0){    
    //face detection code    
    //write the bmp to an output stream, compress    
    textView.setText(R.string.yesFace);    
    imageView.setImageBitmap(bitmap565);    
}else{
    textView.setText(R.string.noFaceFive);    
    imageView.setImageBitmap(bitmap565);    
}

再次......除非您展示完全不同的布局,否则您不需要使用不同的ImageViewsTextView来显示结果。

如果您坚持使用不同的布局,请务必在使用setContentView(R.layout.noface_layout)方法之前致电findViewById

修改

现在我知道为什么你认为你需要另一种布局。您可以只使用一个布局。这就是你能做的:

OnClickListener editPhoto = new OnClickListener(){
   public void onClick(){
       // edit photo stuff
   }
};

OnClickListener takePhotoAgain = new OnClickListener(){
   public void onClick(){
       // take the photo again
   }
};

ImageView imageView = (ImageView) findViewById(R.id.image_view);    
TextView textView = (TextView) findViewById(R.id.textViewGoodPic);
Button actionButton = (Button) findViewById(R.id.some_id);

if (facesFound > 0){    
    //face detection code    
    //write the bmp to an output stream, compress    
    textView.setText(R.string.yesFace);    
    imageView.setImageBitmap(bitmap565);    
    actionButton.setText("Face detected, edit it!");
    actionButton.setOnClickListener(editPhoto);
}else{
    textView.setText(R.string.noFaceFive);    
    imageView.setImageBitmap(bitmap565);    
    actionButton.setText("You are too ugly for me, take another picture!");
    actionButton.setOnClickListener(takePhotoAgain);
}