(解决)调用公共方法导致NullPointerException异常 - Android电子 [英] (SOLVED) Calling A Public Method Causes NullPointerException - Android

查看:292
本文介绍了(解决)调用公共方法导致NullPointerException异常 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AHM,大家好,我只想问什么是我创建的程序问题。下面是摘录codeS

Ahm, Hey everyone, I just want to ask what is the problem in the program I am creating. Here is the Snippet Codes

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

* 相机preview.class 的*

*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页谷歌搜索我的问题,但没有它的帮助我。
而且,请不要提及的方法更改为静态的,因为它不可能是因为它当Camera preview.class检测/ Undetects面临改变的TextView标签..
在此先感谢!

I want to know what is causing the NullPointerException (A Fix will be Better), I have read almost 3 pages of google search about my problem but none of it has helped me. and also, please don't mention to change the method to static because It cannot be because it changes the TextView Label when the CameraPreview.class detects/Undetects Faces.. Thanks in Advance!

编辑:我也看了这么多相关的问题在这里,但他们都不来帮助

EDIT : I have also read so many related questions here, but none of them seem to help

编辑2:问题解决了,感谢所有,特别是对PrafulBhatnagar,我已经学会了使用中的主要活动的方法需要上下文的参考。谢谢大家!

EDIT 2 : Problem Solved, thanks to all, specially to PrafulBhatnagar, I have learned that Using a method in the Main Activity needs a reference to its context. Thanks Everyone!

推荐答案

在Android的你不应该自己创建活动的对象。

In android you should never create Activity object on your own..

当您从应用程序启动应用程序托盘系统,为您启动活动创建对象并启动它。我们使用 startActivity()如果我们想要去从一个屏幕到另一个,这里又系统创建活动 ..

When you launch application from app tray the System creates Object for your Launcher Activity and launch it.. We use startActivity() if we want to go from one screen to another, here again system creates the object of Activity..

所以一般来说系统创建活动的对象,并调用它的生命周期方法如的onCreate(),在onStart()等。而我们使用这些生命周期回调初始化/ DE初始化活动状态 ..

So in general the system creates object of Activity and calls its life cycle method like onCreate(), onStart() etc.. And we use these lifecycle callback to initialize/de-initialize state of the Activity..

在code的问题是,您要自己创建活动对象;所以现在你code创建由系统生成的两个实例之一,所有国家已被初始化,一个,其中的类的状态一直没有初始化因为有以生命周期的方法没有调用因此的NullPointerException

The problem in your code is that you are trying to create Activity object on your own; so now there are two instances one created by system where all the state has been initialized and one created by your code where the state on the class has not been initialize since there was no call to lifecycle method hence the NullPointerException.

您可以使用以下code修正这个错误:

You can use the following code to fix this error:

((PlayActivity)mContext).stFLabel(numFace,mContext); ...假设 mContext 存储参照 PlayActivity

((PlayActivity)mContext).stFLabel(numFace, mContext);... Assuming mContext stores the reference to the PlayActivity

希望它可以帮助..

这篇关于(解决)调用公共方法导致NullPointerException异常 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