gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:45:42 25 4
gpt4 key购买 nike

    @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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com