-1

Bitte überprüfen Sie, was ist das Problem mit meinem Code hier:Nullpointer (GameActivity öffnet nicht)

Hauptaktivität:

I Android Studio bin mit. Importanweisungen werden für mich verwaltet. Es wird nicht notwendig sein, sie

public class MainActivity extends Activity implements View.OnClickListener { 

SharedPreferences prefs; 
String dataName = "MyData"; 
String intName = "MyString"; 
int defaultInt = 0; 
public static int highScore; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button buttonPlay = (Button) findViewById(R.id.btnPlay); 
    TextView textHighScore = (TextView) findViewById(R.id.textHighScore); 

    buttonPlay.setOnClickListener(this); 
    prefs = getSharedPreferences(dataName, MODE_PRIVATE); 
    highScore = prefs.getInt(intName, defaultInt); 
    textHighScore.setText("High Score: " + highScore); 
} 

@Override 
public void onClick(View view) { 
    Intent intent; 
    intent = new Intent(this, GameActivity.class); 
    startActivity(intent); 
} 
} 

hier zu schreiben, wenn ich diese Anwendung Hauptaktivität laufen geladen wird, aber wenn ich schließt Play-Taste Anwendung getroffen. Ich habe überprüft, dass die ID der Play-Taste O.K ist. Ich hatte das gleiche Problem in GameActivity, aber schließlich fand ich heraus, dass das Problem mit der ID war, aber dieses Mal denke ich, dass Problem mit der this ist, die ich in Intent(); verwendete. Die Aktivität wird nicht geladen.

Ausnahme:

06-24 01:34:05.196 12826-12826/com.cruzerblade.memorygame E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.cruzerblade.memorygame, PID: 12826 
                     java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cruzerblade.memorygame/com.cruzerblade.memorygame.GameActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
                      at android.content.ContextWrapper.getResources(ContextWrapper.java:87) 
                      at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81) 
                      at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:75) 
                      at com.cruzerblade.memorygame.GameActivity.<init>(GameActivity.java:55) 
                      at java.lang.Class.newInstance(Native Method) 
                      at android.app.Instrumentation.newActivity(Instrumentation.java:1067) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                      at android.os.Handler.dispatchMessage(Handler.java:102)  
                      at android.os.Looper.loop(Looper.java:148)  
                      at android.app.ActivityThread.main(ActivityThread.java:5417)  
                      at java.lang.reflect.Method.invoke(Native Method)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

Bitte helfen Sie mir! Ich brauchte zu viel Zeit, bis ich das Problem nicht lösen konnte.

Antwort

2

Zitiert aus Ihrem logcat:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
    'android.content.res.Resources android.content.Context.getResources()' 
    on a null object reference 
    at android.content.ContextWrapper.getResources(ContextWrapper.java:87) 
    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81) 
    at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:75) 
    at com.cruzerblade.memorygame.GameActivity.<init>(GameActivity.java:55) 

Diese letzte Zeile ist die einzige Zeile in der Spur, die Ihr Code:

Blick auf Linie 55 von GameActivity.java. Es ruft loadAnimation auf. Es ist etwas falsch mit der Animation, die es versucht zu laden (wahrscheinlich eine fehlende Ressourcen-ID, aber es könnte auch ein Null oder ungültiges Kontext-Argument für loadAnimation sein.)

+0

Ich habe es endlich herausgefunden. Aber immer noch danke. –