NullPointerException(GameActivity无法打开)

时间:2016-06-23 20:52:11

标签: java android android-studio android-activity nullpointerexception

请查看我的代码中的问题:

主要活动:

我正在使用Android Studio。导入语句是为我管理的。在这里写它们不是必要的

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);
}
}

当我运行此应用程序时,主活动已加载,但是当我点击播放按钮应用程序关闭时。我已经检查了播放按钮的ID是O.K.我在GameActivity遇到了同样的问题,但最后我发现问题出在ID上,但这次我认为问题出在我this中使用的Intent();上。活动无法加载。

例外:

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) 

请帮帮我!我花了太多时间仍无法解决问题。

1 个答案:

答案 0 :(得分:2)

从您的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)

最后一行是跟踪中唯一的代码行:

查看GameActivity.java的第55行。它正在调用loadAnimation。它正在尝试加载的动画有问题(可能是缺少的资源ID,但它也可能是loadAnimation的null或无效的上下文参数。)