Question

I have a ProgressBar which calls another function when changed, but every once in a while my app crashes and throws a NoSuchMethodError. I am including my partial code as well as the stack trace to help me troubleshoot the issue.

My Partial 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 the code so it doesn't throw the exception or have a better error handling method?

Was it helpful?

Solution

Use TextView.setBackgroundResource(int) instead, and pass it the drawable resource ID, like this:

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

Don't know where the strange error came from, but this is a better solution either way.

OTHER TIPS

setBackground() can be used only for api level 16 and above. Use setBackgroundDrawable() as I think in your case the api level is set to below 16.

Phone with api level below 16,they will support

setBackgroundDrawable()

OR you can do this:

if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) {
    myTextView.setBackground(d);
}
else {
    myTextView.setBackgroundDrawable(d);
}

There are two commons scenarios for NoSuchMethodError:

1) API method/s you have used in your code is different than run time jar version

2) Jar (X) you have used has dependency on Jar (Y)--> But you have incompatible Jar (Y) at runtime.

EDIT:

For #1 how can I prevent it?

This is easy to handle case, add a jar to lib and add same jar in compiler path (add library section if eclipse)

For #2 why does it happen?

This happens if you have downloaded Jar X which in turn requires jar Y, so you will go internet and download, but doesn't care much about which version of Y is compatible with X.

How can we prevent this case? Read readme.txt from Jar X and download supported Y version.

Any way to prevent both?

There is no bullet answer for this, you need to properly read the documentation instead of simply download and put in classpath.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top