简体   繁体   中英

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

Every time i click onClickCreateButton i get this in return: android.content.res.Resources$NotFoundException: String resource ID #0x1

Main Activity class is as follows:

public class MainActivity extends AppCompatActivity {
    EditText mInput;
    RelativeLayout.LayoutParams layoutParams;
    LinearLayout linearLayout2;
    LinearLayout linearLayout3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mInput = findViewById(R.id.EditText);
        linearLayout2 = findViewById(R.id.LinearLayout2);
        layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout3 = new LinearLayout(this);
        linearLayout3.setLayoutParams(layoutParams);
        linearLayout3.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout2.addView(linearLayout3);
    }

    public void onClickCreateButton(View v) {
        LinearLayout temp;
        try {
            Toast.makeText(getApplicationContext(), linearLayout2.getChildCount(), Toast.LENGTH_SHORT).show();
            if (linearLayout2.getChildCount() <= 8) {
                temp = (LinearLayout) linearLayout2.getChildAt(linearLayout2.getChildCount());
                Toast.makeText(getApplicationContext(), temp.getChildCount(), Toast.LENGTH_SHORT).show();
                if (temp.getChildCount() <= 3) {
                    Button button = new Button(this);
                    button.setText(mInput.getText().toString());
                    button.setLayoutParams(layoutParams);
                    linearLayout3.addView(button);
                } else {
                    linearLayout3 = new LinearLayout(this);
                    linearLayout3.setLayoutParams(layoutParams);
                    linearLayout3.setOrientation(LinearLayout.HORIZONTAL);
                    linearLayout2.addView(linearLayout3);
                }
            }
        } catch (Exception ex)
        {
            Toast.makeText(getApplicationContext(), ex+"", Toast.LENGTH_SHORT).show();
        }
    }

activity_main:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/LinearLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />
    </LinearLayout>

It would be of great help is someone could point out the mistake in my code. Thanks! Cheers!

 Toast.makeText(getApplicationContext(), temp.getChildCount(), Toast.LENGTH_SHORT).show();

temp.getChildCount() returns an int and the Toast.makeText() overload that takes an int argument there expects it to be a resource id. Use the String overload instead by converting the int to a string, eg

Toast.makeText(getApplicationContext(), "" + temp.getChildCount(), Toast.LENGTH_SHORT).show();

Same issue with the other Toast .

You're trying to show a number by calling the version of Toast.makeText that takes a resource ID :

Toast.makeText(getApplicationContext(), temp.getChildCount(), Toast.LENGTH_SHORT).show();

You should convert it to a string:

Toast.makeText(getApplicationContext(), Integer.toString(temp.getChildCount()), Toast.LENGTH_SHORT).show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM