简体   繁体   English

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

[英]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.我在 oncreate 方法中添加了这些方法。 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 .在 OnSuccess 时的代码中,必须调用方法,但只调用 syncDB(),sync() 并且不调用 rest 。 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.这是因为在sync()方法中或可能在subsync()方法的开头,您的程序抛出了错误。 Because of the try/catch block, you are allowing the program to continue.由于 try/catch 块,您允许程序继续。

Check the method for an error and fix that.检查方法是否有错误并修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中的onCreate方法中调用onCreate方法 - Calling a onCreate method inside onCreate method in android Android Studio的多个onCreate方法 - Android Studio multiple onCreate methods Android Studio MainActivity.java 中的多个 onCreate 方法 - Android Studio Multiple onCreate methods in MainActivity.java 在Android上的“ onCreate”方法中的问题 - issue in 'onCreate' method on android OnCreate方法中的Android NullPointerException - Android NullPointerException in OnCreate method onCreate() 方法不是调用方法 A 而是 onStart() 调用方法 A - onCreate() method is not calling method A but onStart() calls method A 在 main() 方法中调用多个方法(int) - Calling multiple methods(int) in main() method 即使在 oncreate 方法中没有调用它们,为什么 android 也会运行像“onItemSelected”这样的方法? - Why do android run methods like "onItemSelected" even if they are not called in the oncreate Method? 可以在Android Studio的onCreate方法中调用java文件中的其他方法吗? - Can other methods from a java file be called in the onCreate method in Android Studio? 我们可以调用在 MainActivity 中的同一类中声明的方法,而不是在 onCreate 或 android studio 中的其他方法中吗? - Can we call a method declared in same class in MainActivity not in onCreate or other methods in android studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM