我只是想从另一个活动返回带有intent的字符串,但是没有在父活动中获取该字符串...虽然没有显示错误。 出于测试目的,只想在返回结果时显示吐司信息。
这是父母活动的代码......
public class Login2 extends Activity {
static final int REQ_CODE=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button startButton=(Button) findViewById(R.id.btn_login);
startButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
final TextView var=(TextView)findViewById(R.id.errmsg);
EditText usrnameobj = (EditText)findViewById(R.id.et_usename);
EditText usrpassobj = (EditText)findViewById(R.id.et_password);
String username = usrnameobj.getText().toString();
String password = usrpassobj.getText().toString();
Intent i=new Intent(getApplicationContext(),AndroidXmlResource.class);
i.putExtra("entry",username.toString() + password.toString());
startActivityForResult(i,REQ_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//if (resultCode == Activity.RESULT_OK && requestCode == 0)
{
//String res = data.getExtras().getString("result");
// TextView var=(TextView)findViewById(R.id.errmsg);
// var.setVisibility(0);
// var.setText(res.toString());
// var.setText("called on act res");
Toast.makeText(Login2.this,"Hi! Bright Hub", Toast.LENGTH_SHORT).show();
}
}
});
这是返回结果的子活动....
public class AndroidXmlResource extends Activity {
//public String U="sudeep1";
//public String P="donotlock1";
public int Res;
@Override
protected void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
//setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String pass=extras.getString("entry");
//String result= new String();
//TextView myXmlContent = (TextView)findViewById(R.id.tv_test);
try {
String stringXmlContent = getEventsFromAnXML(this);
//myXmlContent.setText(pass.toString());
Intent returnIntent = new Intent();
if(stringXmlContent.equals(pass))
{
returnIntent.putExtra("result","valid Login");
setResult(RESULT_OK,returnIntent);
super.finish();
}
else
{
returnIntent.putExtra("result","Invalid Login");
setResult(RESULT_OK,returnIntent);
super.finish();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String getEventsFromAnXML(Activity activity)
throws XmlPullParserException, IOException
{
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.myxml);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
int c=1;
if(eventType == XmlPullParser.START_DOCUMENT)
{
//stringBuffer.append("--- Start XML ---");
}
else if(eventType == XmlPullParser.START_TAG)
{
//stringBuffer.append("\nSTART_TAG: "+xpp.getName());
}
else if(eventType == XmlPullParser.END_TAG)
{
//stringBuffer.append("\nEND_TAG: "+xpp.getName());
}
else if(eventType == XmlPullParser.TEXT)
{
//stringBuffer.append("\nTEXT: "+xpp.getText());
//extra
{ String t=xpp.getText().toString();
stringBuffer.append(t.toString());
}
}
eventType = xpp.next();
}
//stringBuffer.append("\n--- End XML ---");
return stringBuffer.toString();
答案 0 :(得分:2)
确保元素没有android:noHistory =“true”属性。
答案 1 :(得分:1)
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK && requestCode == REQ_CODE)
{
String res = data.getExtras().getString("result");
Toast.makeText(Login2.this,"Hi! Bright Hub: "+res, Toast.LENGTH_SHORT).show();
}
}
你已经在使用它。
super.finish()
做了什么?
为什么不this.finish()
??
答案 2 :(得分:1)
是否可以将@Override onActivityResult()
和另一个放入setResult(Activity.RESULT_OK,intent)
?
在儿童活动中确保这一点。
答案 3 :(得分:1)
请确保您尚未在 android:launchMode="singleInstance|singleTask"
中添加此行 menifest.xml
,并在此处宣布您的活动。如果您已添加,请删除这是为了在onActivityResult()
上获得结果。
答案 4 :(得分:0)
我遇到了类似的问题,我发现的解决方案是通过将结果存储到共享首选项中并稍后在onResume中加载来替换startActivityForResult功能。查看完整的讨论here。