-1

我在字符串转换中遇到问题,我通过意图传递一个值并通过捆绑访问,现在我想转换为整数,但它显示强制停止......

主要活动代码:

             btn.setOnClickListener(new OnClickListener()
         {

        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            Intent in=new Intent(MainActivity.this,second.class);
            in.putExtra("id", "12");
            startActivity(in);
        }

    });

第二个活动代码:

    Bundle extras = getIntent().getExtras();
    id=extras.getString("id");
    btn=(Button)findViewById(R.id.button1);
    tv=(TextView)findViewById(R.id.value);
    btn.setOnClickListener(new OnClickListener()
    {

        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            int myid=Integer.parseInt(id);
                            //tv.setText(myid);
            Toast.makeText(getApplicationContext(), myid,Toast.LENGTH_SHORT).show();
        }

    });

             LogCat:>
                 09-25 18:21:52.045: E/AndroidRuntime(1555): FATAL EXCEPTION: main
                 09-25 18:21:52.045: E/AndroidRuntime(1555): android.content.res.Resources$NotFoundException: String resource ID #0xc
                 09-25 18:21:52.045: E/AndroidRuntime(1555):    at android.content.res.Resources.getText(Resources.java:230)
                 09-25 18:21:52.045: E/AndroidRuntime(1555):    at android.widget.Toast.makeText(Toast.java:265)
                 09-25 18:21:52.045: E/AndroidRuntime(1555):    at com.example.convert.second$1.onClick(second.java:34)
                 09-25 18:21:52.045: E/AndroidRuntime(1555):    at android.view.View.performClick(View.java:4204)
                 09-25 18:21:52.045: E/AndroidRuntime(1555):    at android.view.View$PerformClick.run(View.java:17355)
                 09-25 18:21:52.045: E/AndroidRuntime(1555):    at android.os.Handler.handleCallback(Handler.java:725)
                 09-25 18:21:52.045: E/AndroidRuntime(1555):    at android.os.Handler.dispatchMessage(Handler.java:92)
                 09-25 18:21:52.045: E/AndroidRuntime(1555):    at android.os.Looper.loop(Looper.java:137)
                  09-25 18:21:52.045: E/AndroidRuntime(1555):   at android.app.ActivityThread.main(ActivityThread.java:5041)
                 09-25 18:21:52.045: E/AndroidRuntime(1555):    at java.lang.reflect.Method.invokeNative(Native Method)
                  09-25 18:21:52.045: E/AndroidRuntime(1555):   at java.lang.reflect.Method.invoke(Method.java:511)
                   09-25 18:21:52.045: E/AndroidRuntime(1555):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                   09-25 18:21:52.045: E/AndroidRuntime(1555):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                   09-25 18:21:52.045: E/AndroidRuntime(1555):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

Don't do TextView.setText(int), do TextView.setText(String),这意味着完全跳过对话。可以有多个输入setText()如此处所示

在您的情况下发生的事情是,由于您将值转换为整数,因此您无意中调用public final void setText (int resid)了它,假设传递给它的整数是文件中某些内容的 R.id 值strings.xml

这显然不是你想要做的,所以你可以做两件事之一来解决它:

  1. 将您的 setText() 替换为setText("" + myid);
  2. 停止将您的值转换为 int!
于 2013-09-25T18:29:49.920 回答