未知的nullpointer错误?

时间:2014-03-03 14:14:53

标签: java android nullpointerexception logcat oncreate

我的onCreateView中有一个for循环用于我的一个片段。

for(int i = 0; i<digitalIOModeSwitchArray.length;i++){
            digitalIOModeSwitchArray[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub

                }
            });
        }

产生以下logcat错误:

    03-03 08:08:50.236: E/AndroidRuntime(7350): FATAL EXCEPTION: main
03-03 08:08:50.236: E/AndroidRuntime(7350): java.lang.NullPointerException
03-03 08:08:50.236: E/AndroidRuntime(7350):     at com.example.ioiorun.digitalFragment.onCreateView(digitalFragment.java:231)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.View.measure(View.java:15264)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.View.measure(View.java:15264)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.View.measure(View.java:15264)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2163)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.View.measure(View.java:15264)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2131)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1242)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1435)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1127)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4650)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.Choreographer.doCallbacks(Choreographer.java:567)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.Choreographer.doFrame(Choreographer.java:536)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.os.Handler.handleCallback(Handler.java:615)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.os.Looper.loop(Looper.java:153)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at android.app.ActivityThread.main(ActivityThread.java:4987)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at java.lang.reflect.Method.invoke(Method.java:511)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-03 08:08:50.236: E/AndroidRuntime(7350):     at dalvik.system.NativeStart.main(Native Method)
03-03 08:10:00.758: E/Trace(7429): error opening trace file: No such file or directory (2)

我的digitalIOModeSwitchArray只是一个包含许多Switch视图的数组。

我不确定为什么这个for循环会导致应用程序立即崩溃;当我注释掉循环时,应用程序工作得很好,花花公子。 fragmentactivity只是一个带有标签的活动,可以切换一页片段。

如何实例化数组:

Switch[] digitalIOModeSwitchArray = { digitalIO0Mode, digitalIO1Mode,
        digitalIO2Mode, digitalIO3Mode, digitalIO4Mode, digitalIO5Mode,
        digitalIO6Mode, digitalIO7Mode, digitalIO8Mode, digitalIO9Mode };

文件的开头:

public class digitalFragment extends Fragment {
// Declare all the UI Variables
TableLayout centralDigitalTable;
View digitalfragmentview;

Switch digitalIO0Mode;
Switch digitalIO1Mode;
Switch digitalIO2Mode;
Switch digitalIO3Mode;
Switch digitalIO4Mode;
Switch digitalIO5Mode;
Switch digitalIO6Mode;
Switch digitalIO7Mode;
Switch digitalIO8Mode;
Switch digitalIO9Mode;

以下是整个代码块:

public class digitalFragment extends Fragment {
    // Declare all the UI Variables
    TableLayout centralDigitalTable;
    View digitalfragmentview;

    Switch digitalIO0Mode;
    Switch digitalIO1Mode;
    Switch digitalIO2Mode;
    Switch digitalIO3Mode;
    Switch digitalIO4Mode;
    Switch digitalIO5Mode;
    Switch digitalIO6Mode;
    Switch digitalIO7Mode;
    Switch digitalIO8Mode;
    Switch digitalIO9Mode;

    Switch digitalIO0Power;
    Switch digitalIO1Power;
    Switch digitalIO2Power;
    Switch digitalIO3Power;
    Switch digitalIO4Power;
    Switch digitalIO5Power;
    Switch digitalIO6Power;
    Switch digitalIO7Power;
    Switch digitalIO8Power;
    Switch digitalIO9Power;

    Switch digitalInput0Power;
    Switch digitalInput1Power;
    Switch digitalInput2Power;
    Switch digitalInput3Power;
    Switch digitalInput4Power;

