我有一个ProgressBar
,当它发生改变时会调用另一个函数,但是偶尔我的应用程序会崩溃并抛出NoSuchMethodError
。我包含了我的部分代码以及堆栈跟踪来帮助我解决这个问题。
我的Java部分代码:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
ShowText();
switch(seekBar.getId()) {
case R.id.syst_bar:
ShowText();
progress = progress + 70;
tvSVal.setText(String.valueOf(progress));
if (progress == 70) {
tvSOne.setTextColor(Color.parseColor("#4E9CD2"));
tvSTwo.setTextColor(Color.WHITE);
}
else if (progress == 190) {
tvSOne.setTextColor(Color.WHITE);
tvSTwo.setTextColor(Color.parseColor("#4E9CD2"));
}
else {
tvSOne.setTextColor(Color.WHITE);
tvSTwo.setTextColor(Color.WHITE);
}
break;
case R.id.dias_bar:
ShowText();
progress = progress + 40;
tvDVal.setText(String.valueOf(progress));
if (progress == 40) {
tvDOne.setTextColor(Color.parseColor("#4E9CD2"));
tvDTwo.setTextColor(Color.WHITE);
}
else if (progress == 100) {
tvDOne.setTextColor(Color.WHITE);
tvDTwo.setTextColor(Color.parseColor("#4E9CD2"));
}
else {
tvDOne.setTextColor(Color.WHITE);
tvDTwo.setTextColor(Color.WHITE);
}
break;
default:
break;
}
}
public void ShowText() {
int inOne = sbSyst.getProgress() + 70;
int inTwo = sbDias.getProgress() + 40;
Log.i("SYSTOLIC SHOWTEXT", String.valueOf(inOne));
Log.i("DIASTOLIC SHOWTEXT", String.valueOf(inTwo));
if ((inOne >= 70 && inOne <= 90) && (inTwo >= 40 && inTwo <= 60)) {
tvRes.setText("LOW");
tvRes.setBackgroundColor(Color.parseColor("#73539E"));
tvSVal.setBackground(getResources().getDrawable(R.drawable.lowtextpress));
tvDVal.setBackground(getResources().getDrawable(R.drawable.lowtextpress));
}
if ((inOne >= 70 && inOne <= 90) && (inTwo >= 61 && inTwo <= 80)) {
tvRes.setText("IDEAL");
tvRes.setBackgroundColor(Color.parseColor("#679800"));
tvSVal.setBackground(getResources().getDrawable(R.drawable.idealtextpress));
tvDVal.setBackground(getResources().getDrawable(R.drawable.idealtextpress));
}
if ((inOne >= 70 && inOne <= 90) && (inTwo >= 81 && inTwo <= 90)) {
tvRes.setText("PRE-HIGH");
tvRes.setBackgroundColor(Color.parseColor("#967400"));
tvSVal.setBackground(getResources().getDrawable(R.drawable.prehightextpress));
tvDVal.setBackground(getResources().getDrawable(R.drawable.prehightextpress));
}
if ((inOne >= 70 && inOne <= 90) && (inTwo >= 91 && inTwo <= 100)) {
tvRes.setText("HIGH");
tvRes.setBackgroundColor(Color.parseColor("#960000"));
tvSVal.setBackground(getResources().getDrawable(R.drawable.hightextpress));
tvDVal.setBackground(getResources().getDrawable(R.drawable.hightextpress));
}
if ((inOne >= 91 && inOne <= 120) && (inTwo >= 40 && inTwo <= 80)) {
tvRes.setText("IDEAL");
tvRes.setBackgroundColor(Color.parseColor("#679800"));
tvSVal.setBackground(getResources().getDrawable(R.drawable.idealtextpress));
tvDVal.setBackground(getResources().getDrawable(R.drawable.idealtextpress));
}
if ((inOne >= 91 && inOne <= 120) && (inTwo >= 81 && inTwo <= 90)) {
tvRes.setText("PRE-HIGH");
tvRes.setBackgroundColor(Color.parseColor("#967400"));
tvSVal.setBackground(getResources().getDrawable(R.drawable.prehightextpress));
tvDVal.setBackground(getResources().getDrawable(R.drawable.prehightextpress));
}
if ((inOne >= 91 && inOne <= 120) && (inTwo >= 91 && inTwo <= 100)) {
tvRes.setText("HIGH");
tvRes.setBackgroundColor(Color.parseColor("#960000"));
tvSVal.setBackground(getResources().getDrawable(R.drawable.hightextpress));
tvDVal.setBackground(getResources().getDrawable(R.drawable.hightextpress));
}
if ((inOne >= 121 && inOne <= 140) && (inTwo >= 40 && inTwo <= 90)) {
tvRes.setText("PRE-HIGH");
tvRes.setBackgroundColor(Color.parseColor("#967400"));
tvSVal.setBackground(getResources().getDrawable(R.drawable.prehightextpress));
tvDVal.setBackground(getResources().getDrawable(R.drawable.prehightextpress));
}
if ((inOne >= 121 && inOne <= 140) && (inTwo >= 91 && inTwo <= 100)) {
tvRes.setText("HIGH");
tvRes.setBackgroundColor(Color.parseColor("#960000"));
tvSVal.setBackground(getResources().getDrawable(R.drawable.hightextpress));
tvDVal.setBackground(getResources().getDrawable(R.drawable.hightextpress));
}
if ((inOne >= 141 && inOne <= 190) && (inTwo >= 40 && inTwo <= 100)) {
tvRes.setText("HIGH");
tvRes.setBackgroundColor(Color.parseColor("#960000"));
tvSVal.setBackground(getResources().getDrawable(R.drawable.hightextpress));
tvDVal.setBackground(getResources().getDrawable(R.drawable.hightextpress));
}
}
我的LogCat
:
java.lang.NoSuchMethodError: android.widget.TextView.setBackground
at com.sikni8.bloodpressurelite.MainActivity.ShowText(MainActivity.java:231)
at com.sikni8.bloodpressurelite.MainActivity.onProgressChanged(MainActivity.java:171)
at android.widget.SeekBar.onProgressRefresh(SeekBar.java:89)
at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:609)
at android.widget.ProgressBar.refreshProgress(ProgressBar.java:621)
at android.widget.ProgressBar.setProgress(ProgressBar.java:670)
at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:411)
at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:342)
at android.view.View.dispatchTouchEvent(View.java:5553)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2019)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2019)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2019)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2019)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2019)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1930)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1389)
at android.app.Activity.dispatchTouchEvent(Activity.java:2378)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1878)
at android.view.View.dispatchPointerEvent(View.java:5733)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3104)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2666)
at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:900)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2675)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4624)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
我该如何修改代码,以免抛出异常或者使用更好的错误处理方法?
setBackgroundResource
替代。这是文档链接:http://developer.android.com/reference/android/view/View.html#setBackgroundResource(int) - Peshal