简体   繁体   English

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

[英]Error: android.content.res.Resources$NotFoundException: String resource ID #0x1

Every time i click onClickCreateButton i get this in return: android.content.res.Resources$NotFoundException: String resource ID #0x1每次我点击 onClickCreateButton 我都会得到这个回报: android.content.res.Resources$NotFoundException: String resource ID #0x1

Main Activity class is as follows:主要活动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();
        }
    }

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>

It would be of great help is someone could point out the mistake in my code.如果有人可以指出我的代码中的错误,那将有很大帮助。 Thanks!谢谢! Cheers!干杯!

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

temp.getChildCount() returns an int and the Toast.makeText() overload that takes an int argument there expects it to be a resource id. temp.getChildCount()返回一个intToast.makeText()重载,它接受一个int参数,期望它是一个资源 id。 Use the String overload instead by converting the int to a string, eg通过将int转换为字符串来使用String重载,例如

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

Same issue with the other Toast .另一个Toast有同样的问题。

You're trying to show a number by calling the version of Toast.makeText that takes a resource ID :您试图通过调用采用资源 ID 的Toast.makeText版本来显示一个数字:

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

You should convert it to a string:您应该将其转换为字符串:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.content.res.Resources $ NotFoundException:字符串资源ID#0x1 - android.content.res.Resources$NotFoundException: String resource ID #0x1 无法在 textview 中设置文本。错误:-android.content.res.Resources$NotFoundException:字符串资源 ID #0x0 - Faild to settext in textview.Error:-android.content.res.Resources$NotFoundException: String resource ID #0x0 android.content.res.Resources $ NotFoundException:资源ID#0x0 - android.content.res.Resources$NotFoundException: Resource ID #0x0 android.content.res.Resources$NotFoundException · 字符串资源 ID # - android.content.res.Resources$NotFoundException · String resource ID # android.content.res.Resources $ NotFoundException:资源ID#0x0 Android错误 - android.content.res.Resources$NotFoundException: Resource ID #0x0 Android error GridView.FATAL例外:main android.content.res.Resources $ NotFoundException:字符串资源ID#0x0 - GridView.FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: String resource ID #0x0 android.content.res.Resources $ NotFoundException:字符串资源ID#0x2 - android.content.res.Resources$NotFoundException: String resource ID #0x2 android.content.res.Resources$NotFoundException: 字符串资源 ID #0x0 - android.content.res.Resources$NotFoundException: String resource ID #0x0 android.content.res.Resources $ NotFoundException:字符串资源ID#0x42 - android.content.res.Resources$NotFoundException: String resource ID #0x42 android.content.res.Resources$NotFoundException: 字符串资源 ID #0x4 - android.content.res.Resources$NotFoundException: String resource ID #0x4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM