嗨,我在eclipse中编写的代码如下:
在第一个活动中,它应弹出一条包含两个数字之和的消息。 除了显示消息外,每件事都是okk,它没有出现。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
startActivityForResult(new Intent("co.example.SECONDACTIVITY"),request_code);
}
public void onActivityResult(int result,int requestcode,Intent data)
{
if(requestcode == request_code)
{
if(result == RESULT_OK)
{
Toast.makeText(this,data.getData().toString(),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;
}
}
第二项活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondactivity);
}
public void onClick(View view){
EditText b1 = (EditText) findViewById(R.id.editText1);
EditText b2 = (EditText) findViewById(R.id.editText2);
int x =Integer.parseInt(b1.getText().toString())+ Integer.parseInt(b2.getText().toString());
String s = Integer.toString(x);
Intent data = new Intent();
data.setData(Uri.parse(s));
setResult(RESULT_OK,data);
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.secondactivity, menu);
return true;
}
}
答案 0 :(得分:1)
试试这个..
我认为你搞砸了参数。您错误地比较了参数。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestcode == request_code)
{
if(resultCode == RESULT_OK)
{
Toast.makeText(this,data.getData().toString(),Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:0)
首先确保您的结果代码相同并且如果它带有相同的请求代码,您只需按此打印并尝试打印您的数据..在您的Toast中...... 并增加你的Toast时间
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestcode == request_code)
{
if(resultCode == RESULT_OK)
{
Toast.makeText(ActivityName.this,data.getData().toString(),Toast.LENGTH_LONG).show();
}
}
}