【问题标题】:Getting Null Pointer and I have no idea why获取空指针,我不知道为什么
【发布时间】:2010-11-19 00:32:54
【问题描述】:

我在某些代码上有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 类甚至被调用之前,这是我出错的地方吗?

【问题讨论】:

  • NPE 的堆栈跟踪表明它来自哪一行代码?
  • 这些行:ImageView imageView = (ImageView) findViewById(R.id.false_view); TextView textView = (TextView) findViewById(R.id.badPicText); textView.setText(R.string.noFaceFive); imageView.setImageBitmap(bitmap565);
  • 你确定你做了 setContentView 并且你设置它的布局包含 R.id.false_view 和 R.id.badPicText 吗?位图bitmap565设置了吗?你能给我们提供异常的确切堆栈跟踪吗?请复制粘贴整个异常。并告诉我们它所指的确切线。谢谢你:)
  • 所以这就是我认为我的问题所在,既然你说......在我的方法 processCameraImage 中,我将内容视图设置为 detectlayout.xml,但我真的希望内容视图是如果人脸检测没有找到人脸,则detectfalse,如果找到,则为detectlayout - 我想我错了,我不知道如何解决它。
  • 第 130 行是:textView.setText(R.string.noFaceFive);顺便说一句

标签: android xml class layout nullpointerexception


【解决方案1】:

为什么要使用两种布局?我几乎可以肯定您的问题是使用 findViewById(R.id.false_view) 而不调用 setContentView(R.layout.blabla_whatever),其中 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 来显示结果。

如果您坚持使用不同的布局,请确保在使用findViewById 方法之前调用setContentView(R.layout.noface_layout)

编辑

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

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);
}

【讨论】:

  • 嗯,我的一个布局上有一个按钮,可以再次拍摄照片,并显示未检测到人脸!而另一个布局显示人脸检测!并且有一个按钮,允许用户裁剪面部并将其用作头像......所以我真的需要两种不同的布局......一种用于检测到面部时,另一种用于检测到面部时。
  • 天哪,这是我的疏忽——感谢克里斯蒂安!有不同的布局编码不好?我希望能够使用一个,但每个布局显示不同的属性 =/
  • 当结构发生根本变化时(例如,如果纵向和横向的结构不同),则可以使用不同的布局。就您而言,正如我在回答中所展示的那样,您只需修改布局的一些元素就可以了。
  • 反过来,我会说 3 种不同可接受的布局,即:如果我想要一个声音说“文件已保存”,而另一个是图片?尽管应用程序很小,但我不想膨胀代码
  • 布局越多,生成的代码就越复杂……大多数情况下,您都可以使用上述技巧。
猜你喜欢
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多