Android resource not found exception?

前端 未结 9 615
一生所求
一生所求 2020-11-28 12:11

I am having a strange problem using findViewById(id). It comes back with resource not found even though the resource is definitely there. It is a textview in a layout next t

相关标签:
9条回答
  • 2020-11-28 12:50

    Check if the property android:icon="@mipmap/ic_launcher" exists under the application tag of your manifest file.

    0 讨论(0)
  • 2020-11-28 12:51

    Make sure you don't set any attributes programmaticly which are not available. I had the very same problem and the reason was a RemoteView.setFloat(id,"setWeight",1.0f); to a LinearLayout, which was not supported with Android before 4.x Unfortunately the error message was not very helpful on this.

    0 讨论(0)
  • 2020-11-28 13:01

    Make sure that you aren't really just trying to set the text to a number and expecting it to automatically convert to a string.

    0 讨论(0)
  • 2020-11-28 13:02

    I my case, I was trying to pass drawable selector with item android:color to background of view. The problem here is that you cannot define the background color using a color selector, you need a drawable selector.

    0 讨论(0)
  • 2020-11-28 13:05

    Just to clarify Terra Caines answer since I saw it happening a lot to people; TextView, and other text components, have 2 setText() functions with 1 parameter.

    One of them is with a String and one with int. The int is obviously for a string Resource such as R.string.myString - which, to those who didnt know, R.exm always is represented as int. The string is for putting a string there.

    So for example I want to put int x = 1; in a textView. Doing mTextView.setText(x); will cause the textView to use the resource function and since there is probably no resource with the id 1 it will throw the exception Resource not found. If You want to put an int or any number in the setText() Function make sure to convert it to String (x+"") or (x.toString()) would do the trick for you.

    Hope it saved some time to people.

    0 讨论(0)
  • 2020-11-28 13:06

    Exception Message thrown is not very descriptive. Its very much likely the case you are trying to cast the int value to String, applying the below change fixed the issue for me.

    Code before the fix:

     itemPrice.setText(foodMenuItems.get(position).getItemPrice());
    

    Code after the fix:

     itemPrice.setText(Integer.toString(foodMenuItems.get(position).getItemPrice()));
    
    0 讨论(0)
提交回复
热议问题