我一直在关注 youtube 上的 Surfaceview 教程,其中包含一个线程。
我已经完全复制了教程中代码显示的内容,但是当我运行它时它会在它应该工作时崩溃。这是我代码中的表面类:
它在 resume() 处显示空指针期望;
public class dfgfActivity extends Activity implements OnTouchListener
{
OurView v;
protected void OnCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = new OurView(this);
v.setOnTouchListener(this);
setContentView(v);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable
{
Thread t=null;
SurfaceHolder holder;
boolean isItOK=false;
public OurView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
public void run() {
// TODO Auto-generated method stub
while (isItOK) {
if (!holder.getSurface().isValid()) {
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(255, 255, 10, 200);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
isItOK = false;
while (true) {
try {
t.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
07-02 18:55:48.585: D/AndroidRuntime(684): Shutting down VM
07-02 18:55:48.595: W/dalvikvm(684): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
07-02 18:55:48.595: E/AndroidRuntime(684): Uncaught handler: thread main exiting due to uncaught exception
07-02 18:55:48.665: E/AndroidRuntime(684): java.lang.RuntimeException: Unable to resume activity {fdgfd.dfgfdg/fdgfd.dfgfdg.dfgfActivity}: java.lang.NullPointerException
07-02 18:55:48.665: E/AndroidRuntime(684): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2950)
07-02 18:55:48.665: E/AndroidRuntime(684): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2965)
07-02 18:55:48.665: E/AndroidRuntime(684): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2516)
07-02 18:55:48.665: E/AndroidRuntime(684): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-02 18:55:48.665: E/AndroidRuntime(684): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-02 18:55:48.665: E/AndroidRuntime(684): at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 18:55:48.665: E/AndroidRuntime(684): at android.os.Looper.loop(Looper.java:123)
07-02 18:55:48.665: E/AndroidRuntime(684): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-02 18:55:48.665: E/AndroidRuntime(684): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 18:55:48.665: E/AndroidRuntime(684): at java.lang.reflect.Method.invoke(Method.java:521)
07-02 18:55:48.665: E/AndroidRuntime(684): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-02 18:55:48.665: E/AndroidRuntime(684): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-02 18:55:48.665: E/AndroidRuntime(684): at dalvik.system.NativeStart.main(Native Method)
07-02 18:55:48.665: E/AndroidRuntime(684): Caused by: java.lang.NullPointerException
07-02 18:55:48.665: E/AndroidRuntime(684): at fdgfd.dfgfdg.dfgfActivity.onResume(dfgfActivity.java:42)
07-02 18:55:48.665: E/AndroidRuntime(684): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
07-02 18:55:48.665: E/AndroidRuntime(684): at android.app.Activity.performResume(Activity.java:3763)
07-02 18:55:48.665: E/AndroidRuntime(684): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2937)
07-02 18:55:48.665: E/AndroidRuntime(684): ... 12 more
07-02 18:55:48.716: I/dalvikvm(684): threadid=7: reacting to signal 3
07-02 18:55:48.716: E/dalvikvm(684): Unable to open stack trace file '/data/anr/traces.txt': Permission denied