    TextView digitalIO0VoltageText;
    TextView digitalIO1VoltageText;
    TextView digitalIO2VoltageText;
    TextView digitalIO3VoltageText;
    TextView digitalIO4VoltageText;
    TextView digitalIO5VoltageText;
    TextView digitalIO6VoltageText;
    TextView digitalIO7VoltageText;
    TextView digitalIO8VoltageText;
    TextView digitalIO9VoltageText;

    TextView digitalInput0VoltageText;
    TextView digitalInput1VoltageText;
    TextView digitalInput2VoltageText;
    TextView digitalInput3VoltageText;
    TextView digitalInput4VoltageText;

    ProgressBar digitalIO0VoltageProgress;
    ProgressBar digitalIO1VoltageProgress;
    ProgressBar digitalIO2VoltageProgress;
    ProgressBar digitalIO3VoltageProgress;
    ProgressBar digitalIO4VoltageProgress;
    ProgressBar digitalIO5VoltageProgress;
    ProgressBar digitalIO6VoltageProgress;
    ProgressBar digitalIO7VoltageProgress;
    ProgressBar digitalIO8VoltageProgress;
    ProgressBar digitalIO9VoltageProgress;

    ProgressBar digitalInput0VoltageProgress;
    ProgressBar digitalInput1VoltageProgress;
    ProgressBar digitalInput2VoltageProgress;
    ProgressBar digitalInput3VoltageProgress;
    ProgressBar digitalInput4VoltageProgress;

    // Place UI into arrays for easier management.
    // TextViews
    TextView[] digitalIOVoltageTextArray = { digitalIO0VoltageText,
            digitalIO1VoltageText, digitalIO2VoltageText,
            digitalIO3VoltageText, digitalIO4VoltageText,
            digitalIO5VoltageText, digitalIO6VoltageText,
            digitalIO7VoltageText, digitalIO8VoltageText, digitalIO9VoltageText };

    TextView[] digitalInputVoltageTextArray = { digitalInput0VoltageText,
            digitalInput1VoltageText, digitalInput2VoltageText,
            digitalInput3VoltageText, digitalInput4VoltageText };
    // Switches

    Switch[] digitalIOPowerSwitchArray = { digitalIO0Power, digitalIO1Power,
            digitalIO2Power, digitalIO3Power, digitalIO4Power, digitalIO5Power,
            digitalIO6Power, digitalIO7Power, digitalIO8Power, digitalIO9Power };

    Switch[] digitalInputPowerSwitchArray = { digitalInput0Power,
            digitalInput1Power, digitalInput2Power, digitalInput3Power,
            digitalInput4Power };

    Switch[] digitalIOModeSwitchArray = { digitalIO0Mode, digitalIO1Mode,
            digitalIO2Mode, digitalIO3Mode, digitalIO4Mode, digitalIO5Mode,
            digitalIO6Mode, digitalIO7Mode, digitalIO8Mode, digitalIO9Mode };
    // progress bars

    ProgressBar[] digitalIOVoltageProgressArray = { digitalIO0VoltageProgress,
            digitalIO1VoltageProgress, digitalIO2VoltageProgress,
            digitalIO3VoltageProgress, digitalIO4VoltageProgress,
            digitalIO5VoltageProgress, digitalIO6VoltageProgress,
            digitalIO7VoltageProgress, digitalIO8VoltageProgress,
            digitalIO9VoltageProgress };

    ProgressBar[] digitalInputVoltageProgressArray = {
            digitalInput0VoltageProgress, digitalInput1VoltageProgress,
            digitalInput2VoltageProgress, digitalInput3VoltageProgress,
            digitalInput4VoltageProgress };
    Context superContext;


