学習目的で単純な 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}