Android:线程无法运行:/

时间:2012-07-02 18:39:14

标签: android nullpointerexception surfaceview runnable resume

我一直在youtube上关注faceview的教程,其中包含一个帖子。

我已经完全复制了代码在教程中显示的内容,但是当我运行它时崩溃它应该工作.. 这是我的代码中的表面类:

它表示在resume()的nullpointerexpection;

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

1 个答案:

答案 0 :(得分:0)

在您的方法onCreate()中,该方法使用大写情况编写:OnCreate()。尝试将其更改为小案例。