    public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
            Bundle SavedInstantState) {
        superContext = getActivity().getApplicationContext();
        digitalfragmentview = viewInflation.inflate(
                R.layout.digitalfragment_page, container, false);

        digitalIO0Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio0mode);
        digitalIO1Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio1mode);
        digitalIO2Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio2mode);
        digitalIO3Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio3mode);
        digitalIO4Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio4mode);
        digitalIO5Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio5mode);
        digitalIO6Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio6mode);
        digitalIO7Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio7mode);
        digitalIO8Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio8mode);
        digitalIO9Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio9mode);

        digitalIO0Power = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio0power);
        digitalIO1Power = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio1power);
        digitalIO2Power = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio2power);
        digitalIO3Power = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio3power);
        digitalIO4Power = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio4power);
        digitalIO5Power = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio5power);
        digitalIO6Power = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio6power);
        digitalIO7Power = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio7power);
        digitalIO8Power = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio8power);
        digitalIO9Power = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio9power);

        digitalIO0VoltageProgress = (ProgressBar) digitalfragmentview
                .findViewById(R.id.digitalio0voltage);
        digitalIO1VoltageProgress = (ProgressBar) digitalfragmentview
                .findViewById(R.id.digitalio0voltage);
        digitalIO2VoltageProgress = (ProgressBar) digitalfragmentview
                .findViewById(R.id.digitalio0voltage);
        digitalIO3VoltageProgress = (ProgressBar) digitalfragmentview
                .findViewById(R.id.digitalio0voltage);
        digitalIO4VoltageProgress = (ProgressBar) digitalfragmentview
                .findViewById(R.id.digitalio0voltage);
        digitalIO5VoltageProgress = (ProgressBar) digitalfragmentview
                .findViewById(R.id.digitalio0voltage);
        digitalIO6VoltageProgress = (ProgressBar) digitalfragmentview
                .findViewById(R.id.digitalio0voltage);
        digitalIO7VoltageProgress = (ProgressBar) digitalfragmentview
                .findViewById(R.id.digitalio0voltage);
        digitalIO8VoltageProgress = (ProgressBar) digitalfragmentview
                .findViewById(R.id.digitalio0voltage);
        digitalIO9VoltageProgress = (ProgressBar) digitalfragmentview
                .findViewById(R.id.digitalio0voltage);

        digitalIO0VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalio0voltagetext);
        digitalIO1VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalio1voltagetext);
        digitalIO2VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalio2voltagetext);
        digitalIO3VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalio3voltagetext);
        digitalIO4VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalio4voltagetext);
        digitalIO5VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalio5voltagetext);
        digitalIO6VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalio6voltagetext);
        digitalIO7VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalio7voltagetext);
        digitalIO8VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalio8voltagetext);
        digitalIO9VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalio9voltagetext);

        digitalInput0VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalinput0voltagetext);
        digitalInput1VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalinput1voltagetext);
        digitalInput2VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalinput2voltagetext);
        digitalInput3VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalinput3voltagetext);
        digitalInput4VoltageText = (TextView) digitalfragmentview
                .findViewById(R.id.digitalinput4voltagetext);

        centralDigitalTable = (TableLayout) digitalfragmentview
                .findViewById(R.id.digitalTable);

        //sets the listener for the mode switches
        for(int i = 0; i<digitalIOModeSwitchArray.length;i++){
            digitalIOModeSwitchArray[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub

                }
            });
        }


        return digitalfragmentview;
    }

问题出在Eltabo所说的那样,他的解决方案可以解决问题。

1 个答案:

答案 0 :(得分:1)

您正在发生的事情的一个简单示例:

package snippet;

public class MainExample {

    public static void main(String[] args) throws Exception {

        MainExample s = null; //A variable containing null

        MainExample[] ss = {s}; //An array containing, the object referenced by your variable, errr... null

        s = new MainExample(); //The varible references a new object

        System.out.println(ss[0]); //The array still contains null
    }
}

要避免此问题,您可以进行以下更改:

package snippet;

public class MainExample {

    public static void main(String[] args) throws Exception {

        MainExample s = null;

        MainExample[] ss = null;

        s = new MainExample();

        ss = new MainExample[]{s};

        System.out.println(ss[0]);
    }
}

希望它有所帮助。