如果条件在调试器中执行两个块

时间:2011-06-13 13:39:10

标签: android

这可能是愚蠢的,但我有一个活动类从非活动类调用方法。当我在Eclipse调试器中逐步执行此操作时,返回true并返回false在if条件中执行,该条件评估netinfo对象并且isOnline方法返回FALSE,即使netInfo.isConnected()计算结果为true且不为null。当我在调用类的if条件中得到值时,在调试器中检查时,我得到“JDI线程评估遇到了问题”。它评估为true,所以我得到了成功的消息。为什么我会看到此行为以及如何正确评估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;}

}

}

1 个答案:

答案 0 :(得分:2)

有时,在调试时,我也会得到当前指令奇怪的返回位置的行为 所以我认为了解isOnline函数返回值的最佳方法是记录结果本身;

之类的东西
boolean online = netInfo != null && netInfo.isConnected();
Log.d("test", "online returning " + online);
return online;