开发者

if condition executing both blocks in debugger

This is probably something stupid, but I have one activity class calling a method out of a non-activity class. When I step through this in the Eclipse debugger, both return true and return false get executed in the if condition that evaluates the netinfo object and the isOnline method returns FALSE, even though the netInfo.isConnected() evaluates to true and is not null. When I get the value in the if condition of the Calling Class, upon inspection in the debugger I get, "JDI Thread Evaluations has encountered a problem." and it evaluates to true, so I get the success message. Why am I seeing this behavior and how do I properly evaluate the results of the isConnected method?

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 Con开发者_Go百科nection 
    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;}

}

}


Sometimes, when debugging, I also get the weird behaviour of the current instruction going to weird return places.
So i think the best way to know the value returned by your isOnline function is to log the result itself; something like

boolean online = netInfo != null && netInfo.isConnected();
Log.d("test", "online returning " + online);
return online;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