-1

Android Studio 3.0.1, Java 8.

Here my xml file (layout):

           <android.support.constraint.ConstraintLayout
                android:id="@+id/contentContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

               <ImageView
                    android:id="@+id/minusImageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_minus" />

                <TextView
                    android:id="@+id/counterTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toTopOf="@+id/minusImageView" />

                <ImageView
                    android:id="@+id/plusImageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/ic_plus" />    
</android.support.constraint.ConstraintLayout>

Here java code in activity (ShoppingDetailsActivity):

@BindView(R.id.counterTextView)
TextView counterTextView;
@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shopping_details);
        ButterKnife.bind(this);
        init(productId);
}

private void init(int productId) {
  setQuantityCounter(quantityCounter);
}  
private void setQuantityCounter(int count) {
        counterTextView.setText(count);
}

But I get error in method setQuantityCounter(int count) when activity is try to open:

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.android.customer.debug/com.myproject.android.customer.ui.ShoppingDetailsActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
Alex
  • 705
  • 1
  • 9
  • 18

1 Answers1

2

Your problem is here:

private void setQuantityCounter(int count) {
    counterTextView.setText(count);
}

When you pass an int to TextView.setText(), it is assuming that you're passing a String resource identifier... something like R.string.my_string (which is an int under the hood).

It looks like you're just trying to display a count. The easiest way to do this (though not strictly the best way) would be something like this:

private void setQuantityCounter(int count) {
    counterTextView.setText("" + count);
}
Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • Admittedly, `String.valueOf()` or `Integer.toString()` would be better options. – Anindya Dutta Dec 12 '17 at 19:29
  • @AnindyaDutta those two are functionally identical to the string concatenation option. The best solution would be to use locale-based formatting... something like `String.format(Locale.getDefault(), "%d", count);` – Ben P. Dec 12 '17 at 19:31