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异常(A修复会更好), 我看了一下我的问题,近3页的谷歌搜索,但没有它具有帮助过我。 也请不要提及将方法更改为static,因为它不能因为它在CameraPreview.class检测到/未检测到面时更改TextView标签。 在此先感谢!
编辑:我也看过很多相关的问题在这里,但他们都不来帮助
编辑2:问题迎刃而解,感谢所有,特别是对PrafulBhatnagar,我已经了解到,使用方法在主要活动中需要参考其上下文。感谢大家!
+0
尝试'((PlayActivity)mContext ).stFLabel(numFace,mContext);'...假设'mContext'存储对'PlayActivity'的引用 –
2013-04-05 08:23:55
+0
你试图从一个空对象中调用/获取某些东西。当你调用setText()时,它可能是“faceLebel”。调试它 –
2013-04-05 08:24:41
+0
'faceLebel'是否正确声明?似乎它是唯一可以抛出的东西。 –
2013-04-05 08:25:54