android中的Touchevent程序无法正常工作

时间:2013-07-12 06:10:18

标签: android touch ontouchevent android-gesture

我已经制作了一个简单的singleTouch程序用于学习目的,我已经尝试了fllowing代码,但它不工作我没有得到究竟问题是为什么我的程序不runnig ..请帮助我。我的代码是下面:

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}

4 个答案:

答案 0 :(得分:2)

您的代码已被编辑。现在你可以运行使用这个程序..它的工作现在完美..

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;

}

}

答案 1 :(得分:1)

你永远不会分配绘画和路径对象,它们是null,因此你得到NullPointerException。

答案 2 :(得分:1)

你可以获得触摸事件并查看Action down,Move或Action Up以及其他动作,但暂时让我们停在这里。我有一个简单的例子,我认为你或其他任何人都会觉得它很有用。

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;

}

}

答案 3 :(得分:1)

您尚未在代码中初始化paintpath个引用。用对象初始化它然后使用它。即。

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

}