错误:android.content.res.resources$notfoundexception:字符串资源id#0x1

bfnvny8b  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(188)

每次单击onclickcreatebutton,我都会得到这样的结果:android.content.res.resources$notfoundexception:string resource id#0x1
主要活动类别如下:

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();
        }
    }

主要活动:

<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>

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

b4wnujal

b4wnujal1#

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` .
jv4diomz

jv4diomz2#

你想通过调用 Toast.makeText 需要资源id的:

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

应将其转换为字符串:

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

相关问题