gpt4 book ai didi

java - 为什么应用程序抛出 NoSuchMethodError

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:51 34 4
gpt4 key购买 nike

我有一个 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)

如何修改代码使其不抛出异常或有更好的错误处理方法?

最佳答案

改为使用 TextView.setBackgroundResource(int),并将可绘制资源 ID 传递给它,如下所示:

tvRes.setBackgroundColor(Color.parseColor("#73539E"));
tvSVal.setBackgroundResource(R.drawable.lowtextpress);
tvDVal.setBackgroundResource(R.drawable.lowtextpress);

不知道奇怪的错误从何而来,但无论哪种方式,这都是一个更好的解决方案。

关于java - 为什么应用程序抛出 NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21925162/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com