Android 开发人员 - 使用 SeekBar 的问题
我正在尝试在一个简单的手电筒应用程序(我正在学习)中实现 SeekBar,并且无法理解为什么当我将 OnSeekBarChangeListener 设置为我的 SeekBar 时应用程序不断崩溃。一旦删除监听器 onStopTrackingTouch() 内的所有代码,应用程序就可以正常运行。但如果侦听器的方法中有任何内容,它就会崩溃。我还在应用程序中使用按钮和手势,并且在使用这些按钮和手势时没有遇到任何问题。
当用户按下“菜单”按钮时,我将调出一个单独的布局 |亮度选项,显示 SeekBar(用于调整亮度)
以下是我实现 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
}
});
感谢您的时间和建议。
更新:这是我的 Logcat 信息的链接 http://pastie.org/2561098
更新 2:这是链接到我的发布运行 LogCat 信息:http://pastie.org/2561111
已解决:
我在布局出现之前实例化了 mSeekBar 并设置了侦听器。 SeekBar 为空,因为我为调整亮度而制作的单独布局当前不在视图中。在设置新布局(用于亮度)之后,将实例化和侦听器放入我用于选项菜单的 switch 语句中,效果非常好。
谢谢你!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是你的答案。在 onCreate 方法的第 77 行,您的程序因 NullPointerException 而崩溃。答案就在堆栈跟踪中,虽然这些看起来令人困惑,但它们实际上指出了导致崩溃的确切代码行(大多数情况下)。
当您挖掘堆栈跟踪时,您想要查找指示您的实际类名称和方法名称的部分(不是所有 android.os... 或 java.lang... 方法)。 77 表示它是代码中的第 77 行。无论您引用什么都是空的,也许它没有被实例化?
希望这有帮助!
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).
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!