jigar
jigar

Reputation: 1591

Touchevent program in android not working

I've made a simple singleTouch program for learning purpose,I have tried the fllowing code but its not working I am not getting what exactly the problem is why my program is not runnig..please help me.my code is as below:

singleTouchView.java

package com.example.singletouch;

import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Switch;

public class SingleTouchView extends View{
private Paint paint;
private Path path;
    public SingleTouchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(3f);
        paint.setColor(color.black);
        paint.setStrokeJoin(Paint.Join.ROUND);

    }

    public void onDraw(Canvas canvas){
        canvas.drawPath(path,paint);
        }
    public boolean onTouchEvent(MotionEvent me){
        float eventX=me.getX();
        float eventY=me.getY();

        switch (me.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(eventX, eventY);
            break;
        case MotionEvent.ACTION_UP:
            break;
        }
        invalidate();
        return true;

    }

}

main.java

package com.example.singletouch;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SingleTouchView(MainActivity.this, null));
    }   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Logcat

07-12 11:42:01.283: E/AndroidRuntime(771): FATAL EXCEPTION: main
07-12 11:42:01.283: E/AndroidRuntime(771): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.singletouch/com.example.singletouch.MainActivity}: java.lang.NullPointerException
07-12 11:42:01.283: E/AndroidRuntime(771):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-12 11:42:01.283: E/AndroidRuntime(771):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-12 11:42:01.283: E/AndroidRuntime(771):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-12 11:42:01.283: E/AndroidRuntime(771):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-12 11:42:01.283: E/AndroidRuntime(771):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 11:42:01.283: E/AndroidRuntime(771):  at android.os.Looper.loop(Looper.java:137)
07-12 11:42:01.283: E/AndroidRuntime(771):  at android.app.ActivityThread.main(ActivityThread.java:4424)
07-12 11:42:01.283: E/AndroidRuntime(771):  at java.lang.reflect.Method.invokeNative(Native Method)
07-12 11:42:01.283: E/AndroidRuntime(771):  at java.lang.reflect.Method.invoke(Method.java:511)
07-12 11:42:01.283: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-12 11:42:01.283: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-12 11:42:01.283: E/AndroidRuntime(771):  at dalvik.system.NativeStart.main(Native Method)
07-12 11:42:01.283: E/AndroidRuntime(771): Caused by: java.lang.NullPointerException
07-12 11:42:01.283: E/AndroidRuntime(771):  at com.example.singletouch.SingleTouchView.<init>(SingleTouchView.java:19)
07-12 11:42:01.283: E/AndroidRuntime(771):  at com.example.singletouch.MainActivity.onCreate(MainActivity.java:12)
07-12 11:42:01.283: E/AndroidRuntime(771):  at android.app.Activity.performCreate(Activity.java:4465)
07-12 11:42:01.283: E/AndroidRuntime(771):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-12 11:42:01.283: E/AndroidRuntime(771):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-12 11:42:01.283: E/AndroidRuntime(771):  ... 11 more
07-12 11:42:01.283: I/jdwp(358): Ignoring second debugger -- accepting and dropping
07-12 11:42:01.312: I/jdwp(563): Ignoring second debugger -- accepting and dropping
07-12 11:42:01.312: W/ActivityManager(90):   Force finishing activity com.example.singletouch/.MainActivity
07-12 11:42:01.322: W/WindowManager(90): Failure taking screenshot for (180x300) to layer 21015
07-12 11:42:01.403: I/jdwp(385): Ignoring second debugger -- accepting and dropping
07-12 11:42:01.422: I/jdwp(298): Ignoring second debugger -- accepting and dropping
07-12 11:42:01.452: I/jdwp(210): Ignoring second debugger -- accepting and dropping
07-12 11:42:01.483: I/Process(90): Sending signal. PID: 771 SIG: 3
07-12 11:42:01.483: I/dalvikvm(771): threadid=3: reacting to signal 3
07-12 11:42:01.502: I/dalvikvm(771): Wrote stack traces to '/data/anr/traces.txt'
07-12 11:42:01.572: I/jdwp(689): Ignoring second debugger -- accepting and dropping
07-12 11:42:01.842: W/ActivityManager(90): Activity pause timeout for ActivityRecord{41451ca8 com.example.singletouch/.MainActivity}

Upvotes: 0

Views: 125

Answers (4)

harikrishnan
harikrishnan

Reputation: 1945

your code is edited. now you can run use this program.. its working perfect now..

import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Switch;

 public class SingleTouchView extends View{
private Paint paint = new Paint();
  private Path path = new Path();

public SingleTouchView(Context context, AttributeSet attrs) {
    super(context, attrs);

    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
 }

 public void onDraw(Canvas canvas){
    canvas.drawPath(path,paint);
    }
public boolean onTouchEvent(MotionEvent me){
    float eventX=me.getX();
    float eventY=me.getY();

    switch (me.getAction()) {
    case MotionEvent.ACTION_DOWN:
        path.moveTo(eventX, eventY);
        return true;
    case MotionEvent.ACTION_MOVE:
        path.lineTo(eventX, eventY);
        break;
    case MotionEvent.ACTION_UP:
        break;
    }
    invalidate();
    return true;

}

}

Upvotes: 2

Mukesh Kumar Singh
Mukesh Kumar Singh

Reputation: 4522

You have not initialized paint and path references in your code. Initialized it with object and then use it. i.e.

private Paint paint;
private Path path;
public SingleTouchView(Context context, AttributeSet attrs) {
    super(context, attrs);
    paint= new Paint();
    path= new Path(); 
    paint.setAntiAlias(true);
    paint.setStrokeWidth(3f);
    paint.setColor(color.black);
    paint.setStrokeJoin(Paint.Join.ROUND);

} 

Upvotes: 1

MSA
MSA

Reputation: 2482

You can get the on touch event and see if Action down, Move or Action Up and other actions but for the moment let us stop here. I have a simple example that I think you or anyone else will find it usefull.

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends Activity {


private boolean isTouch = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}


@Override
public boolean onTouchEvent(MotionEvent event) {

int X = (int) event.getX();
int Y = (int) event.getY();

int eventaction = event.getAction();

switch (eventaction) {

case MotionEvent.ACTION_DOWN:

    Toast.makeText(this, "ACTION_DOWN AT COORDS "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();

    isTouch = true;
    break;

case MotionEvent.ACTION_MOVE:

    Toast.makeText(this, "MOVE "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();

    break;

case MotionEvent.ACTION_UP:

    Toast.makeText(this, "ACTION_UP "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();

    break;

}

return true;

}

}

Upvotes: 1

gulati
gulati

Reputation: 382

You are never allocating paint, and path objects, they are null and hence you get the NullPointerException.

Upvotes: 1

Related Questions