質問

I have this error and i don't know why!

04-28 09:26:55.935: E/AndroidRuntime(1437): FATAL EXCEPTION: main

04-28 09:26:55.935: E/AndroidRuntime(1437): Process: com.neurondigital.HighwaySpeed, PID: 1437

04-28 09:26:55.935: E/AndroidRuntime(1437): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.neurondigital.HighwaySpeed/com.neurondigital.HighwaySpeed.HighwaySpeed}: java.lang.NullPointerException

04-28 09:26:55.935: E/AndroidRuntime(1437): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

04-28 09:26:55.935: E/AndroidRuntime(1437): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

04-28 09:26:55.935: E/AndroidRuntime(1437): at android.app.ActivityThread.access$800(ActivityThread.java:135)

04-28 09:26:55.935: E/AndroidRuntime(1437): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

04-28 09:26:55.935: E/AndroidRuntime(1437): at android.os.Handler.dispatchMessage(Handler.java:102)

04-28 09:26:55.935: E/AndroidRuntime(1437): at android.os.Looper.loop(Looper.java:136)

04-28 09:26:55.935: E/AndroidRuntime(1437): at android.app.ActivityThread.main(ActivityThread.java:5017)

04-28 09:26:55.935: E/AndroidRuntime(1437): at java.lang.reflect.Method.invokeNative(Native Method)

04-28 09:26:55.935: E/AndroidRuntime(1437): at java.lang.reflect.Method.invoke(Method.java:515)

04-28 09:26:55.935: E/AndroidRuntime(1437): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

04-28 09:26:55.935: E/AndroidRuntime(1437): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

04-28 09:26:55.935: E/AndroidRuntime(1437): at dalvik.system.NativeStart.main(Native Method)

04-28 09:26:55.935: E/AndroidRuntime(1437): Caused by: java.lang.NullPointerException

04-28 09:26:55.935: E/AndroidRuntime(1437): at com.neurondigital.HighwaySpeed.HighwaySpeed.onCreate(HighwaySpeed.java:40)

04-28 09:26:55.935: E/AndroidRuntime(1437): at android.app.Activity.performCreate(Activity.java:5231)

04-28 09:26:55.935: E/AndroidRuntime(1437): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

04-28 09:26:55.935: E/AndroidRuntime(1437): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

04-28 09:26:55.935: E/AndroidRuntime(1437): ... 11 more

This is the code:

import android.app.Activity;

import android.content.Context;

import android.content.pm.ActivityInfo;

import android.os.Bundle;

import android.os.PowerManager;

import android.os.PowerManager.WakeLock;

import android.view.Display;

import android.view.KeyEvent;

import android.view.Window;

import android.view.WindowManager;

import android.widget.RelativeLayout;

import android.widget.RelativeLayout.LayoutParams;

public class HighwaySpeed extends Activity {

    Surface view;
    WakeLock WL;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //wake-lock
        PowerManager PM = (PowerManager)getSystemService(Context.POWER_SERVICE);
        WL = PM.newWakeLock(PowerManager.FULL_WAKE_LOCK, "Graphics");
        WL.acquire();

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int rotation = display.getRotation();
        if(rotation == 0)
            view.default_lanscape = true;
        if(rotation == 180)
            view.default_lanscape = true;
        if(rotation == 90)
            view.default_lanscape = false;
        if(rotation == 270)
            view.default_lanscape = false;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);
        if(keyCode == KeyEvent.KEYCODE_BACK){
            view.back();
            return false;
        }

        return false;
    }

    protected void onPause(){
        super.onPause();
        view.pause();
        WL.release();
    }

    protected void onResume(){
        super.onResume();
        view.resume();
        WL.acquire();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WL.release();
    }
}
役に立ちましたか?

解決

At HighwaySpeed.java:40 there's a NullPointerException

If you attach the code it's easy to resolve the problem.

EDIT: You have to write view = new SurfaceView(this); at the beginin of onCreate method to avoid the NullPointerException.

The main problem is that you don't initialize the variable, but there is other errors in the code, for example the class Surface hasn't got a default_lanscape attribute and you're triying to initialize it in view.default_lanscape = true;

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top