啊,大家好,我只是想问一下我正在创建的程序中有什么问题。这是片段代码
PlayActivity.class
//more codes here
public void stFLabel(int numFace, Context ct) {
try {
if(numFace > 0)
faceLebel.setText("Face Hint : I See You Human");
else
faceLebel.setText("Face Hint : Where Are You?");
}
catch(NullPointerException e)
{
e.printStackTrace();
Log.d(TAG, "stFLabel Has Error");
}
}
//more codes here
* CameraPreview.class *
//More codes
...
PlayActivity pacT = new PlayActivity();
...
//More Codes
..
public void pausy(int numFace) {
pacT.stFLabel(numFace, mContext);
}
LOGCAT
04-05 16:11:26.150: D/FaceDetection(27929): face detected: 1 Face 1 Location X: 65Y: -1
04-05 16:11:26.150: W/System.err(27929): java.lang.NullPointerException
04-05 16:11:26.150: W/System.err(27929): at com.delihente.faceplay.PlayActivity.stFLabel(PlayActivity.java:90)
04-05 16:11:26.150: W/System.err(27929): at com.delihente.faceplay.CameraPreview.pausy(CameraPreview.java:62)
04-05 16:11:26.150: W/System.err(27929): at com.delihente.faceplay.CameraPreview$1.onFaceDetection(CameraPreview.java:53)
04-05 16:11:26.150: W/System.err(27929): at android.hardware.Camera$EventHandler.handleMessage(Camera.java)
04-05 16:11:26.150: W/System.err(27929): at android.os.Handler.dispatchMessage(Handler.java)
04-05 16:11:26.150: W/System.err(27929): at android.os.Looper.loop(Looper.java)
04-05 16:11:26.150: W/System.err(27929): at android.app.ActivityThread.main(ActivityThread.java)
04-05 16:11:26.150: W/System.err(27929): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 16:11:26.150: W/System.err(27929): at java.lang.reflect.Method.invoke(Method.java:511)
04-05 16:11:26.150: W/System.err(27929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
04-05 16:11:26.150: W/System.err(27929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
04-05 16:11:26.150: W/System.err(27929): at dalvik.system.NativeStart.main(Native Method)
04-05 16:11:26.150: D/(27929): stFLabel Has Error
更多信息 :
PlayAcvitiy.class extends Activity (Main Class)
CameraPreview.class - The camera preview class provided by google android development
faceLebel = TextView on PlayAcvitiy.class
mContext = PlayActivity.class Context
我想知道是什么导致了 NullPointerException(修复会更好),我已经阅读了近 3 页关于我的问题的谷歌搜索,但没有一个对我有帮助。另外,请不要提及将方法更改为静态,因为这不可能是因为它会在 CameraPreview.class 检测/取消检测人脸时更改 TextView 标签。提前致谢!
编辑:我也在这里阅读了很多相关问题,但似乎都没有帮助
编辑 2:问题已解决,感谢所有人,特别是 PrafulBhatnagar,我了解到在 Main Activity 中使用方法需要引用其上下文。谢谢大家!