每次单击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>
如果有人能指出我代码中的错误,那会很有帮助。谢谢!干杯!
2条答案
按热度按时间b4wnujal1#
Toast.makeText(getApplicationContext(), "" + temp.getChildCount(), Toast.LENGTH_SHORT).show();
jv4diomz2#
你想通过调用
Toast.makeText
需要资源id的:应将其转换为字符串: