我有以下代码:
public class MyProfileActivity extends Activity {
private SeekBar mSeekBar;
private TextView mSeekBarValue;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_profile);
mSeekBar = (SeekBar) findViewById(R.id.seekBar1);
mSeekBarValue = (TextView) findViewById(R.id.seek_bar_value);
mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int value = mSeekBar.getProgress();
mSeekBarValue.setText(value);
SharedPreferences prefs = getSharedPreferences(
PublicMacros.SP_NAME, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt(PublicMacros.USER_SETTINGS_RADIUS, value);
editor.commit();
}
});
}
}
我在尝试设置
时遇到以下错误mSeekBarValue.setText(value);
错误:
07-06 06:57:57.456: E/(31651): Device driver API match
07-06 06:57:57.456: E/(31651): Device driver API version: 23
07-06 06:57:57.456: E/(31651): User space API version: 23
07-06 06:57:57.456: E/(31651): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 30 09:36:10 KST 2013
07-06 06:58:29.041: E/ViewRootImpl(31651): sendUserActionEvent() mView == null
07-06 06:59:11.576: E/InputEventReceiver(31651): Exception dispatching input event.
07-06 06:59:11.576: E/MessageQueue-JNI(31651): Exception in MessageQueue callback: handleReceiveCallback
07-06 06:59:11.721: E/MessageQueue-JNI(31651): android.content.res.Resources$NotFoundException: String resource ID #0x35c
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.content.res.Resources.getText(Resources.java:1068)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.widget.TextView.setText(TextView.java:4546)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at com.zoomer.settings.MyProfileActivity$1.onProgressChanged(MyProfileActivity.java:47)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.widget.SeekBar.onProgressRefresh(SeekBar.java:96)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:684)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.widget.ProgressBar.refreshProgress(ProgressBar.java:709)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.widget.ProgressBar.setProgress(ProgressBar.java:799)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:655)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:566)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.View.dispatchTouchEvent(View.java:7817)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2131)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2131)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2131)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2131)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2131)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2326)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1612)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.app.Activity.dispatchTouchEvent(Activity.java:2494)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2274)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.View.dispatchPointerEvent(View.java:8025)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4675)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4563)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4268)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4191)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4325)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4191)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6317)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6255)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6226)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6397)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:188)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.os.MessageQueue.nativePollOnce(Native Method)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.os.MessageQueue.next(MessageQueue.java:132)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.os.Looper.loop(Looper.java:124)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at android.app.ActivityThread.main(ActivityThread.java:5493)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at java.lang.reflect.Method.invoke(Method.java:525)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
07-06 06:59:11.721: E/MessageQueue-JNI(31651): at dalvik.system.NativeStart.main(Native Method)
我看到mSeekBarValue不为null。怎么会丢失丢失的src错误?
答案 0 :(得分:2)
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int value = mSeekBar.getProgress();
mSeekBarValue.setText(value); //<-- Error
//^^When you pass an int value to setText,
//android will look for a resource matching that id
//but it wont find it because its just an int value
//that you're passing not a resource id.
SharedPreferences prefs = getSharedPreferences(
PublicMacros.SP_NAME, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt(PublicMacros.USER_SETTINGS_RADIUS, value);
editor.commit();
}
});
mSeekBarValue.setText(Integer.toString());
mSeekBar.getProgress()
,因为进度值会在onProgressChanged()
方法的progress参数中传递给您。只需使用mSeekBarValue.setText(Integer.toString(progress));
答案 1 :(得分:1)
您的代码:
int value = mSeekBar.getProgress();
mSeekBarValue.setText(value);
使用方法setText(int resid)
,因为您提供int
作为参数。此方法需要资源ID作为输入。在您的代码中,此资源ID不存在。
要使用setText(CharSequence text)
,您需要转换或以其他方式更改输入,以使其为CharSequence
(String
实现CharSequence
)。
获得所需结果的示例代码可能是:
int value = mSeekBar.getProgress();
mSeekBarValue.setText(String.valueOf(value));