将字符串传输到新活动 +将字符串转换为整数

发布于 2025-01-08 14:21:20 字数 3087 浏览 0 评论 0原文

首先,我先贴一张图,方便大家理解。

在此处输入图像描述

如您所见,有一个 EditText 和一个按钮。 我希望按钮将 EditText 的内容保存到字符串中+启动一个新活动。

在下一个活动中,我想将字符串转换为整数。

这是我当前的代码:

SENDER ACTIVITY

        startscore = (EditText) findViewById(R.id.startscore);

proceed = (Button) findViewById(R.id.bProceed);
proceed.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        Intent myIntent = new Intent(Introscreen.this, BillardScoreboardActivity.class);
        String s = startscore.getText().toString();
        Bundle b = new Bundle();
        b.putString("lol", s);
        //put into your intent
        myIntent.putExtras(b);
        Introscreen.this.startActivity(myIntent);

    }
}); 
}

RECEIVER ACTIVITY

        int counter1, counter2, counter3, counter4, counter5;

            oncreate.....{

            Bundle b = getIntent().getExtras();
    String s = b.getString("lol");




    column1tv = (TextView) findViewById(R.id.column1text);
    column2tv = (TextView) findViewById(R.id.column2text);
    column3tv = (TextView) findViewById(R.id.column3text);
    column4tv = (TextView) findViewById(R.id.column4text);
    column5tv = (TextView) findViewById(R.id.column5text);

    column1tv.setText(counter1);
    column2tv.setText(counter2);
    column3tv.setText(counter3);
    column4tv.setText(counter4);
    column5tv.setText(counter5);

希望您能帮我排除故障,找出问题所在。

问题:

单击按钮后,它会关闭应用程序并给出这些错误代码:

02-23 15:01:24.136: E/AndroidRuntime(295): FATAL EXCEPTION: main

java.lang.RuntimeException: Unable to start activity
ComponentInfo{inno.games/inno.games.BillardScoreboardActivity}:
java.lang.NumberFormatException: unable to parse '' as integer

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

at android.app.ActivityThread.access$2300(ActivityThread.java:125)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:123)

at android.app.ActivityThread.main(ActivityThread.java:4627)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:521)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.NumberFormatException: unable to parse '' as integer

at java.lang.Integer.parseInt(Integer.java:412)

at java.lang.Integer.parseInt(Integer.java:382)

at inno.games.BillardScoreboardActivity.onCreate(BillardScoreboardActivity.java:35)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

... 11 more

First off, i'll post a picture to make it easier for you guys to understand.

enter image description here

As you can see, there's an EditText and a button.
I want the button to save the contents of the EditText into a string + start a new activity.

In the next activity, i then want to convert the string into an Integer.

This is my current code:

SENDER ACTIVITY

        startscore = (EditText) findViewById(R.id.startscore);

proceed = (Button) findViewById(R.id.bProceed);
proceed.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        Intent myIntent = new Intent(Introscreen.this, BillardScoreboardActivity.class);
        String s = startscore.getText().toString();
        Bundle b = new Bundle();
        b.putString("lol", s);
        //put into your intent
        myIntent.putExtras(b);
        Introscreen.this.startActivity(myIntent);

    }
}); 
}

RECEIVER ACTIVITY

        int counter1, counter2, counter3, counter4, counter5;

            oncreate.....{

            Bundle b = getIntent().getExtras();
    String s = b.getString("lol");




    column1tv = (TextView) findViewById(R.id.column1text);
    column2tv = (TextView) findViewById(R.id.column2text);
    column3tv = (TextView) findViewById(R.id.column3text);
    column4tv = (TextView) findViewById(R.id.column4text);
    column5tv = (TextView) findViewById(R.id.column5text);

    column1tv.setText(counter1);
    column2tv.setText(counter2);
    column3tv.setText(counter3);
    column4tv.setText(counter4);
    column5tv.setText(counter5);

Hope you can help me troubleshooting it, to figure out the problem.

The problem:

Upon clicking the button, it shuts down the application and gives me those error codes:

02-23 15:01:24.136: E/AndroidRuntime(295): FATAL EXCEPTION: main

java.lang.RuntimeException: Unable to start activity
ComponentInfo{inno.games/inno.games.BillardScoreboardActivity}:
java.lang.NumberFormatException: unable to parse '' as integer

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

at android.app.ActivityThread.access$2300(ActivityThread.java:125)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:123)

