简体   繁体   中英

Null Pointer Exception for SeekBar set Max and set Progress

I have a simple seek bar with which I intend to adjust the system volume with. However it has been throwing a null pointer exception for quite some time:

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();
            }
        });

Both progress and max turn out to be valid integers. However, a null pointer exception is continuously thrown!

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﹕ at com.company.volume.MainActivity.initControls(MainActivity.java:71) 12-24 09:23:07.472 11705-11705/com.company.volume W/System.err﹕ at com.company.volume.MainActivity.onCreate(MainActivity.java:61) 12-24 09:23:07.472 11705-11705/com.company.volume W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5248) 12-24 09:23:07.473 11705-11705/com.company.volume W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 12-24 09:23:07.473 11705-11705/com.company.volume W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 12-24 09:23:07.473 11705-11705/com.company.volume W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 12-24 09:23:07.473 11705-11705/com.company.volume W/System.err﹕ at android.ap p.ActivityThread.access$800(ActivityThread.java:139) 12-24 09:23:07.474 11705-11705/com.company.volume W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 12-24 09:23:07.474 11705-11705/com.company.volume W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102) 12-24 09:23:07.474 11705-11705/com.company.volume W/System.err﹕ at android.os.Looper.loop(Looper.java:136) 12-24 09:23:07.474 11705-11705/com.company.volume W/System.err﹕ at 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(Native Method) 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﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 12-24 09:23:07.475 11705-11705/com.company.volume W/System.err ﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 12-24 09:23:07.475 11705-11705/com.company.volume W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

probably your volumeSeekbar is null . not progress and max values. Make sure that volumeSeekbar defined correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM