-4

我有一个class不扩展的Activity. 在那堂课中,我使用一个try-catch块来捕捉exceptions. 如何将Exception e在该块中捕获的任何异常()传递给另一个Activity?我的应用程序使用 a 检查boolean flag以显示toast message. 但是我如何将此消息传递给另一个活动?

public class AgAppHelperMethods   {

  private static AgAppHelperMethods instance = null;
  static boolean flag=true;

 public static   String[][] AgAppXMLParser( String parUrl) {



    String _node,_element;
    String[][] xmlRespone = null;
    try {
  String url = www.xyz.com
            URL finalUrl = new URL(url);


    catch (Exception e)
    {
      flag=false;
      Log.e( "CONNECTION ERROR  SERVER NOT RESPONDING", e);
    } 


          public class LoginScreen extends Activity implements Serializable {


public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.agapplogin);


       btnClear.setOnClickListener(new OnClickListener() {
        public void onClick(View v)

        {
            postLoginData();
        }

    });


           public void postLoginData()

{
               xmlRespone = AgAppHelperMethods.AgAppXMLParser( url);

        if(!AgAppHelperMethods.flag)
        {
             Toast.makeText(getApplicationContext(), "Error  server 
    not responding " , Toast.LENGTH_SHORT).show();
             myProgressDialog.dismiss();
        }
4

2 回答 2

2
 catch (Exception e) {        
    flag=false;
    Log.e( "CONNECTION ERROR  SERVER NOT RESPONDING", e);

    String theException = e.getMessage();

    Bundle b = new Bundle();        
    b.putString("Exception", theException); //or you can replace theException with a custom message

    Intent nextActivity = new Intent(MyActivity.this, NewActivity.class);
    nextActivity.putExtras(b);
    startActivity(nextActivity); 
    }   

onCreate您可以在下一个活动的方法中收到它

    Bundle b = getIntent().getExtras();
    String error = b.getString("Exception");
    Toast.makeText(NewActivity.this, error, Toast.LENGTH_SHORT).show();

希望这可以帮助!!

于 2012-09-19T12:27:17.880 回答
0

使用 Intent extras 将额外数据传递给另一个 Activity:

请参阅此处:如何从 Android 上的意图中获取额外数据? 在这里:http ://www.vogella.com/articles/AndroidIntent/article.html

于 2012-09-19T12:14:21.643 回答