片段中setOnClickListener上的java

片段中setOnClickListener上的java

本文介绍了片段中setOnClickListener上的java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Android还是很陌生,我正在尝试创建一个可以在Vex机器人大赛中得分的应用程序.我有一个 ViewPager ,每个团队都有片段,我有很多按钮可以增加和减少 EditTexts 中的数字.我为每一个创建了一个 OnClickListener .在我引入 SharedPreferences 将值携带到计算分数的活动中之前,这段代码已经起作用.

I'm pretty new to Android and I'm trying to create an application that will score matches of the Vex Robotics Competition. I have a ViewPager with fragments for each team and i have a lot of buttons that increase and decrease numbers in EditTexts. I created an OnClickListener for each one. This code was working before I introduced SharedPreferences to carry values to the activity that calculates the score.

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

Fragment Code (sorry, there's a lot of it):

    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)

推荐答案

打开您的 fragment_blue.xml 布局.您需要在此文件中声明ID为 rAFloorGoalUp Button .

Open your fragment_blue.xml layout. Your Button with id rAFloorGoalUp needs to be declared in this file.

不是的时候

Button rAFloorGoalUp = (Button) rootView.findViewById(R.id.rAFloorGoalUp);

将被分配为 null

rAFloorGoalUp.setOnClickListener(...)

将引发 NullPointerException 错误.

这篇关于片段中setOnClickListener上的java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:08