致命异常:在Android的主要NullPointerException异常 [英] FATAL EXCEPTION: main NullPointerException in Android

查看:112
本文介绍了致命异常:在Android的主要NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班。其中之一是活动。我在活动课的函数,并尝试在其他类调用。在功能上,我不能使用视图功能(的setContentView,findViewById)这些功能效果很好的onCreate。我试图改变图像的位置和大小。

之类的函数定义:

 公共无效changePosition(INT X,INT Y,INT Z){

}
 

我总是得到这个错误。为什么这些功能只适用于的onCreate?

logcat的:

  E / AndroidRuntime:致命异常:主要
    工艺:com.example.project.scan,PID:26122
    显示java.lang.NullPointerException
            在android.app.Activity.findViewById(Activity.java:1970)
            在com.example.project.scan.ProjectActivity.changePosition(ProjectActivity.java:255)
            在com.example.project.scan.Model $ 1.handleMessage(Model.java:145)
            在android.os.Handler.dispatchMessage(Handler.java:102)
            在android.os.Looper.loop(Looper.java:157)
            在android.app.ActivityThread.main(ActivityThread.java:5356)
            在java.lang.reflect.Method.invokeNative(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:515)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            在dalvik.system.NativeStart.main(本机方法)
 

ProjectActivity.java:255:

 图像=(ImageView的)findViewById(R.id.im);
 

Model.java:145:

  myActivity.changePosition(X,Y,Z);
 

解决方案

好像你实例化你的 myActivity 。从来没有实例化活动,因为他们将无法正常进行任何初始化你使用的活动。

使用的意图来实例化活动,正确的生命周期的初始化,或通过活动引用(的活动)到需要调用的活性的方法等方法。

I have two class. One of them is activity. I have a function in activity class and try to call in other class. In function i can't use view functions(setContentView, findViewById) These functions works well in onCreate. I try to change image position and size.

function definition like:

public void changePosition(int x, int y, int z){

}

I always got this error. Why these functions works only in onCreate?

Logcat:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.scan, PID: 26122
    java.lang.NullPointerException
            at android.app.Activity.findViewById(Activity.java:1970)
            at com.example.project.scan.ProjectActivity.changePosition(ProjectActivity.java:255)
            at com.example.project.scan.Model$1.handleMessage(Model.java:145)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

ProjectActivity.java:255 :

image = (ImageView)findViewById(R.id.im);

Model.java:145:

myActivity.changePosition(x,y,z);

解决方案

Seems like you've instantiated your myActivity with new. Never instantiate activities with new as they won't be properly initialized for anything you'd use an activity for.

Use an Intent to instantiate activities with correct lifecycle initialization, or pass the activity reference (this in an activity) to other methods that need to call methods in the activity.

这篇关于致命异常:在Android的主要NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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