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



每次单击ClickCreateButton时,我都会得到以下回报:android.content.res.Resources$NotFoundException:字符串资源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();
}
}

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,而接受int参数的Toast.makeText()重载则希望它是一个资源id。请使用String重载,将int转换为字符串,例如

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

与其他Toast的问题相同。

您试图通过调用采用资源ID:的Toast.makeText版本来显示数字

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

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

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

相关内容

  • 没有找到相关文章

最新更新