转移一个字符串,新活动+字符串转换为整数字符串、整数、转换为

2023-09-12 22:17:28 作者:戏听清影横笛

首先,我会发布图片,使其更容易为你们理解。

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

正如你所看到的,有一个EditText和一个按钮。 我想要的按钮的EditText的内容保存到一个字符串+启动一个新的活动。

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.

这是我目前的code:

This is my current code:

发件人活动

        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);

    }
}); 
}

接收器的工作

        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.

的问题:

一旦点击此按钮,它关闭应用程序,并给了我这些错误codeS:

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

推荐答案

它打破,而试图为空字符串转换为整数。什么是错的,当通过你的字符串到下一个活动。尝试通过你的字符串额外通过捆绑来代替:

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");

您也确实需要切换你TextViews你的声明,当你进行设置。你不能设置他们,如果他们还没有宣布。

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.

编辑:还有一件事。我假设你的 仅 的要输入到该的EditText整数。你应该设置inputType就可以了,如果你还没有。

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.

编辑2:不要觉得迟钝,我们都是初学者在一个点!首先,请尝试使用一个包,看看有什么你输入到你的EditText正确传递到下一个活动。

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.

二,将 inputType 的EditText 在XML文件中。如果是这样的安卓inputType =号

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

该声明是这些行:

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

您正在创建一个对象,你TextViews。然后,你在这里设置它们:

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.

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

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

第一 - 您正在创建的对象计数器1,计数器等。,但你永远实例他们任何东西。这可能是在那里你得到你的最新异常。

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.

二 - 你得到的的String = b.getString(笑)警告; ,因为它是一个未使用的本地变量。 (请在Eclipse中的问题选项卡,你会看到你在你的code什么的警告/问题)。你不这样做与从previous活动传递的字符串值的东西。

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.