Fragment类中的ProgressBar在setMax方法中导致NullPointerException?

时间:2014-08-20 16:32:42

标签: java android android-fragments nullpointerexception android-progressbar

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.analogin_fragment,
                container, false);
        AITVVOLT0 = (TextView) rootView.findViewById(R.id.aintvvolt0);
        AITVVOLT1 = (TextView) rootView.findViewById(R.id.aintvvolt1);
        AITVVOLT2 = (TextView) rootView.findViewById(R.id.aintvvolt2);
        AITVVOLT3 = (TextView) rootView.findViewById(R.id.aintvvolt3);
        AITVVOLT4 = (TextView) rootView.findViewById(R.id.aintvvolt4);
        AITVVOLT5 = (TextView) rootView.findViewById(R.id.aintvvolt5);

        AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
        AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
        AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
        AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
        AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
        AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);


        AIVOLTBAR0.setMax(100);
        AIVOLTBAR1.setMax(100);
        AIVOLTBAR2.setMax(100);
        AIVOLTBAR3.setMax(100);
        AIVOLTBAR4.setMax(100);
        AIVOLTBAR5.setMax(100);

        AIVOLTBAR0.setProgress(0);
        AIVOLTBAR1.setProgress(0);
        AIVOLTBAR2.setProgress(0);
        AIVOLTBAR3.setProgress(0);
        AIVOLTBAR4.setProgress(0);
        AIVOLTBAR5.setProgress(0);

        return rootView;
    }

AIVOLTBAR#.setMax();方法导致NullPointerException - 但ProgressBars不为null(使用日志测试以及注释setMax导致程序工作)。

logcat的:

08-20 16:23:18.409: E/AndroidRuntime(1332): Process: com.nanospark.androidapp, PID: 1332
08-20 16:23:18.409: E/AndroidRuntime(1332): java.lang.NullPointerException
08-20 16:23:18.409: E/AndroidRuntime(1332):     at com.nanospark.androidapp.MainActivity$AnalogInputFragment.onCreateView(MainActivity.java:218)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at android.os.Handler.handleCallback(Handler.java:733)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at android.os.Looper.loop(Looper.java:136)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at java.lang.reflect.Method.invokeNative(Native Method)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at java.lang.reflect.Method.invoke(Method.java:515)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-20 16:23:18.409: E/AndroidRuntime(1332):     at dalvik.system.NativeStart.main(Native Method)
08-20 16:27:27.557: E/BluetoothAdapter(1387): Bluetooth binder is null
08-20 16:27:27.853: E/OpenGLRenderer(1387): Getting MAX_TEXTURE_SIZE from GradienCache
08-20 16:27:27.853: E/OpenGLRenderer(1387): MAX_TEXTURE_SIZE: 16384
08-20 16:27:27.865: E/OpenGLRenderer(1387): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
08-20 16:27:27.869: E/OpenGLRenderer(1387): MAX_TEXTURE_SIZE: 16384
08-20 16:27:28.409: E/AndroidRuntime(1387): FATAL EXCEPTION: main
08-20 16:27:28.409: E/AndroidRuntime(1387): Process: com.nanospark.androidapp, PID: 1387
08-20 16:27:28.409: E/AndroidRuntime(1387): java.lang.NullPointerException
08-20 16:27:28.409: E/AndroidRuntime(1387):     at com.nanospark.androidapp.MainActivity$AnalogInputFragment.onCreateView(MainActivity.java:223)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at android.os.Handler.handleCallback(Handler.java:733)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at android.os.Looper.loop(Looper.java:136)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at java.lang.reflect.Method.invokeNative(Native Method)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at java.lang.reflect.Method.invoke(Method.java:515)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-20 16:27:28.409: E/AndroidRuntime(1387):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

问题是你有一些错别字。您多次设置AIVOLTBAR0而不是单独设置每个进度条:

    AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
    AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
    AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
    AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
    AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
    AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);

相反,它必须是这样的:

    AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
    AIVOLTBAR1 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
    AIVOLTBAR2 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
    AIVOLTBAR3 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
    AIVOLTBAR4 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
    AIVOLTBAR5 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);