我有一个简单的搜索栏,打算用来调整系统音量。但是,它已经抛出空指针异常已有一段时间了:

int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        Log.d("tag","+"+max);
        volumeSeekbar.setMax(max);
        int progress = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        volumeSeekbar.setProgress(progress);

        volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
            @Override
            public void onStopTrackingTouch(SeekBar arg0)
            {
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0)
            {
                Toast.makeText(getApplicationContext(),"HEREO",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
            {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress, 0);
                Toast.makeText(getApplicationContext(),""+progress,Toast.LENGTH_SHORT).show();
            }
        });


进度和最大值都被证明是有效的整数。但是,连续会抛出空指针异常!


12-24 09:23:07.471 11705-11705 / com.company.volume W / System.err:java.lang.NullPointerException
12-24 09:23:07.472 11705-11705 / com.company.volume W / System.err:位于com.company.volume.MainActivity.initControls(MainActivity.java:71)
12-24 09:23:07.472 11705-11705 / com.company.volume W / System.err:位于com.company.volume.MainActivity.onCreate(MainActivity.java:61)
12-24 09:23:07.472 11705-11705 / com.company.volume W / System.err:位于android.app.Activity.performCreate(Activity.java:5248)
12-24 09:23:07.473 11705-11705 / com.company.volume W / System.err:位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
12-24 09:23:07.473 11705-11705 / com.company.volume W / System.err:位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
12-24 09:23:07.473 11705-11705 / com.company.volume W / System.err:位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
12-24 09:23:07.473 11705-11705 / com.company.volume W / System.err:at android.app.ActivityThread.access $ 800(ActivityThread.java:139)
12-24 09:23:07.474 11705-11705 / com.company.volume W / System.err:位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1210)
12-24 09:23:07.474 11705-11705 / com.company.volume W / System.err:位于android.os.Handler.dispatchMessage(Handler.java:102)
12-24 09:23:07.474 11705-11705 / com.company.volume W / System.err:位于android.os.Looper.loop(Looper.java:136)
12-24 09:23:07.474 11705-11705 / com.company.volume W / System.err:位于android.app.ActivityThread.main(ActivityThread.java:5086)
12-24 09:23:07.475 11705-11705 / com.company.volume W / System.err:at java.lang.reflect.Method.invokeNative(本机方法)
12-24 09:23:07.475 11705-11705 / com.company.volume W / System.err:at java.lang.reflect.Method.invoke(Method.java:515)
12-24 09:23:07.475 11705-11705 / com.company.volume W / System.err:位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)
12-24 09:23:07.475 11705-11705 / com.company.volume W / System.err:位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-24 09:23:07.475 11705-11705 / com.company.volume W / System.err:位于dalvik.system.NativeStart.main(本机方法)

最佳答案

可能您的volumeSeekbarnull。不是progressmax值。确保volumeSeekbar定义正确。

08-18 18:09