当错误在Android的另一个类调用方法 [英] Error when call method from another class in android

查看:145
本文介绍了当错误在Android的另一个类调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个班。一个是MainBaby.java,它包含:

i have 2 class. One is MainBaby.java and it contains:

public class MainBaby extends SurfaceView implements SurfaceHolder.Callback {

public MainBaby(Context context){
    super(context);
    init();
}

public MainBaby(Context context, AttributeSet attrs) {
      super(context, attrs);
      // TODO Auto-generated constructor stub
      init();
}

public MainBaby(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      // TODO Auto-generated constructor stub
      init();
}

public void init(){
    // adding the callback (this) to the surface holder to intercept events

    getHolder().addCallback(this);

    // create the game loop thread
    thread = new ThreadBaby(getHolder(), this);

    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    p = new Paint();
    p.setAntiAlias(true);
    p.setDither(true);
    p.setColor(Color.BLACK);
    p.setStyle(Paint.Style.STROKE);
    p.setStrokeJoin(Paint.Join.ROUND);
    p.setStrokeCap(Paint.Cap.ROUND);
    p.setStrokeWidth(50);

    setFocusable(true);

}

public void callMe(){

    Log.v("tag", "the button successfully pressed");
}   
}

本级虚增main.xml中

this class in inflated in main.xml

<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
  <coba.gesture.Main
    android:id="@+id/LayoDalam"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</AbsoluteLayout>

和我想在cobaGesture.java调用方法呼我()是这样的:

and i want to call method callMe() at cobaGesture.java like this:

公共类CobaGesture延伸活动{
    MainBaby MB;

public class CobaGesture extends Activity { MainBaby mb;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  
    ImageButton resetBut = (ImageButton) findViewById(R.id.reset);
    resetBut.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mb.callMe();
        }
    });      
}

但是当我点击该按钮,应用程序被强制关闭,这里的logcat的:

but when i click the button, the application was force closed and here's the logcat:

05-30 10:55:58.433: ERROR/AndroidRuntime(704): FATAL EXCEPTION: main
05-30 10:55:58.433: ERROR/AndroidRuntime(704): java.lang.NullPointerException
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at coba.gesture.CobaGesture$1.onClick(CobaGesture.java:41)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at android.view.View.performClick(View.java:2485)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at android.view.View$PerformClick.run(View.java:9080)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at android.os.Handler.handleCallback(Handler.java:587)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at android.os.Looper.loop(Looper.java:123)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at android.app.ActivityThread.main(ActivityThread.java:3647)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at java.lang.reflect.Method.invokeNative(Native Method)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at java.lang.reflect.Method.invoke(Method.java:507)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-30 10:55:58.433: ERROR/AndroidRuntime(704):     at dalvik.system.NativeStart.main(Native Method)

这是什么意思?如何解决呢?

what does it means? and how to solve it?

感谢您提前。

更新!
这里的编辑code和错误:

UPDATE! here's the edited code and the error:

推荐答案

您最初应该用正确的上下文对象(在这种情况下,你的活动)。因此,它是:

You should initial object with the correct context (in this case, it is your activity). So it is:

  mb = new MainBaby(CobaGesture.this);
OR:
  mb = new MainBaby(getApplicationContext());

将这个code到onClick事件或的onCreate,这样你就可以使用 mb.callMe();

所有关于上下文这里和的 Android的文件

这篇关于当错误在Android的另一个类调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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