2013-07-12 156 views
0

我做了一個簡單的singleTouch程序的學習目的,我已經嘗試了fllowing的代碼,但它不工作我不明白爲什麼我的程序不runnig ..請幫助me.my代碼是如下:Touchevent程序在Android不工作

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} 
+0

你是什麼意思不工作?它是否給出了一些錯誤,崩潰或什麼...? –

+0

@ MukeshKumar-yes..it崩潰....等待我添加我的logcat ..!請看看它,所以你可以幫助我。 – jigar

+0

@ MukeshKumar-我把我的logcat。 – jigar

回答

1

您可以獲得聯繫事件,並查看是否執行動作,動作或動作以及其他動作,但暫時讓我們停下來。我有一個簡單的例子,我認爲你或其他人會發現它有用。

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; 

} 

} 
1

你永遠分配油漆和路徑的對象,它們是空的,所以你得到的NullPointerException異常。

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

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

} 

} 
+0

Thabks ... frend ..! – jigar