在活动之间传递数据的运行时错误。 Android的

时间:2014-08-17 13:13:04

标签: java android

我真的无法弄清楚这里的问题是什么,我做的几乎是我从网上某个地方得到的同样的例子,但是当我工作时,这个报告了一个运行时错误单击按钮切换到secondActivity。但在我首先设置onActivityResult并在第二个活动中发送结果之前,它已经切换正常。

它的代码有点长,但它并不复杂。在第一个活动中,您单击按钮转到第二个活动,然后您选择两个数字,这些数字存储在对象中并通过意图发送回第一个活动,而在textview的第一个活动中,您将获得这些数字的总和。

MainActivity

package com.example.parcelablevezba4;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    TextView tv1;
    Button btn;

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

        tv1 = (TextView)findViewById(R.id.tv1);
        btn = (Button)findViewById(R.id.btn);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(i, 42);


            }
        });

    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode == 42){
            if(resultCode == RESULT_OK){
                Object obj2 = data.getParcelableExtra("obje");
                int total = obj2.getFirstSummand() + obj2.getSecondSummand();
                tv1.setText(obj2.getFirstSummand()+"+"+obj2.getSecondSummand()+"is "+total);
            }else if(resultCode == RESULT_CANCELED){
                Toast.makeText(getApplicationContext(), "CANCELED", Toast.LENGTH_SHORT).show();
            }
        }


    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

SecondActivity

package com.example.parcelablevezba4;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;

public class SecondActivity extends Activity {

    EditText et1;
    EditText et2;
    Button btnOk;

    int firstSummand;
    int secondSummand;

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

        et1 = (EditText)findViewById(R.id.etFirst);
        et2 = (EditText)findViewById(R.id.etSecond);
        btnOk = (Button)findViewById(R.id.btnOk);

        firstSummand = Integer.parseInt(et1.getText().toString());
        secondSummand = Integer.parseInt(et2.getText().toString());

        btnOk.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent resultIntent = new Intent();
                Object obj = new Object();

                obj.setFirstSummand(firstSummand);
                obj.setSecondSummand(secondSummand);


                resultIntent.putExtra("obje", obj);
                setResult(RESULT_OK,resultIntent);
                finish();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }

}

对象类

package com.example.parcelablevezba4;

import android.os.Parcel;
import android.os.Parcelable;

public class Object implements Parcelable {
 private int firstSummand;
 private int secondSummand;

 public Object(){}

 public Object(Parcel p){
     this.firstSummand; = p.readInt();
     this.secondSummand; = p.readInt();
 }


 public int getFirstSummand(){
     return this.firstSummand;
 }

 public int getSecondSummand(){
     return this.secondSummand;;
 }

 public void setFirstSummand(int f){
     this.firstSummand = f;
 }

 public void setSecondSummand(int s){
     this.secondSummand; = s;
 }

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel p, int flag) {

    p.writeInt(firstSummand);
    p.writeInt(secondSummand);

}

public static Parcelable.Creator<Object> CREATOR = new Parcelable.Creator<Object>() {

    @Override
    public Object createFromParcel(Parcel source) {
        // TODO Auto-generated method stub
        return new Object(source);
    }

    @Override
    public Object[] newArray(int size) {
        // TODO Auto-generated method stub
        return new Object[size];
    }
};

}

我用我的语言翻译了这个,所以如果我在某个地方输错了那就不好了。

错误

08-17 13:20:22.933: E/AndroidRuntime(743): FATAL EXCEPTION: main
08-17 13:20:22.933: E/AndroidRuntime(743): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.parcelablevezba4/com.example.parcelablevezba4.SecondActivity}: java.lang.NumberFormatException: unable to parse '' as integer
08-17 13:20:22.933: E/AndroidRuntime(743):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-17 13:20:22.933: E/AndroidRuntime(743):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-17 13:20:22.933: E/AndroidRuntime(743):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-17 13:20:22.933: E/AndroidRuntime(743):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-17 13:20:22.933: E/AndroidRuntime(743):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 13:20:22.933: E/AndroidRuntime(743):  at android.os.Looper.loop(Looper.java:123)
08-17 13:20:22.933: E/AndroidRuntime(743):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-17 13:20:22.933: E/AndroidRuntime(743):  at java.lang.reflect.Method.invokeNative(Native Method)
08-17 13:20:22.933: E/AndroidRuntime(743):  at java.lang.reflect.Method.invoke(Method.java:507)
08-17 13:20:22.933: E/AndroidRuntime(743):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-17 13:20:22.933: E/AndroidRuntime(743):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-17 13:20:22.933: E/AndroidRuntime(743):  at dalvik.system.NativeStart.main(Native Method)
08-17 13:20:22.933: E/AndroidRuntime(743): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
08-17 13:20:22.933: E/AndroidRuntime(743):  at java.lang.Integer.parseInt(Integer.java:362)
08-17 13:20:22.933: E/AndroidRuntime(743):  at java.lang.Integer.parseInt(Integer.java:332)
08-17 13:20:22.933: E/AndroidRuntime(743):  at com.example.parcelablevezba4.SecondActivity.onCreate(SecondActivity.java:30)
08-17 13:20:22.933: E/AndroidRuntime(743):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-17 13:20:22.933: E/AndroidRuntime(743):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-17 13:20:22.933: E/AndroidRuntime(743):  ... 11 more

1 个答案:

答案 0 :(得分:1)

看起来你的EditText之一正在返回一个空字符串:

firstSummand = Integer.parseInt(et1.getText().toString());
secondSummand = Integer.parseInt(et2.getText().toString());

然后你尝试解析那个空字符串。

之前添加日志或甚至更好的检查:

String edit1 = et1.getText().toString();
String edit2 = et2.getText().toString();

Log.e("TAG", "First: "+edit1+" Second: "+edit2);

firstSummand = (edit1.isEmpty()) ? 0 : Integer.parseInt(edit1);
secondSummand = (edit2.isEmpty()) ? 0 : Integer.parseInt(edit2);