我正在尝试调试我的应用程序,它似乎为 MainActivity 运行了两次 onCreate 方法。
我输入了一个 Log.i() 命令,果然,它在 Logcat 中出现了两次。
这可能是什么原因?
这是我的 onCreate 代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Starting..."); // this appear twice in logcat
// Set activity view
setContentView(R.layout.activity_main);
// Initialise buttons
Button loginBtn = (Button) findViewById(R.id.login);
// Initialise text inputs
screen = (EditText)findViewById(R.id.screen);
pw = (EditText)findViewById(R.id.pw);
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String storedScreenCode = sharedPreferences.getString("storedScreenCode", null);
String storedScreenPassword = sharedPreferences.getString("storedScreenPassword", null);
domain = sharedPreferences.getString("apiUrl", null);
final String lastUpdated = sharedPreferences.getString("lastUpdated", null);
if(storedScreenCode != null) {
String url = domain + getResources().getString(R.string.api_login_url) + storedScreenCode + "/" + storedScreenPassword;
try {
status = new NetworkChecker(getApplicationContext()).execute().get(4, TimeUnit.SECONDS);
Log.i(TAG, status.toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
if(status) {
try {
String loginResult = new ReadJSONFeedTask().execute(url).get(4, TimeUnit.SECONDS);
JSONObject loginJson = new JSONObject(loginResult);
if(loginJson.getInt("success") == 1) {
Intent i = new Intent(getApplicationContext(), NextActivity.class);
startActivity(i);
} else {
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (isEmpty(screen) && isEmpty(pw)) {
screen.setError("Screen Code is required!");
pw.setError("Password is required!");
} else {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(!wifi.isWifiEnabled()) {
Toast.makeText(getBaseContext(), "Wifi is not enabled.", Toast.LENGTH_LONG).show();
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
} else if(!mWifi.isConnected()) {
Toast.makeText(getBaseContext(), "Wifi is not connected.", Toast.LENGTH_LONG).show();
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
} else {
//String domain = sharedPreferences.getString("apiUrl", null);
String url = domain + getResources().getString(R.string.api_login_url) + screen.getText().toString() + "/" + pw.getText().toString();
Log.i(TAG, "API Login URL: " + url);
try {
status = new NetworkChecker(getApplicationContext()).execute().get();
Log.i(TAG, "Status: " + status);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
if(status) {
try {
String loginResult = new ReadJSONFeedTask().execute(url).get();
JSONObject loginJson = new JSONObject(loginResult);
// check success boolean
if(loginJson.getInt("success") == 1) { // if success equals 1, carry on with activity
Intent i = new Intent(getApplicationContext(), NextActivity.class);
startActivity(i);
} else {
// if success equals 0, show toast
Toast.makeText(getBaseContext(), loginJson.getString("message") , Toast.LENGTH_LONG).show();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getBaseContext(), "No Connectivity", Toast.LENGTH_LONG).show();
}
}
}
}
});
}
我把它分解了一下,把它的主要部分放在 onStart() 中。然后我在 onCreate 和 onStart 中放置一条日志消息,设置断点并作为调试运行。 Android Studio 选择了断点,首先是 onCreate 中的日志消息,然后是 onStart 中的日志消息,然后它返回到 onCreate 并再次返回到 onStart。
所以肯定有东西调用了 onCreate 两次(或整个 Activity ?)
最佳答案
在应该使用 this
或 YourActivity.this
的情况下,您可以多次使用 getApplicationContext() 和 getBaseContext()。在某些情况下,这可能会导致内存泄漏。
这应该是一个评论,但我没有足够的代表。非常抱歉...
关于java - MainActivity onCreate 运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25889474/