I want to automatically increment a number when a specific button is clicked, but the app crashes with the following error:
E/textview: initAddtionalStyle default
W/ResourceType: No package identifier when getting value for resource number 0x00000001
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.circles_2907, PID: 5688
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.HwResources.getText(HwResources.java:1248)
at android.widget.TextView.setText(TextView.java:4169)
at com.example.android.circles_2907.SkipActivity$1.onClick(SkipActivity.java:39)
at android.view.View.performClick(View.java:4768)
at android.view.View$PerformClick.run(View.java:19685)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5538)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:958)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)
The Java code is as below. There is only one OnClickListener
method.
public class SkipActivity extends AppCompatActivity {
Button likebtn,dislike;
static int i=0;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.after_skip_scr);
likebtn=(Button) findViewById(R.id.likebtn) ;
likebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView counter_like=(TextView) findViewById(R.id.counter);
i=i+1;
counter_like.setText(i);
}
});
}
The relevant layout is as below. It includes only one button and one TextView
(increment numbers).
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.circles_2907.SkipActivity">
<Button
android:id="@+id/likebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="98dp"
android:layout_marginStart="55dp"
android:onClick="submitLike"
android:text="Like" />
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_alignBottom="@+id/likebtn"
android:layout_centerHorizontal="true"
android:inputType="number"
android:text="0" />
</RelativeLayout>
Please help!
.setText()
only accepts an argument that is a String
, so instead of passing an int
there, pass a String
value.
For example:
counter_like.setText(String.valueOf(i));
IMPORTANT EDIT AND EXPALANATION:
If you go through .setText() documentation, you will come to known that as a single argument .setText()
accepts either CharSequence
or int resid
(the resource identifier of the string resource to be displayed)
Currently you are passing some custom incrementing int value to .setText()
so setText is searching for some string resources (eg, R.sting.label) for your custom value i
and hence no such resource is available on it, so it is throwing you below error -
No package identifier when getting value for resource number 0x00000001
So instead of passing int value, you should convert your custom int
value to expected CharSequence
using String.valueOf()
.
Thank you MarkKeen for pointing this out. Please correct this answer if I am still going wrong somewhere.
When you assign an integer value to a TextView
with setText()
you actually use this overload of setText()
from the TextView.java
class:
@android.view.RemotableViewMethod
public final void setText(@StringRes int resid) {
setText(getContext().getResources().getText(resid));
mTextFromResource = true;
}
As you can see you can pass an integer value with setText()
to a TextView
but it is considered to be a resource identifier, that's why you get the error:
No package identifier when getting value for resource number 0x00000001
But this is not what you want to do.
So make i
a string by String.valueOf(i)
or just pass i + ""
.
User contributions licensed under CC BY-SA 3.0