-1

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");
}   
}

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>

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

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();
        }
    });      
}

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?

thank you in advance.

UPDATE! here's the edited code and the error: enter image description here

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
abuybuy
  • 799
  • 2
  • 16
  • 33

4 Answers4

2

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());

Put this code into onClick event or onCreate, so you can use mb.callMe();

All about the context is here and from Android document

Community
  • 1
  • 1
ductran
  • 10,043
  • 19
  • 82
  • 165
0
mb.callMe();

Is not institiated thats the reason of the error. In other words, you are using a variable that points to nothing or Null. If you want to use other classes in your code your should declare it first.

public void onClick(View v) {
 MainBaby mb = new MainBaby(); //This initiates the variable correctly.
 mb.callMe();
}
Shiin Zu
  • 166
  • 9
  • when i used the code above, it display error. it says "add argument to match MainBaby(context)", "add argument to match MainBaby(context, attrs)", "add argument to match MainBaby(context, attrs, defStyle)" – abuybuy May 30 '12 at 04:21
  • I didn't pay attention that the MainBaby class had parameters in the constructor, my bad. – Shiin Zu May 30 '12 at 17:36
0

Initialize the object mb as mb=new MainBaby(CobaGesture.this); inside the oncreate method

Ponmalar
  • 6,871
  • 10
  • 50
  • 80
  • i've used the code above, it display error. it says "add argument to match MainBaby(context)", "add argument to match MainBaby(context, attrs)", "add argument to match MainBaby(context, attrs, defStyle)" – – abuybuy May 30 '12 at 04:22
  • i've posted the image. please check it out. – abuybuy May 30 '12 at 04:41
  • see the edited code, Mainbaby has the contructor with paramater, so while creating the object we have to pass context of this CabaGesture class. – Ponmalar May 30 '12 at 04:56
-2

callme() is a method of android which is called when in view when we pressed any button . just put this below method in activity which contain atleast one button and run your project .

 public void callMe(View view){

            Log.e("tag", "the button successfully pressed");
        } 
Bhagvati
  • 27
  • 3