onActivityResult不起作用,apk崩溃了

时间:2014-03-05 18:47:11

标签: android apk onactivityresult

我正在尝试编写一个简单的注释apk并卡在startActivityForResultonActivityResult中。

这是代码

public class MainActivity extends ListActivity {

//Global variables to use later
private ArrayList<Note> note;
private ArrayAdapter<Note> adapter;
private Intent intent;

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

    note = new ArrayList<Note>() ;
    adapter = new ArrayAdapter<Note>(this, R.layout.edit , note);

    Button btn = (Button) findViewById(R.id.btn_edit);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            intent = new Intent(MainActivity.this,Edit.class);
            startActivityForResult(intent, 12);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

            note.add(new Note(data.getStringExtra("subject"), data.getStringExtra("details"))); 

}

第二个是编辑页面:

public class Edit extends Activity{

private Intent recieve;
EditText subject;
EditText details;
String subject_text;
String subject_details;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit);

    recieve = getIntent();

    subject = (EditText) findViewById(R.id.enter_subject);
    details = (EditText) findViewById(R.id.enter_details);

    Button ok = (Button) findViewById(R.id.ok_btn);
    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            subject_text = subject.getText().toString();
            subject_details = subject.getText().toString();

            recieve.putExtra("subject", subject_text);
            recieve.putExtra("details", subject_details);
            setResult(RESULT_OK, recieve);
            finish();

        }
    });
}

}

Note本身是一个带有2个变量的java类(私有String主题;     私有String详细信息;),构造函数,getter和setter。

是否会崩溃并且没有添加新的Note?

1 个答案:

答案 0 :(得分:0)

尝试编写logcat的输出。

也尝试写这一行:

super.onActivityResult(requestCode, resultCode, data);

后:

note.add(new Note(data.getStringExtra("subject"), data.getStringExtra("details")));