简体   繁体   English

如何将意图数据设置为当前活动editText?

[英]How do I set intent data to current activity editText?

I'm trying to transfer two numerical inputs from one activity to another's UI but when I click the button to change intent it crashes. 我试图将两个数字输入从一个活动转移到另一个活动的UI,但是当我单击按钮更改意图时,它崩溃了。

I get the following in logcat: http://pastebin.com/zkWPcSNZ , which suggests a problem with the way I parsed the data to the editTexts in CalcResult. 我在logcat中得到以下内容: http ://pastebin.com/zkWPcSNZ,这表明我将数据解析为CalcResult中的editTexts的方式存在问题。

My question is what is wrong with the way I'm trying to pass the data to the CalcResult editText.Is there an alternative method of acheiving this? 我的问题是我尝试将数据传递给CalcResult editText的方式有什么问题吗?

My two classes look like this for reference: 我的两个类如下所示:

public class MainActivity extends Activity implements OnClickListener {

    //variables for xml objects
    EditText offsetLength,offsetDepth,ductDepth;
    Button calculate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //setting the variables to the xml id's and setting the click listener on the calc button
        offsetLength = (EditText)findViewById(R.id.offLength);
        offsetDepth = (EditText)findViewById(R.id.offDepth);
        ductDepth = (EditText)findViewById(R.id.ductDepth);
        calculate = (Button)findViewById(R.id.calc);
        calculate.setOnClickListener(this);//don't cast the listener to OnClickListener


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        try {

            String getoffsetlength = offsetLength.getText().toString(); 
            String getoffsetdepth = offsetDepth.getText().toString(); 
            String getductdepth = ductDepth.getText().toString(); 

            double tri1,tri2;
            double marking1,marking2;

            double off1 = Double.parseDouble(getoffsetlength);
            double off2 = Double.parseDouble(getoffsetdepth);
            double off3 = Double.parseDouble(getductdepth)
                    ;
            marking1 = Math.pow(off1,2) + Math.pow(off2,2);
            tri1 = (float)off2/(float)off1;
            tri2 = (float)off3/Math.atan((float)tri1);
            marking2 = (float)off3/Math.atan(tri2);


            Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
            myIntent.putExtra("numbers", marking1);
            myIntent.putExtra("numbers", marking2);

            startActivity(myIntent);


        } catch (NumberFormatException e) {
            // TODO: handle exception
            System.out.println("Must enter a numeric value!");

        }

    }


}

This is the activity that I'm passing the data to: 这是我要将数据传递到的活动:

public class CalcResult extends MainActivity
{
    EditText result1,result2;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        result1 = (EditText)findViewById(R.id.mark1);
        result2 = (EditText)findViewById(R.id.mark2);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        double mark1 = bundle.getDouble("number1");
        double mark2 = bundle.getDouble("number2");

        int a = Integer.valueOf(result1.getText().toString());
        int b = Integer.valueOf(result2.getText().toString());

        result1.setText(a + "  ");
        result2.setText(b + "  ");

    }

}

When you start your second activity, both editText are empty 当您开始第二个活动时,两个editText都为空

So when you do : 因此,当您这样做时:

int a = Integer.valueOf(result1.getText().toString());
int b = Integer.valueOf(result2.getText().toString());

it's equivalent to : 它等效于:

int a = Integer.valueOf("");
int b = Integer.valueOf("");

which throws the exception 引发异常

Caused by: java.lang.NumberFormatException: Invalid int: ""

If you want to set them the values you passed through both activities, you can just do : 如果要为它们设置通过两个活动传递的值,则可以执行以下操作:

double mark1 = bundle.getDouble("number1");
double mark2 = bundle.getDouble("number2");

result1.setText(mark1 + "  ");
result2.setText(mark2 + "  ");

The error is that you are putting your extras with same key " numbers " , and you are trying to retreive them by another keys "number1" and "number2". 错误是您要使用相同的键“ numbers ”放置临时演员,并尝试通过另一个键“ number1”和“ number2”取回它们。 your code should be like this : 您的代码应如下所示:

Intent myIntent = new Intent(MainActivity.this, CalcResult.class);
myIntent.putExtra("number1", marking1);
myIntent.putExtra("number2", marking2);

and to retreive them you should use : 要使它们恢复原状,您应该使用:

Intent intent = getIntent();
double mark1 = intent.getDoubleExtra("number1", 0);
double mark2 = intent.getDoubleExtra("number2", 0);

And then , set the variables on your EditTexts like this : 然后,像这样在EditTexts上设置变量:

result1 = (EditText)findViewById(R.id.mark1);
result2 = (EditText)findViewById(R.id.mark2);
result1.setText(mark1+"");
result2.setText(mark2+"");

您在发送意向书时使用了相同的名称作为附加内容,请尝试对其进行更正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将相机意图设置为主要活动 - How do I set the Camera Intent as the Main Activity 如何通过意图将数据从当前活动发送到新活动? - How to send data to new activity from current activity via intent? 如何设置一个意图,以便我可以将数据发送到第二个活动,然后从那里发送到第三个活动? - How to set an intent so that I can send a data to the second activity and from there to the third activity? 如何将我在EditText框中键入的数据添加到要在另一个活动中列出的数组中? - How do I add data I have keyed in a EditText box into an array to list in another activity? 如何获得使用意图打开的活动的参考? - How do I get the reference of an activity I opened using intent? 如何在Android中意图动态生成的EditText数据? - How to Intent Dynamically generated EditText data in Android? 如何使用意图将ArrayList传递给另一个Activity? - How do I pass ArrayList to another Activity using intent? 如何将意图从活动传递给扩展LinearLayout的类 - How do I pass an intent from an activity to a class extending LinearLayout 如何使用arrayAdapter中的intent进入第三个活动? - How do I use intent from arrayAdapter to go into a third activity? 单击按钮后,如何将文本从alertdialog的EditText复制并粘贴到活动的EditText中? - How do I copy and paste text from EditText of alertdialog to EditText of my activity, on button click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM