java - Fragment 类内部的 ProgressBar 在 setMax 方法中导致 NullPointerException?

标签 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 - 但 ProgressBar 不为空(使用日志进行测试并注释掉 setMax 导致程序正常工作)。

日志猫:

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)

最佳答案

问题是你有一些拼写错误。您多次设置 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);

关于java - Fragment 类内部的 ProgressBar 在 setMax 方法中导致 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409963/

相关文章:

java - BearerTokenAccessDeniedHandler 类定义未找到

java - 在 Spring 使用 Haversine 公式获取最近的位置

java - Android AsyncTask onPostExecute 调用晚了?

android - NullPointerException LayoutInflater.from(context) - Android fragment

android - 操作栏按钮未显示在 fragment 中

java - 无法从命令行运行命令 : ffmpeg doesn't convert file

java - spring中@Named类的全局异常处理

java - 如何更改 ActionBar 的颜色

android - 如何在Gradle 3.0.0上使用 'implementation/api'

android-fragments - Tablayout 更新自定义 View 选项卡的页面标题