0

我正在做一个应用程序,它将显示 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)
4

1 回答 1

1

我的假设,

你对这些行有错误,

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

当你setContentView(demoview1)
然后你的 demoVIewonDraw()将首先被调用并且仍然onTouch()不是,那么你还没有一个值String n1, n2..两者都是空的,所以它会给你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 = "";

于 2012-05-07T06:20:28.877 回答