片段中的setOnClickListener上的java.lang.NullPointerException

时间:2014-05-31 05:09:08

标签: java android nullpointerexception fragment sharedpreferences

我对Android很陌生,我正在尝试创建一个能够与Vex机器人大赛相匹配的应用程序。我有一个ViewPager每个团队都有片段,我有很多按钮可以增加和减少EditTexts中的数字。我为每个人创建了一个OnClickListener。在我引入SharedPreferences以将值传递给计算得分的活动之前,此代码正在运行。

片段代码(对不起,还有很多):

    package com.bearsandthings.vexskyrisescorer;

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;

    public class BlueFragment extends Fragment {
        public static int bafloorgoal, bapost, bapostowned, baskyrisebuilt, baskyrisecubes;
        public static int bdfloorgoal, bdpost, bdpostowned, bdskyrisebuilt, bdskyrisecubes;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.fragment_blue, container, false);


            //AUTONOMOUS
            bafloorgoal = 0;
            bapost = 0;
            bapostowned = 0;
            baskyrisebuilt = 0;
            baskyrisecubes = 0;
            bdfloorgoal = 0;
            bdpost = 0;
            bdpostowned = 0;
            bdskyrisebuilt = 0;
            bdskyrisecubes = 0;

            final SharedPreferences prefs = this.getActivity().getSharedPreferences("com.bearsandthings.vexskyrisescorer", Context.MODE_PRIVATE);
            prefs.edit().putInt("bafloorgoal", 0).apply();
            prefs.edit().putInt("bapost", 0).apply();
            prefs.edit().putInt("bapostowned", 0).apply();
            prefs.edit().putInt("baskyrisebuilt", 0).apply();
            prefs.edit().putInt("baskyrisecubes", 0).apply();
            prefs.edit().putInt("bdfloorgoal", 0).apply();
            prefs.edit().putInt("bdpost", 0).apply();
            prefs.edit().putInt("bdpostowned", 0).apply();
            prefs.edit().putInt("bdskyrisebuilt", 0).apply();
            prefs.edit().putInt("bdskyrisecubes", 0).apply();

            Button rAFloorGoalUp = (Button) rootView.findViewById(R.id.rAFloorGoalUp);
            rAFloorGoalUp.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText rAFloorGoal = (EditText) rootView.findViewById(R.id.rAFloorGoal);
                    int testnum = Integer.valueOf(rAFloorGoal.getText().toString());
                    if (testnum < 22)
                    {
                        testnum += 1;
                        bafloorgoal = testnum;
                        prefs.edit().putInt("bafloorgoal",testnum).apply();

                        rAFloorGoal.setText(String.valueOf(testnum));
                    }
                }
            });
            Button rAFloorGoalDown = (Button) rootView.findViewById(R.id.rAFloorGoalDn);
            rAFloorGoalDown.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {
                    EditText rAFloorGoal = (EditText) rootView.findViewById(R.id.rAFloorGoal);
                    int testnum = Integer.parseInt(rAFloorGoal.getText().toString());
                    if (testnum > 0)
                    {
                        testnum -=1;
                        bafloorgoal = testnum;
                        prefs.edit().putInt("bafloorgoal",testnum).apply();
                        rAFloorGoal.setText(String.valueOf(testnum));
                    }
                }
            });

            Button rAPostUp = (Button) rootView.findViewById(R.id.rAPostUp);
            rAPostUp.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText rAPost = (EditText) rootView.findViewById(R.id.rAPost);
                    int testnum = Integer.parseInt(rAPost.getText().toString());
                    if (testnum < 22)
                    {
                        testnum +=1;
                        bapost = testnum;
                        prefs.edit().putInt("bapost",testnum).apply();
                        rAPost.setText(String.valueOf(testnum));
                    }

                }

            });

            Button rAPostDn = (Button) rootView.findViewById(R.id.rAPostDn);
            rAPostDn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText rAPost = (EditText) rootView.findViewById(R.id.rAPost);
                    int testnum = Integer.parseInt(rAPost.getText().toString());
                    if (testnum > 0)
                    {
                        testnum -=1;
                        bapost = testnum;
                        prefs.edit().putInt("bapost", testnum).apply();
                        rAPost.setText(String.valueOf(testnum));
                    }

                }

            });

            Button rAPostOwnedUp = (Button) rootView.findViewById(R.id.rAPostOwnedUp);
            rAPostOwnedUp.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText rAPostOwned = (EditText) rootView.findViewById(R.id.rAPostOwned);
                    int testnum = Integer.parseInt(rAPostOwned.getText().toString());
                    if (testnum < 10)
                    {
                        testnum +=1;
                        bapostowned = testnum;
                        prefs.edit().putInt("bapost", testnum).apply();
                        rAPostOwned.setText(String.valueOf(testnum));
                    }

                }

            });

            Button rAPostOwnedDn = (Button) rootView.findViewById(R.id.rAPostOwnedDn);
            rAPostOwnedDn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText rAPostOwned = (EditText) rootView.findViewById(R.id.rAPostOwned);
                    int testnum = Integer.parseInt(rAPostOwned.getText().toString());
                    if (testnum >0 )
                    {
                        testnum -=1;
                        bapostowned = testnum;
                        prefs.edit().putInt("bapostowned", testnum).apply();
                        rAPostOwned.setText(String.valueOf(testnum));
                    }

                }

            });

            Button rASkyriseBuiltUp = (Button) rootView.findViewById(R.id.rASkyriseBuiltUp);
            rASkyriseBuiltUp.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText rASkyriseBuilt = (EditText) rootView.findViewById(R.id.rASkyriseBuilt);
                    int testnum = Integer.parseInt(rASkyriseBuilt.getText().toString());
                    if (testnum < 7)
                    {
                        testnum += 1;
                        baskyrisebuilt = testnum;
                        prefs.edit().putInt("baskyrisebuilt", testnum).apply();
                        rASkyriseBuilt.setText(String.valueOf(testnum));
                    }
                }
            });

            Button rASkyriseBuiltDn = (Button) rootView.findViewById(R.id.rASkyriseBuiltDn);
            rASkyriseBuiltDn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText rASkyriseBuilt = (EditText) rootView.findViewById(R.id.rASkyriseBuilt);
                    int testnum = Integer.parseInt(rASkyriseBuilt.getText().toString());
                    if (testnum > 0)
                    {
                        testnum -= 1;
                        baskyrisebuilt = testnum;
                        prefs.edit().putInt("baskyrisebuilt", testnum).apply();
                        rASkyriseBuilt.setText(String.valueOf(testnum));
                    }
                }
            });
            Button rASkyriseCubesUp = (Button) rootView.findViewById(R.id.rASkyriseCubesUp);
            rASkyriseCubesUp.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText rASkyriseCubes = (EditText) rootView.findViewById(R.id.rASkyriseCubes);
                    int testnum = Integer.parseInt(rASkyriseCubes.getText().toString());
                    if (testnum < 22)
                    {
                        testnum +=1;
                        baskyrisecubes = testnum;
                        prefs.edit().putInt("baskyrisecubes", testnum).apply();
                        rASkyriseCubes.setText(String.valueOf(testnum));
                    }
                }
            });

            Button rASkyriseCubesDn = (Button) rootView.findViewById(R.id.rASkyriseCubesDn);
            rASkyriseCubesDn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText rASkyriseCubes = (EditText) rootView.findViewById(R.id.rASkyriseCubes);
                    int testnum = Integer.parseInt(rASkyriseCubes.getText().toString());
                    if (testnum > 0)
                    {
                        testnum -=1;
                        baskyrisecubes = testnum;
                        prefs.edit().putInt("baskyrisecubes", testnum).apply();
                        rASkyriseCubes.setText(String.valueOf(testnum));
                    }
                }
            });


            // DRIVER CONTROL

            Button rDFloorGoalUp = (Button) rootView.findViewById(R.id.rDFloorGoalUp);
            rDFloorGoalUp.setOnClickListener(new OnClickListener() {



                @Override
                public void onClick(View v) {
                    EditText rDFloorGoal = (EditText) rootView.findViewById(R.id.rDFloorGoal);
                    int testnum = Integer.valueOf(rDFloorGoal.getText().toString());
                    if (testnum < 22)
                    {
                        testnum += 1;
                        bdfloorgoal = testnum;
                        prefs.edit().putInt("bdfloorgoal", testnum).apply();
                        rDFloorGoal.setText(String.valueOf(testnum));
                    }
                }
            });
            Button rDFloorGoalDown = (Button) rootView.findViewById(R.id.rDFloorGoalDn);
            rDFloorGoalDown.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {
                    EditText rDFloorGoal = (EditText) rootView.findViewById(R.id.rDFloorGoal);
                    int testnum = Integer.parseInt(rDFloorGoal.getText().toString());
                    if (testnum > 0)
                    {
                        testnum -=1;
                        bdfloorgoal = testnum;
                        prefs.edit().putInt("bdfloorgoal", testnum).apply();
                        rDFloorGoal.setText(String.valueOf(testnum));
                    }
                }
            });

            Button rDPostUp = (Button) rootView.findViewById(R.id.rDPostUp);
            rDPostUp.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText rDPost = (EditText) rootView.findViewById(R.id.rDPost);
                    int testnum = Integer.parseInt(rDPost.getText().toString());
                    if (testnum < 22)
                    {
                        testnum +=1;
                        bdpost = testnum;
                        prefs.edit().putInt("bdpost", testnum).apply();
                        rDPost.setText(String.valueOf(testnum));
                    }

                }

            });

            Button rDPostDn = (Button) rootView.findViewById(R.id.rDPostDn);
            rDPostDn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText rDPost = (EditText) rootView.findViewById(R.id.rDPost);
                    int testnum = Integer.parseInt(rDPost.getText().toString());
                    if (testnum > 0)
                    {
                        testnum -=1;
                        bdpost = testnum;
                        prefs.edit().putInt("bdpost", testnum).apply();
                        rDPost.setText(String.valueOf(testnum));
                    }

                }

            });

            Button rDPostOwnedUp = (Button) rootView.findViewById(R.id.rDPostOwnedUp);
            rDPostOwnedUp.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText rDPostOwned = (EditText) rootView.findViewById(R.id.rDPostOwned);
                    int testnum = Integer.parseInt(rDPostOwned.getText().toString());
                    if (testnum < 10)
                    {
                        testnum +=1;
                        bdpostowned = testnum;
                        prefs.edit().putInt("bdpostowned", testnum).apply();
                        rDPostOwned.setText(String.valueOf(testnum));
                    }

                }

            });

            Button rDPostOwnedDn = (Button) rootView.findViewById(R.id.rDPostOwnedDn);
            rDPostOwnedDn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText rDPostOwned = (EditText) rootView.findViewById(R.id.rDPostOwned);
                    int testnum = Integer.parseInt(rDPostOwned.getText().toString());
                    if (testnum >0 )
                    {
                        testnum -=1;
                        bdpostowned = testnum;
                        prefs.edit().putInt("bdpostowned", testnum).apply();
                        rDPostOwned.setText(String.valueOf(testnum));
                    }

                }

            });

            Button rDSkyriseBuiltUp = (Button) rootView.findViewById(R.id.rDSkyriseBuiltUp);
            rDSkyriseBuiltUp.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText rDSkyriseBuilt = (EditText) rootView.findViewById(R.id.rDSkyriseBuilt);
                    int testnum = Integer.parseInt(rDSkyriseBuilt.getText().toString());
                    if (testnum < 7)
                    {
                        testnum += 1;
                        bdskyrisebuilt = testnum;
                        prefs.edit().putInt("bdskyrisebuilt", testnum).apply();
                        rDSkyriseBuilt.setText(String.valueOf(testnum));
                    }
                }
            });

            Button rDSkyriseBuiltDn = (Button) rootView.findViewById(R.id.rDSkyriseBuiltDn);
            rDSkyriseBuiltDn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText rDSkyriseBuilt = (EditText) rootView.findViewById(R.id.rDSkyriseBuilt);
                    int testnum = Integer.parseInt(rDSkyriseBuilt.getText().toString());
                    if (testnum > 0)
                    {
                        testnum -= 1;
                        bdskyrisebuilt = testnum;
                        prefs.edit().putInt("bdskyrisebuilt", testnum).apply();
                        rDSkyriseBuilt.setText(String.valueOf(testnum));
                    }
                }
            });
            Button rDSkyriseCubesUp = (Button) rootView.findViewById(R.id.rDSkyriseCubesUp);
            rDSkyriseCubesUp.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText rDSkyriseCubes = (EditText) rootView.findViewById(R.id.rDSkyriseCubes);
                    int testnum = Integer.parseInt(rDSkyriseCubes.getText().toString());
                    if (testnum < 22)
                    {
                        testnum +=1;
                        bdskyrisecubes = testnum;
                        prefs.edit().putInt("bdskyrisecubes", testnum).apply();
                        rDSkyriseCubes.setText(String.valueOf(testnum));
                    }
                }
            });

            Button rDSkyriseCubesDn = (Button) rootView.findViewById(R.id.rDSkyriseCubesDn);
            rDSkyriseCubesDn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText rDSkyriseCubes = (EditText) rootView.findViewById(R.id.rDSkyriseCubes);
                    int testnum = Integer.parseInt(rDSkyriseCubes.getText().toString());
                    if (testnum > 0)
                    {
                        testnum -=1;
                        bdskyrisecubes = testnum;
                        prefs.edit().putInt("bdskyrisecubes", testnum).apply();
                        rDSkyriseCubes.setText(String.valueOf(testnum));
                    }
                }
            });

            return rootView;
        }


    }

LogCat:

05-30 22:04:05.827: E/AndroidRuntime(16083): FATAL EXCEPTION: main
05-30 22:04:05.827: E/AndroidRuntime(16083): java.lang.NullPointerException
05-30 22:04:05.827: E/AndroidRuntime(16083):    at com.bearsandthings.vexskyrisescorer.BlueFragment.onCreateView(BlueFragment.java:48)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.view.View.measure(View.java:12729)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.view.View.measure(View.java:12729)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.view.View.measure(View.java:12729)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2098)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.view.View.measure(View.java:12729)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2453)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.os.Looper.loop(Looper.java:137)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at android.app.ActivityThread.main(ActivityThread.java:4429)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at java.lang.reflect.Method.invokeNative(Native Method)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at java.lang.reflect.Method.invoke(Method.java:511)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:3151)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:2918)
05-30 22:04:05.827: E/AndroidRuntime(16083):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

打开fragment_blue.xml布局。您需要在此文件中声明标识为Button的{​​{1}}。

当它不是

rAFloorGoalUp

将被分配Button rAFloorGoalUp = (Button) rootView.findViewById(R.id.rAFloorGoalUp);

null

会引发rAFloorGoalUp.setOnClickListener(...) 错误。