错误:android.content.res.Resources$NotFoundException:字符串资源 ID #0x1

问题描述

每次我点击ClickCreateButton我都会得到这个回报: android.content.res.Resources$NotFoundException:字符串资源 ID #0x1

Main Activity类如下:

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>

如果有人可以指出我的代码中的错误,那将会很有帮助。谢谢!干杯!

解决方法

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

temp.getChildCount() 返回一个 int 并且带有 Toast.makeText() 参数的 int 重载期望它是一个资源 ID。使用 String 重载而不是将 int 转换为字符串,例如

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

另一个 Toast 也有同样的问题。

,

您试图通过拨打 the version of Toast.makeText that takes a resource ID 来显示号码:

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

您应该将其转换为字符串:

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