at android.app.ActivityThread.main(ActivityThread.java:4627)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:521)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.NumberFormatException: unable to parse '' as integer

at java.lang.Integer.parseInt(Integer.java:412)

at java.lang.Integer.parseInt(Integer.java:382)

at inno.games.BillardScoreboardActivity.onCreate(BillardScoreboardActivity.java:35)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

... 11 more

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2025-01-15 14:21:20

尝试将空字符串转换为整数时它会中断。将字符串传递到下一个活动时出现问题。尝试通过捆绑传递额外的字符串:

Bundle b = new Bundle();
b.putString("key", string);
//put into your intent
yourIntent.putExtras(b);

然后在下一个活动中获取它:

Bundle b = getIntent().getExtras();
String s = b.getString("key");

您还需要切换 TextView 的声明以及设置它们的时间。如果尚未声明,则无法设置它们。

编辑:还有一件事。我假设您想要在该 EditText 中输入整数。如果还没有的话,您应该在其上设置 inputType。

编辑2:不要觉得自己很落后,我们都曾经是初学者!首先,尝试使用捆绑包,看看您在 EditText 中输入的内容是否正确传递到下一个活动。

其次,在 XML 文件中设置 EditTextinputType。应该类似于 android:inputType="number"

声明如下:

column1tv = (TextView) findViewById(R.id.column1text);

您正在为 TextView 创建一个对象。然后,您可以在此处设置它们:

column1tv.setText(counter1);

您必须先创建并实例化该对象,然后才能对其执行任何操作。

编辑3:
好的,从您发布的屏幕截图中,我收集了以下内容:

首先-您正在创建对象counter1、counter2等..,但您从未使用实例化它们任何事物。这可能是您收到最新异常的地方。

第二-您会收到关于 String s = b.getString("lol"); 的警告,因为它是一个未使用的局部变量。 (检查 Eclipse 中的问题选项卡,您将看到代码中存在哪些警告/问题)。您没有对从上一个活动传递的字符串值执行任何操作。

It's breaking while trying to convert an empty string to an integer. Something is wrong while passing your string to the next activity. Try passing your string extra through a bundle instead:

Bundle b = new Bundle();
b.putString("key", string);
//put into your intent
yourIntent.putExtras(b);

Then get it in your next activity:

Bundle b = getIntent().getExtras();
String s = b.getString("key");

You also do need to switch your declarations of your TextViews and when you set them. You can't set them if they are not yet declared.

Edit: One more thing. I'm assuming you only want integers typed into that EditText. You should set the inputType on it, if you haven't already.

Edit 2: Don't feel retarded, we were all beginners at one point! Firstly, try using a bundle and see if what you type into your EditText is properly passed to the next activity.

Second, you set the inputType for the EditText in your XML file. Should be something like android:inputType="number".

The declarations are these lines:

column1tv = (TextView) findViewById(R.id.column1text);

You're creating an object for your TextViews. Then, you set them here:

column1tv.setText(counter1);

You must create and instantiate the object before you can do anything with it.

Edit 3:
Okay, from the screenshot you posted, I gathered the following:

First- You're creating the objects counter1, counter2, etc.. but you're never instantiating them with anything. Which is probably where you're getting your latest exception.

