如何查找NullPointerException / RuntimeException的根本原因

时间:2014-09-10 19:57:21

标签: java android debugging google-play-games shiva3d

Google Play游戏服务在我退出并重新登入时使用GameHelper会导致我的游戏崩溃。我已经浏览过每一行代码而我找不到任何问题(当我没有使用Google游戏服务时没有崩溃)。

但是当我运行我的游戏时,使用BaseGameUtils库,并使用GameHelper注销,当我在Android应用程序中重新登录时,每次都会出现此错误:

我已经对此次崩溃进行了3天的故障排除,我完全失败了。任何有关如何排除故障或调试此错误的建议都表示赞赏。

09-10 13:51:03.419: E/AndroidRuntime(9463): FATAL EXCEPTION: main
09-10 13:51:03.419: E/AndroidRuntime(9463): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nurfacegames.testgame07/com.nurfacegames.testgame07.TestGame07}: java.lang.NullPointerException
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread.access$1500(ActivityThread.java:124)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.os.Looper.loop(Looper.java:130)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread.main(ActivityThread.java:3806)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at java.lang.reflect.Method.invoke(Method.java:507)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at dalvik.system.NativeStart.main(Native Method)
09-10 13:51:03.419: E/AndroidRuntime(9463): Caused by: java.lang.NullPointerException
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.nurfacegames.testgame07.MainMenuFragment.updateUi(MainMenuFragment.java:61)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.nurfacegames.testgame07.MainMenuFragment.onStart(MainMenuFragment.java:48)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1866)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:568)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.google.example.games.basegameutils.BaseGameActivity.onStart(BaseGameActivity.java:110)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.nurfacegames.testgame07.TestGame07.onStart(TestGame07.java:522)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.Activity.performStart(Activity.java:3871)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1669)

以下是MainMenuFragment.class(来自Google):

public class MainMenuFragment extends Fragment implements OnClickListener {

    String mGreeting = "Hello, anonymous user (not signed in)";

    public interface Listener {
        public void onSignInButtonClicked();
        public void onSignOutButtonClicked();
    }

    Listener mListener = null;
    boolean mShowSignIn = true;
    boolean mShowScreenMenu = true;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mainmenu, container, false);
        final int[] CLICKABLES = new int[] {
                R.id.sign_in_button, R.id.sign_out_button
        };
        for (int i : CLICKABLES) {
            v.findViewById(i).setOnClickListener(this);
        }
        return v;
    }

    public void setListener(Listener l) {
        mListener = l;
    }

    @Override
    public void onStart() {
        super.onStart();
        updateUi();
    }

    public void setGreeting(String greeting) {
        mGreeting = greeting;
        updateUi();
    }

    void updateUi() {
        if (getActivity() == null) return;
        TextView tv = (TextView) getActivity().findViewById(R.id.hello);
        if (tv != null) tv.setText(mGreeting);

        getActivity().findViewById(R.id.sign_in_bar).setVisibility(mShowSignIn ?
                View.VISIBLE : View.GONE);
        getActivity().findViewById(R.id.sign_out_bar).setVisibility(mShowSignIn ?
                View.GONE : View.VISIBLE);
        getActivity().findViewById(R.id.screen_menu).setVisibility(mShowScreenMenu ?
                View.VISIBLE : View.GONE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.sign_in_button:
            mListener.onSignInButtonClicked();
            break;
        case R.id.sign_out_button:
            mListener.onSignOutButtonClicked();
            break;
        }
    }

这是TestGame07.java第522行(super.onStart):

//------------------------------------------------------------------
// @@BEGIN_ACTIVITY_METHODS@@   
//------------------------------------------------------------------
@Override
protected void onStart ( )
{
    Log.d ( Globals.sApplicationName, "--------------------------------------------" ) ;
    Log.d ( Globals.sApplicationName, "Start activity " + Globals.sApplicationName ) ;
    Log.d ( Globals.sApplicationName, "--------------------------------------------" ) ;
    super.onStart ( ) ;
}

2 个答案:

答案 0 :(得分:0)

3个视图中的一个(R.id.sign_in_bar或R.id.sign_out_bar或R.id.screen_menu,我看不到行号,所以我无法分辨哪一个)未找到,所以其中一个setVisibility()方法引用了null对象,它会生成NullPointException错误。

答案 1 :(得分:0)

在此程序中,由于updateUi方法

导致NullPointerException
  void updateUi() {

    if (getActivity() == null) return;
    TextView tv = (TextView) getActivity().findViewById(R.id.hello);
    if (tv != null) tv.setText(mGreeting);

    getActivity().findViewById(R.id.sign_in_bar).setVisibility(mShowSignIn ?
            View.VISIBLE : View.GONE);
    getActivity().findViewById(R.id.sign_out_bar).setVisibility(mShowSignIn ?
            View.GONE : View.VISIBLE);
    getActivity().findViewById(R.id.screen_menu).setVisibility(mShowScreenMenu ?
            View.VISIBLE : View.GONE);
}

当任何字段或变量持有空值并且我们尝试在代码中使用它们时,总会引发空指针异常