English 中文(简体)
Null PointerException on NetworkInfo
原标题:NullPointerException on NetworkInfo
  • 时间:2011-11-23 10:38:09
  •  标签:
  • android

I am programming a service that runs when you boot the phone, but you need an Internet connection and connection to send SMS operator. I wrote a function to ConnectionManager as in other post appears, but I always get the same answer: NullPointerException, when I do the following:

页: 1

NetworkInfo NetInfo = cm.getNetworkInfo();

不是因为这种情况?

我怎么能够确定这一点?

如果我没有制定所有法典,那么我会问:

I have the following in the OnStart () method of a service that is activated by ACTION_BOOT_COMPLETED ...

@ Override
public void OnStart (Intent intent, int start) {

    Context aplCtx = getApplicationContext ();


    IsConnected boolean = false;
    while (! IsConnected) {
        try {
            IsConnected = IsOnline (aplCtx);

        } Catch (Exception e) {
            Log.d (e.toString ());
        }
    }

    .....  
}

private boolean IsOnline (Context ctx) {

    boolean result = false;

    try {
        ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService     (Context.CONNECTIVITY_SERVICE);
        cm.getActiveNetworkInfo NetworkInfo ni = ();

        if (ni! = null) {
           if (ni.getState () == NetworkInfo.State.CONNECTED) {
               result = true;
           }
        }
        return result;
}

Since this is a background function and after starting the Smartphone, the operating system to force kill the process, before we get an Internet connection. How I can resolve this?, How do I get the operating system does not kill the process and have an Internet connection to perform the following actions?

问题回答

Without your code, We can t say anything what going wrong,

但是,如果你不犯错误,那么我会利用这个网络。

public static boolean netConnect(Context ctx){
    ConnectivityManager cm ; 
    NetworkInfo info = null;

    try {
        cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        info = cm.getActiveNetworkInfo();        

    } catch (Exception e) {
        Log.e("connectivity", e.toString());
    }

    if(info != null){
          return info.isConnected();
    }else{
        return false;
    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签