0
這可能是愚蠢的,但我有一個活動類調用非活動類的方法。當我在Eclipse調試器中逐步完成此操作時,即使netInfo.isConnected()的計算結果爲true且不爲空,它們都會返回true並在if條件中返回false,然後返回FALSE。當我在調用類的if條件中獲得值時,在調試器中檢查後,我得到「JDI線程評估遇到問題」。它的評估爲真,所以我得到了成功的信息。爲什麼我看到這種行爲,以及如何正確評估isConnected方法的結果?如果條件在調試程序中執行兩個塊
Calling Main Activity Class:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Splash extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Connection.context = this;
// Check Internet Connection
if (!Connection.isOnline())
{
//throw some message
Log.d("Test","Fail!");
}
else
{
//state something good.
Log.d("Test","Success!");
}
Called Non Activity Class
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class Connection {
public static Context context;
// Check Internet Connection
public static boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected())
{return true;}
else
{return false;}
}
}
「都返回true,並返回false在if條件得到執行」 - 很不尋常... – 2011-06-13 13:48:47
哈哈這怎麼可能:D。我在這裏看不到錯字或其他內容。 – DArkO 2011-06-13 13:51:31
也許嘗試三元操作。不知道爲什麼兩條路都會被擊中。 – Spidy 2011-06-13 13:52:58