不能再掀"活动"在Android的 - 提供错误 [英] Can't start another "Activity" in Android - Gives Error

查看:87
本文介绍了不能再掀"活动"在Android的 - 提供错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个更大的code简化版本。我刚开始与Android编程和糟糕坚持这个问题过去一个小时。

This is simplified version of a larger code. I've just started with Android programming and badly stuck with this problem for past an hour.

    /**Main Activity**/

    sumBut.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(MainActivity.this, SumActivity.class);
            intent.putExtra("var1", et1.getText().toString());
            intent.putExtra("var2", et2.getText().toString());
            startActivity(intent);
        }
    });

此code获取从文本框两个可变和驱动用户到另一个活动其中这些数字的总和将被显示。

This code takes two variable from the text boxes and drives the user to another activity where the sum of these numbers will be shown.

下面是目标活动:

    public class SumActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sum);

    TextView tv = (TextView) findViewById(R.id.textView1);
    Intent intent = getIntent();
    int a = Integer.parseInt(intent.getStringExtra("var1"));
    int b = Integer.parseInt(intent.getStringExtra("var2"));
    int c = a+b;
    tv.setText(c);
}

有相关的内部资源/文件夹的布局XML文件为我所创建的类。

There are associated xml files inside res/layout folder for all the classes I've created.

这是我的Manifest.xml文件的示例

Sample from my Manifest.xml File

  <activity
        android:name="com.example.summer.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.example.summer.SumActivity"></activity>
    <activity android:name="com.example.summer.DifActivity"></activity>

LogCat中

LogCat

推荐答案

这是你的问题

 tv.setText(c);

将其更改为

 tv.setText(String.valueOf(c));

有不同的的setText()方法,你会看到的在这里的文档。一个你正在调用,它接受一个 INT 作为一个参数,用于查找与'身份证',所以你需要您的变量更改为<$资源C $ C>字符串。这就是为什么你会得到一个未找到资源例外。它无法找到字符串资源 ID 无论变量 C

There are different setText() methods as you will see here in the docs. The one that you are calling, which accepts an int as a parameter, is used to find a resource with that 'id' so you need to change your variable to a String. This is why you get a Resource Not found exception. It can't find a String resource with the id of whatever variable c is.

只是一个侧面说明,但你可能要包装你分析code进行的try / catch ,除非你正在做一些其他错误检查或你最终可能与数字格式异常如果用户输入一个非整数值。

Just a side note but you might want to wrap your parsing code in a try/catch unless you are doing some other error checking or you may end up with a Number Format Exception if the user enters a non-integer value.

这篇关于不能再掀&QUOT;活动&QUOT;在Android的 - 提供错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