Android - 我正在使用意图在两个活动之间进行通信。我在调用活动中使用了 putExtra(),并且在被调用活动中使用了 getExtras。但问题是我无法使用我在意图中检索到的数字设置编辑文本。下面是我的程序`
// 调用活动
package com.example.androidtutorial2;
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.EditText;
import android.text.InputType;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//EDIT TEXT
final EditText sharedata = (EditText) findViewById ( R.id.editText1 ) ;
sharedata.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//BUTTON
Button sendbutton = (Button) findViewById (R.id.button2) ;
sendbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int edittext_data = Integer.valueOf(sharedata.getText().toString());
Intent sendintent = new Intent(MainActivity.this,second_activity.class);
sendintent.putExtra("somedata", edittext_data);
startActivity(sendintent);
}
});
// Button button_to_call_activity = (Button) findViewById(R.id.button1);
// button_to_call_activity.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
//
//
// }
// });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
//CALLED ACTIVITY
package com.example.androidtutorial2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;
public class second_activity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
EditText displaymessage = (EditText) findViewById ( R.id.editText1 );
Intent intent1 = getIntent();
Bundle bundle1 = intent1.getExtras();
int integer1 = bundle1.getInt("somedata");
Integer string1 = integer1;
displaymessage.setText(integer1); // HERE IS A RUNTIME ERROR
}
}