在android中的ontouchevent

时间:2012-05-07 06:15:15

标签: android ontouchevent ontouchlistener

我正在做一个应用程序,它将显示ontouch的位置。 我的代码如下,我编辑了我的代码。 公共类AndroidTabLayoutAcActivity扩展了Activity {

DemoView demoview1 ;
DemoView2 demoview2;
DemoView3 demoview3;
DemoView4 demoview4;
LowerView lowerview;

float d,b;

//ViewFlipper c;
//ViewFlipper a;
String text="Oxygen";
String text1="Carbon";
String text2="Phosphate";
String text3="Hydrogen";

//Called when the activity is first created. 
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            demoview1 = new DemoView(this);
             setContentView(demoview1);
              demoview1.requestFocus();

}

public class DemoView extends View implements OnTouchListener{
    String n1="";
            String n2="";
            Canvas canvas;
    public DemoView(Context context){
        super(context);
        setOnTouchListener(this);

    }
     public boolean onTouch(View view, MotionEvent event) {
            // if(event.getAction() != MotionEvent.ACTION_DOWN)
            // return super.onTouchEvent(event);

            d = event.getX();
           b = event.getY();

          n1=Float.toString(d);
          n2=Float.toString(b);
              onDraw(canvas); 
            return true;
        }

    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int x = 150;
        int y = 100;
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);

        // make the entire canvas white
        paint.setColor(Color.CYAN);
        canvas.drawPaint(paint);
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        canvas.drawCircle(x-24, y+30, 11, paint);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(x+11, y-11, 7, paint);
        canvas.drawCircle(x+6, y+48, 7, paint);
        canvas.drawCircle(x+21, y+43, 7, paint);
        //carbon molecule
        paint.setColor(Color.BLACK);
        canvas.drawCircle(x, y, 11, paint);
        canvas.drawCircle(x-11, y+19, 11, paint);
        canvas.drawCircle(x, y+38, 11, paint);
        canvas.drawCircle(x+20, y+30, 11, paint);
        canvas.drawCircle(x-11, y-19, 11, paint);

        //hydrogen molecule
        paint.setColor(Color.WHITE);
        canvas.drawCircle(x-25, y-12, 7, paint);
        canvas.drawCircle(x+2, y-27, 7, paint);
        canvas.drawCircle(x-20, y+9, 7, paint);
        canvas.drawCircle(x-10, y+45, 7, paint);
        canvas.drawCircle(x-34, y+40, 7, paint);
        canvas.drawCircle(x+51, y+18, 7, paint);
        canvas.drawCircle(x-33, y-52, 7, paint);

        //oxygen molecule
        paint.setColor(Color.RED);
        canvas.drawCircle(x+41, y+30, 11, paint);
        canvas.drawCircle(x+20, y+9, 11, paint);
        canvas.drawCircle(x-23, y-37, 11, paint);
        canvas.drawText(n1, x+100, y+100, paint);
        canvas.drawText(n2, x+100, y+100, paint);

    }

}

}

我的日志cat文件显示以下错误但我没有得到它显示以下错误的原因..

    05-07 12:26:31.354: E/AndroidRuntime(385): FATAL EXCEPTION: main
05-07 12:26:31.354: E/AndroidRuntime(385): java.lang.NullPointerException
05-07 12:26:31.354: E/AndroidRuntime(385):  at com.example.an.AndroidTabLayoutAcActivity$DemoView.onDraw(AndroidTabLayoutAcActivity.java:150)
05-07 12:26:31.354: E/AndroidRuntime(385):  at com.example.an.AndroidTabLayoutAcActivity$DemoView.onTouch(AndroidTabLayoutAcActivity.java:136)
05-07 12:26:31.354: E/AndroidRuntime(385):  at android.view.View.dispatchTouchEvent(View.java:3762)
05-07 12:26:31.354: E/AndroidRuntime(385):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
05-07 12:26:31.354: E/AndroidRuntime(385):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
05-07 12:26:31.354: E/AndroidRuntime(385):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
05-07 12:26:31.354: E/AndroidRuntime(385):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-07 12:26:31.354: E/AndroidRuntime(385):  at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
05-07 12:26:31.354: E/AndroidRuntime(385):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
05-07 12:26:31.354: E/AndroidRuntime(385):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
05-07 12:26:31.354: E/AndroidRuntime(385):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 12:26:31.354: E/AndroidRuntime(385):  at android.os.Looper.loop(Looper.java:123)
05-07 12:26:31.354: E/AndroidRuntime(385):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-07 12:26:31.354: E/AndroidRuntime(385):  at java.lang.reflect.Method.invokeNative(Native Method)
05-07 12:26:31.354: E/AndroidRuntime(385):  at java.lang.reflect.Method.invoke(Method.java:521)
05-07 12:26:31.354: E/AndroidRuntime(385):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-07 12:26:31.354: E/AndroidRuntime(385):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-07 12:26:31.354: E/AndroidRuntime(385):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我的假设,

这些行有错误,

canvas.drawText(n1, x+100, y+100, paint);
canvas.drawText(n2, x+100, y+100, paint);

当你setContentView(demoview1)时 那么你的demoVIew onDraw()将首先被调用,而onTouch()仍然没有, 那么你没有String n1, n2的值..两者都是null所以它给你 NullPointerException

所以试试,

String n1 = "", n2="";
        public DemoView(Context context){
            super(context);
            setOnTouchListener(this);
        }
      public boolean onTouch(View view, MotionEvent event) {
         // if(event.getAction() != MotionEvent.ACTION_DOWN)
         // return super.onTouchEvent(event);

         d = event.getX();
         b = event.getY();

         n1 = d +"";
         n2 = b +"";
          return true;
   }

实际上我认为你必须初始化String n1 = "", n2 = "";