折腾了一天,百度了各种方法都不奏效.怎么办呢
新建了个测试项目,在里边儿丢进去如下代码
这是窗体1
package com.example.activityonresult; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{ Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void onClick(View v) { // TODO Auto-generated method stub if(v==btn) { Log.d("主窗体按钮","按下触发"); Intent intt=new Intent(MainActivity.this,Activity02.class); intt.putExtra("info1", "Ifff"); startActivityForResult(intt,1); } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("GetResult02", data.getStringExtra("info2")); Integer ss=data.getIntExtra("info3", 444); String sdf=ss.toString(); Log.d("GetResult02",sdf); Boolean sa= data.getBooleanExtra("info4", false); Log.d("GetResult02", sa.toString()); btn.setText(data.getStringExtra("info2")); } }
这是窗体2
package com.example.activityonresult; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Activity02 extends Activity implements OnClickListener{ Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void onClick(View v) { // TODO Auto-generated method stub Intent intent =new Intent(Activity02.this,MainActivity.class); intent.putExtra("info2", "123"); intent.putExtra("info3", 1244); intent.putExtra("info4", true); setResult(33,intent); finish(); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("触发result02","获取结果"); Log.d("GetResultMain", data.getStringExtra("info1")); btn.setText(data.getStringExtra("info1")); } }
试了试终于触发了...但是很遗憾只触发了窗体1的onActivityResult
半天想不明白怎么回事,于是回到之前的测试项目中检查了一下代码,修修改改一些不着边际的东西
于是我想既然穿不回来,把参数传给第二个窗体怎么样,于是我在菜单中添加了如下代码
public boolean onMenuItemSelected(int featureId, MenuItem item) { Intent it=new Intent(TestProc.this,Cfg.class); it.putExtra("IPaddress", IPaddress); it.putExtra("Port", Port); it.putExtra("IsTcp", IsTcp); switch (item.getItemId()) { case 1: startActivityForResult(it,34); break; default: break; } // TODO Auto-generated method stub return super.onMenuItemSelected(featureId, item); }
嘿,成了,第二个窗体触发了onActivityResult,兴奋至极...于是翻开第二个窗体中的按钮事件
public void onClick(View v) { // TODO Auto-generated method stub //Bundle bundle = new Bundle(); //bundle.putInt("Port", resPort); //intent.putExtras(bundle); Intent intent = new Intent(); string ipaddr=TxtipAddr.getText().toString(); intent.putExtra("IP", ipaddr); int Port=Integer.parseInt(TxtPort.getText().toString()); intent.putExtra("Port", Port); if(rbtntcp.isChecked()){ Log.d("设置结果", "设置tcp"); intent.putExtra("IsTcp", true);} else{ Log.d("设置结果", "设置udp"); intent.putExtra("IsTcp", false);} Cfg.this.setResult(1, intent);//返回参数 Log.d("设置结果", "设置结束"); Cfg.this.finish();//关闭Activity }
发现三处不同的地方
第一,没有为Intent设置class
第二,在声明Intent后执行了些其它代码
第三,用的变量类型不一样
于是改了改代码:
public void onClick(View v) { // TODO Auto-generated method stub //Bundle bundle = new Bundle(); //bundle.putInt("Port", resPort); //intent.putExtras(bundle); String ipaddr=TxtipAddr.getText().toString(); Boolean IsTcp=rbtntcp.isChecked(); Integer Port=Integer.parseInt(TxtPort.getText().toString()); Intent intent = new Intent(Cfg.this,TestProc.class); intent.putExtra("IP", ipaddr); intent.putExtra("Port", Port); intent.putExtra("IsTcp", IsTcp); Cfg.this.setResult(642, intent);//返回参数 Log.d("设置结果", "设置结束"); Cfg.this.finish();//关闭Activity }
窗体一中的onActivityResult成功执行! 大功告成...
等等...窗体二中的onActivityResult又不执行了.....买了个表....