Java – Why the application throws NoSuchMethodError

Why the application throws NoSuchMethodError… here is a solution to the problem.

Why the application throws NoSuchMethodError

I have a ProgressBar that calls another function on change, but every once in a while my application crashes and throws a NoSuchMethodError. I included part of my code along with a stack trace to help me solve the problem.

Part of my Java code:

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));
    }
}

My 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)

How can I modify my code so that it doesn’t throw exceptions or has better error handling?

Solution

Instead, use TextView.setBackgroundResource(int) and pass it the drawable resource ID as follows:

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

Not sure where the weird error came from, but either way, this is a better solution.

Related Problems and Solutions