简体   繁体   中英

calling multiple methods in oncreate method in android

I have multiple methods to be called when activity is started. I have added those methods in the oncreate method. The problem is when the activity is started some methods are called some or not called. How do i call all the methods when the activity is started.

My code is

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");
          }
     });       
}

In the code when OnSuccess the methods has to be called but only syncDB(),sync() is called and rest are not called . What change should i make in the code to resolve this issue.

Please help

It is because in the sync() method or possibly at the start of the subsync() method your program is throwing an error. Because of the try/catch block, you are allowing the program to continue.

Check the method for an error and fix that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM