0

我为学习目的制作了一个简单的 singleTouch 程序,我尝试了以下代码,但它不起作用我不知道究竟是什么问题是为什么我的程序没有运行..请帮助我。我的代码如下:

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

}

日志猫

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

4 回答 4

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;

}

}
于 2013-07-12T06:47:02.923 回答
1

您可以获取 on touch 事件并查看是否有 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;

}

}
于 2013-07-12T06:37:11.777 回答
1

您尚未在代码中进行初始化paintpath引用。用对象初始化它,然后使用它。IE

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

} 
于 2013-07-12T06:38:50.550 回答
1

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

于 2013-07-12T06:34:18.680 回答