开发者

Android dev - Issue with using SeekBar

I'm trying to implement a SeekBar in a simple flashlight app (that I'm learning on) and cannot understand why the app keeps crashing when I set the OnSeekBarChangeListener to my SeekBar. As soon as a remove all of the code inside the onStopTrackingTouch() for the listener, the app runs fine. But if there is anything at all inside the listener's methods, it crashes. I'm also using buttons and gestures in the app and have not had any problems using those.

I am bringing up a separate layout when the user presses the Menu button | Brightness option, which displays the SeekBar (to adjust the brightness)

Here is the how I am implementing the SeekBar:

SeekBar mSeekBar;

...

@Override 
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);

...

mSeekBar = (SeekBar)findViewById(R.id.mSeekbar);
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //first set the brightness mode to manual
                Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_开发者_如何转开发MODE_MANUAL);

                //change the actual setting
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                float brightness = Float.valueOf(seekBar.getProgress()); //change the brightness here
                lp.screenBrightness = brightness;
                getWindow().setAttributes(lp); //set the new brightness
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
            }
        });

Thank you for your time and advice.

UPDATE: Here is the link to my Logcat info http://pastie.org/2561098

UPDATE 2: Here is the link to my release run LogCat info: http://pastie.org/2561111

SOLVED:

I had mSeekBar being instantiated and the listener's set before the layout was present. The SeekBar was null because the separate layout I made for adjusting the brightness was not currently in view. Putting the instantiation and listener's inside the switch statement I use for options menu, after the new layout (for the brightness) is set works perfectly.

Thank you!


There is your answer. On line 77 of your onCreate method your program is crashing because of a NullPointerException. The answer is in the stack trace, while these look perplexing, they actually point you to the exact line of code that caused the crash (most of the time).

at com.polaniec.myflashlight.MyFlashLightActivity.onCreate(MyFlashLightActivity.java:77)

When you dig through the stack trace you want to look for the part that indicates YOUR actual class name and method name (not all of the android.os... or java.lang... methods). The 77 means it is line 77 in your code. Whatever you are referencing is null, maybe it was not instantiated?

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