微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

每次我点击ClickCreateButton我都会得到这个回报: android.content.res.Resources$NotFoundException:字符串资源 ID #0x1

Main Activity类如下:

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 重载期望它是一个资源 ID。使用 String 重载而不是将 int 转换为字符串,例如

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

另一个 Toast 也有同样的问题。

,

您试图通过拨打 the version of Toast.makeText that takes a resource ID 来显示号码:

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。