我找不到这个NullPointerException

时间:2014-12-07 18:20:57

标签: java android nullpointerexception

我有一个需要帮助的问题。无论我使用这个应用程序做什么,我都遇到了NullPointerException,而且此时它正在严重阻止这个项目。我已经多次查看了logcat,进行了更改并重新运行了应用程序,每次结果都是nullpointerexception。对于我的生活,我似乎无法弄清楚造成它的原因。有人可以指出它在哪里,如果可能的话,为什么会发生这种情况,因为据我所知,我已经为所有事物分配了一个值。

GameStart.java:

package com.saphiric.simproject;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Saphiric on 11/12/14.
 */
public class GameStart extends Activity {

    /**
     * Necessary activity variables
     */
    int clickCount = 0;
    TextView storyText;
    Button advanceGame;
    DecisionFragment decisionFragment;
    ControlsFragment controlsFragment;

    public View.OnClickListener decisionTime = new View.OnClickListener(){
        public void onClick(View v) {
            clickCount = clickCount + 1; // Increments clickCount by 1 per click

//            /**
//             * Will handle checking how far into the story the player is
//             * and update components as necessary.
//             */
//            switch (clickCount) {
//                case 1:
//                    storyText.setText(R.string.story_opening_two);
//                    break;
//
//                case 2:
//                    FragmentTransaction decisionTime = fragmentManager.beginTransaction();
//                    // Replace the fragment currently inside of the fragmentContainer
//                    decisionTime.replace(R.id.fragmentContainer, decisionFragment);
//                    decisionTime.commit();
//                    break;
//
//                default:
//                    System.out.println("An error occurred");
//            }
//            System.out.println(clickCount);
        }
    };

    /**
     * Android activity methods
     * @param savedInstanceState is the apps savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_start);

        // UI element handles
        advanceGame = (Button) findViewById(R.id.advanceButton);
        storyText = (TextView) findViewById(R.id.storyText);

        decisionFragment = new DecisionFragment();
        controlsFragment = new ControlsFragment();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        // UI will add the ControlsFragment in it's starting state for that activity.
        fragmentTransaction.add(R.id.fragmentContainer, controlsFragment).commit();

        // Sets the advanceButton to run decisionTime method
        advanceGame.setOnClickListener(decisionTime);
    }
}

LogCat:

12-07 13:16:10.142  19915-19915/com.saphiric.simproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.saphiric.simproject, PID: 19915
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.saphiric.simproject/com.saphiric.simproject.GameStart}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5872)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.saphiric.simproject.GameStart.onCreate(GameStart.java:75)
            at android.app.Activity.performCreate(Activity.java:5312)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5872)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
            at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:3)

空指针发生在第75行,即

advanceGame.setOnClickListener(decisionTime);

在这里,唯一可以为null的是advanceGame。这意味着

advanceGame = (Button) findViewById(R.id.advanceButton);

无法从您的视图中找到您的按钮。检查按钮的ID是否与您在android xml中定义的完全相同。

答案 1 :(得分:1)

我认为advanceGamenull

您无法按findViewById(R.id.advanceButton);

找到按钮

答案 2 :(得分:0)

NPE在第75行

advanceGame.setOnClickListener(decisionTime);

很明显

at com.saphiric.simproject.GameStart.onCreate(GameStart.java:75)

看起来像

advanceGame = (Button) findViewById(R.id.advanceButton);
找不到视图。您可以在75

调试或添加检查
if(null != advanceGame) advanceGame = (Button) findViewById(R.id.advanceButton);

答案 3 :(得分:0)

我想只需检查您的activityGame按钮的activity_game_start.xml布局的@ + id 它可能有不同的名称