【问题标题】:Error when call method from another class in android从android中的另一个类调用方法时出错
【发布时间】:2012-06-04 07:20:54
【问题描述】:

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

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 中膨胀

<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 调用方法 callMe():

公共类 CobaGesture 扩展 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:

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)

这是什么意思?以及如何解决?

提前谢谢你。

更新! 这是编辑后的代码和错误:

【问题讨论】:

    标签: android button methods


    【解决方案1】:

    您应该使用正确的上下文初始化对象(在这种情况下,它是您的活动)。原来如此:

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

    将此代码放入onClick事件或onCreate中,这样就可以使用mb.callMe();

    所有关于上下文的信息都是here 和来自Android document

    【讨论】:

      【解决方案2】:
      mb.callMe();
      

      没有启动这就是错误的原因。换句话说,您正在使用一个指向空或空的变量。 如果你想在你的代码中使用其他类,你应该先声明它。

      public void onClick(View v) {
       MainBaby mb = new MainBaby(); //This initiates the variable correctly.
       mb.callMe();
      }
      

      【讨论】:

      • 当我使用上面的代码时,它显示错误。它说“添加参数以匹配 MainBaby(context)”、“添加参数以匹配 MainBaby(context, attrs)”、“添加参数以匹配 MainBaby(context, attrs, defStyle)”
      • 没注意MainBaby类在构造函数中有参数,我的错。
      【解决方案3】:

      在oncreate方法中将对象mb初始化为mb=new MainBaby(CobaGesture.this);

      【讨论】:

      • 我使用了上面的代码,它显示错误。它说“添加参数以匹配 MainBaby(context)”、“添加参数以匹配 MainBaby(context, attrs)”、“添加参数以匹配 MainBaby(context, attrs, defStyle)”–
      • 我已经发布了图片。请检查一下。
      • 查看编辑后的代码,Mainbaby 有带参数的构造器,所以在创建对象时我们必须传递这个 CabaGesture 类的上下文。
      【解决方案4】:

      callme() 是 android 的一种方法,当我们按下任何按钮时都会调用它。 只需将以下方法放在包含至少一个按钮的活动中并运行您的项目。

       public void callMe(View view){
      
                  Log.e("tag", "the button successfully pressed");
              } 
      

      【讨论】:

        猜你喜欢
        • 2018-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-27
        • 2019-03-24
        • 2017-06-21
        • 1970-01-01
        相关资源
        最近更新 更多