Second- You're getting the warning on the String s = b.getString("lol"); because it's an unused local variable. (Check the problems tab in Eclipse and you'll see what the warnings/problems you have in your code). You're not doing anything with the string value that was passed from the previous activity.

著墨染雨君画夕 2025-01-15 14:21:20

尝试更改column1tv.setText(...) 和column1tv = (TextView) findViewById(...) 的顺序。

// Assign new View to columnt1tv
column1tv = (TextView) findViewById(R.id.column1text); 

// change its appearance
column1tv.setText(counter1); 

此外,您还想设置一个 R.id 数组,以便可以循环这些重复的构造。

for( ... )
   columnTv[i] = (TextView) findByView( R_columnText[i] );

使用数组简短示例进行简化。

class TheActivity extends Activity
{
    // Resource group id's
    private int R_columnTextIds[]={ R.id.column1text, R.id.column2text, 
               R.id.column3text, R.id.column4text};
    TextView columnTv[]= new TextView[4];
    // Now you can use loops.
    onCreate.... {
       .. get stuff and counter1
       for(int i = 0; i< columnTv.length; i++){
          columnTv[i] = (TextView) findByView( R_columnTextIds[i] );
          columnTv[i].setText( counter1 );
       }

Try changing the order of the column1tv.setText(...) and the column1tv = (TextView) findViewById(...).

// Assign new View to columnt1tv
column1tv = (TextView) findViewById(R.id.column1text); 

// change its appearance
column1tv.setText(counter1); 

Also you want to setup an array of R.id's so you can loop through these repetitive constructs.

for( ... )
   columnTv[i] = (TextView) findByView( R_columnText[i] );

Simplification with arrays short sample.

class TheActivity extends Activity
{
    // Resource group id's
    private int R_columnTextIds[]={ R.id.column1text, R.id.column2text, 
               R.id.column3text, R.id.column4text};
    TextView columnTv[]= new TextView[4];
    // Now you can use loops.
    onCreate.... {
       .. get stuff and counter1
       for(int i = 0; i< columnTv.length; i++){
          columnTv[i] = (TextView) findByView( R_columnTextIds[i] );
          columnTv[i].setText( counter1 );
       }
美人如玉 2025-01-15 14:21:20

您的字符串肯定在 Intent 中正确传达,因为否则如果该名称值,i.getStringExtra("lol") 将返回 null对不存在。 (顺便说一句,在尝试从字符串中解析整数之前,检查得到的结果是否为 null 是一个非常好的主意)。然而,问题似乎在于您传递的字符串实际上是一个零长度字符串,即“”,这意味着在尝试解析整数时 parseInt() 失败它的。

看来您需要做的是使 sfinal,并在触发 Intent 之前将其添加到按钮侦听器中:

s = startscore.getText().toString();

Your string is definitely being conveyed within the Intent properly, because otherwise i.getStringExtra("lol") would return null if that name-value pair didn't exist. (It would be a very good idea for you to check whether the result you get is null, by the way, before attempting to parse an integer from the string). However, what seems to be the problem is that the string you're passing is actually a zero-length string, i.e. "", which means that parseInt() falls over when attempting to parse an integer out of it.

It seems that what you need to do is make s non-final, and add this into your button listener just before the Intent is fired:

s = startscore.getText().toString();
只为一人 2025-01-15 14:21:20

startscore.getText().toString() 可能为空时(就在获取 EditText 之后),您会得到它。所以,您发送的字符串是“”。尝试在之前获取它

myIntent.putExtra("lol", s);

之后,在接收活动中,您可以进行测试,例如:

if(var != null && !TextUtils.isEmpty(var)) {
    // deal with var
}

然后,您必须在 Integer.parseInt() 因为它可以抛出 NumberFormatException 如果字符串不是数字 (这是你的情况,因为 var 是“”)。

You get the startscore.getText().toString() when it's probably empty (just after getting the EditText). So, the String you send is "". Try to get it just before

myIntent.putExtra("lol", s);

After, in receiving activity, you can test such as:

if(var != null && !TextUtils.isEmpty(var)) {
    // deal with var
}

Then, you must try/catch operation on Integer.parseInt() because it can throw a NumberFormatException if the String is not a number (it's your case because var is "").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文