0

So I made my own LinearLayout because I needed a horizontal number picker. I am using this custom layout twice in my activity:

enter image description here

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/button_minus"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="@string/lt"
        android:textSize="30sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/number"
        android:layout_width="75dp"
        android:layout_height="match_parent"
        android:inputType="number"
        android:gravity="center"
        android:focusable="false"
        android:text="0" />

    <Button
        android:id="@+id/button_plus"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="@string/gt"
        android:textSize="30sp"
        android:textStyle="bold" />

</LinearLayout>

JAVA:

public class NumberPickerHorizontal extends LinearLayout
{
    private final EditText number;
    private final Button button_minus, button_plus;

    public NumberPickerHorizontal(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.numberpicker_horizontal, this, true);

        number = (EditText) findViewById(R.id.number);

        button_minus = (Button) findViewById(R.id.button_minus);
        button_minus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                int count = Integer.parseInt(number.getText().toString());
                number.setText(count--);
            }
        });

        button_plus = (Button) findViewById(R.id.button_plus);
        button_plus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                int count = Integer.parseInt(number.getText().toString());
                number.setText(count++);
            }
        });
    }
}

You'll notice I'm trying to activate the onClick methods for the plus and minus buttons... However, when I actually click the buttons, I get the following error:

android.content.res.Resources$NotFoundException: String resource ID #0x0

Not quite sure what I'm doing wrong.

Jason Axelrod
  • 7,155
  • 10
  • 50
  • 78
  • 5
    Possible duplicate of [android.content.res.Resources$NotFoundException: String resource ID #0x0](http://stackoverflow.com/questions/20177003/android-content-res-resourcesnotfoundexception-string-resource-id-0x0) – George Mulligan Jan 28 '16 at 20:39
  • That does supress the error... but the text of the number field doesnt change. – Jason Axelrod Jan 28 '16 at 20:44

3 Answers3

3

You are trying to setText for 0, which should be an string resource(int). Also your new increased value is never used. Increase count before setting the text. Then use setText(String.valueOf(increased count value)).

madar
  • 106
  • 5
1

You should do this:

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/button_minus"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="@string/lt"
        android:textSize="30sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/number"
        android:layout_width="75dp"
        android:layout_height="match_parent"
        android:inputType="number"
        android:gravity="center"
        android:focusable="false"
        android:text="0" />

    <Button
        android:id="@+id/button_plus"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="@string/gt"
        android:textSize="30sp"
        android:textStyle="bold" />

</merge>

Java:

public class NumberPickerHorizontal extends LinearLayout
{
    private final EditText number;
    private final Button button_minus, button_plus;

    @Override
    public NumberPickerHorizontal(Context context) {
        super(context);
        init(context);
    }

    @Override
    public NumberPickerHorizontal(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(context);
    }

    @TargetApi(11)
    @Override
    public NumberPickerHorizontal(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(21)
    @Override
    public NumberPickerHorizontal(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
    //android:layout_width="match_parent"
    //android:layout_height="wrap_content"
    //set these where you instantiate the view, XML or code!

        this.setOrientation(LinearLayout.HORIZONTAL);
        LayoutInflater.from(context).inflate(R.layout.numberpicker_horizontal, this, true);

            number = (EditText) findViewById(R.id.number);

            button_minus = (Button) findViewById(R.id.button_minus);
            button_minus.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    int count = Integer.parseInt(number.getText().toString());
                    count--;
                    number.setText(String.valueOf(count));
                }
            });

            button_plus = (Button) findViewById(R.id.button_plus);
            button_plus.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    int count = Integer.parseInt(number.getText().toString());
                    count++;
                    number.setText(String.valueOf(count));
                }
            });
    }

So the changes were:

1.) added <merge> to remove the duplicate layout

2.) added the remaining 3 constructors for the view in case the framework calls them

3.) added String.valueOf() around your int so that it's not perceived as a resource identifier by setText().

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • This doesn't solve the issue: `number.setText(count++)` is still wrong. But using `` is a good pointer to improvements. – tynn Jan 28 '16 at 20:55
  • @tynn you're right, I added the relevant fix too. At first glance I figured the wrong constructor is called nad that's why the clicks don't work. – EpicPandaForce Jan 28 '16 at 21:01
-1

Try this:

View v = LayoutInflater.from(context).inflate(R.layout.numberpicker_horizontal, this, true);

And replace all findViewById by v.findViewById

P. Jairaj
  • 1,033
  • 1
  • 6
  • 8