Каждый раз, когда я нажимаю onClickCreateButton, я получаю это в ответ: android.content.res.Resources$NotFoundException: Идентификатор строкового ресурса #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
, а перегрузка Toast.makeText()
, которая принимает аргумент int
, ожидает, что это будет идентификатор ресурса. Вместо этого используйте перегрузку String
, преобразовав int
в строку, например.
Toast.makeText(getApplicationContext(), "" + temp.getChildCount(), Toast.LENGTH_SHORT).show();
Та же проблема с другим Toast
.
Вы пытаетесь показать число, вызывая версию Toast.makeText, которая принимает идентификатор ресурса:
Toast.makeText(getApplicationContext(), temp.getChildCount(), Toast.LENGTH_SHORT).show();
Вы должны преобразовать его в строку:
Toast.makeText(getApplicationContext(), Integer.toString(temp.getChildCount()), Toast.LENGTH_SHORT).show();