![](/img/trans.png)
[英]android.content.res.Resources$NotFoundException: String resource ID #0x1
[英]Error: android.content.res.Resources$NotFoundException: String resource ID #0x1
每次我点击 onClickCreateButton 我都会得到这个回报: android.content.res.Resources$NotFoundException: String resource ID #0x1
主要活动class如下:
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>
如果有人可以指出我的代码中的错误,那将有很大帮助。 谢谢! 干杯!
Toast.makeText(getApplicationContext(), temp.getChildCount(), Toast.LENGTH_SHORT).show();
temp.getChildCount()
返回一个int
和Toast.makeText()
重载,它接受一个int
参数,期望它是一个资源 id。 通过将int
转换为字符串来使用String
重载,例如
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.