在android中调用oncreate方法中的多个方法

时间:2015-07-30 08:26:13

标签: java android

启动活动时,我有多种方法可供调用。我在oncreate方法中添加了这些方法。问题是当活动开始时,某些方法被调用一些或不被调用。如何在活动开始时调用所有方法。

我的代码是

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     AsyncHttpClient client = new AsyncHttpClient();
     RequestParams params = new RequestParams();

     client.post("http://localhost/website/getdbrowcount.php",params ,new AsyncHttpResponseHandler()
     {

          public void onSuccess(String response) 
          {
              try
              {
                 Log.d("home", "success"); 
                 JSONObject obj = new JSONObject(response);
                 Log.d("home", obj.toString());
                System.out.println(obj.get("count"));      

                syncDB();
                sync();
                subsync();
                syncfeature();
                syncelec();
                syncconnector();
                synccontrols();
                synckeypad();
                syncmech();
                syncorder();
                syncpower();
              }

              catch (JSONException e) 
              {
             // TODO Auto-generated catch block
             e.printStackTrace();
             }
          }

          public void onFailure(int statusCode, Throwable error,String content) 
          {
              if(statusCode == 404)
              {
                update.setText("The update has been cancelled. Please update via Settings to work"
                            + " with latest Sonetonix product data");
                  Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                  btn1.setEnabled(true);
                  btn1.setTextColor(Color.parseColor("#FFFFFF"));
              }
              else if(statusCode == 500)
              {
                update.setText("The update has been cancelled. Please update via Settings to work"
                            + " with latest Sonetonix product data");
                  Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                  btn1.setEnabled(true);
                  btn1.setTextColor(Color.parseColor("#FFFFFF"));
              }
              else
              {
                update.setText("The update has been cancelled. Please update via Settings to work"
                            + " with latest Sonetonix product data");
                  Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]", Toast.LENGTH_LONG).show();
                  btn1.setEnabled(true);
                  btn1.setTextColor(Color.parseColor("#FFFFFF"));
              }
              Log.d("home", "failure");
          }
     });       
}

在OnSuccess的代码中,必须调用方法,但只调用syncDB(),调用sync(),不调用rest。我应该在代码中做出哪些更改来解决此问题。

请帮忙

1 个答案:

答案 0 :(得分:0)

这是因为在sync()方法或可能在subsync()方法开始时,您的程序会抛出错误。由于try / catch块,您允许程序继续。

检查方法是否有错误并解决该问题。