简体   繁体   中英

Unable to Instantiate Service - Java.NullPointerException

so basically I'm rather new to android development and I want to create an application that runs in the background. I have decided to use Service as I need the application to constantly be grabbing information regarding Wifi as long as the toggle button remains on in the application itself. The service will be turned off if the toggle button is off.

However, I have been having a LOT of trouble getting the service to even run, and the error that comes up is the one specified in the title.

Can anyone help me figure this out?

Here is my manifest. The reason why you see other activities here is because I'm trying to migrate my previous program which didn't run in the background into a service that runs in the background. So right now the only activity I use is Main Activity that has a toggle button, and the service class.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wifianalysis"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-     permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <uses-feature android:name="android.hardware.wifi" />
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!--
            Because android:exported is set to "false",
            the service is only available to this app.
        -->
        <service
        android:name=".WifiPullService"
        android:label="Wifi Pull Service" >
    </service>


        <activity
            android:name="com.example.wifianalysis.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.wifianalysis.Checkwifi"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.CHECK" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.wifianalysis.wifi_on"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.WIFION" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


        <activity
            android:name="com.example.wifianalysis.wifi_off"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.WIFIOFF" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


        <receiver android:name=".WiFiScanReceiver">
          <intent-filter>
            <action android:name="com.rdc" />
          </intent-filter>
        </receiver>

    </application>

</manifest>

Code for my service class:

There used to be code written here that processed information, but I decided to try the barebones of the service first to figure out what was wrong.

package com.example.wifianalysis;

    import java.util.List;
    import java.util.concurrent.ScheduledExecutorService;

    import android.app.Service;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.wifi.ScanResult;
    import android.net.wifi.WifiManager;
    import android.os.Binder;
    import android.os.Handler;
    import android.os.IBinder;
    import android.widget.Toast;


    public class WifiPullService extends Service {

    ScheduledExecutorService scheduleTaskExecutor;
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    int count =0;
    private Runnable mScanInfo;
    private final Handler mHandler = new Handler();

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public class LocalBinder extends Binder {
        WifiPullService getService() {
            return WifiPullService.this;
        }
    }







    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        return super.onStartCommand(intent, flags, startId);





    }



    @Override
    public void onCreate() {

        super.onCreate();
        Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();
        // TODO Auto-generated method stub




        //super.onCreate();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
        super.onDestroy();
    }

    @Override
    @Deprecated
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
    }


}

My Main Activity:

 package com.example.wifianalysis;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.ToggleButton;
    //import android.net.wifi.WifiManager;

    public class MainActivity extends Activity {

        public void onToggleClicked(View view) { //when backbutton is pressed go back to main
            // Do something in response to button
            boolean on = ((ToggleButton) view).isChecked();

            try{

                if (on) {
                    //Intent backIntent = new Intent("android.intent.action.CHECK");
                    Intent i = new Intent(this, WifiPullService.class);
                    startService(i); //if checked, start service
                }
                else{
                    stopService(new Intent(this, WifiPullService.class)); //if unchecked, stop service
                }
            }
            catch (Exception e){
                System.err.println("Could not start/stop service: " + e.getMessage());
            }
        }


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);



        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

LogCat Errors:

12-06 10:38:39.440: W/dalvikvm(7458): threadid=1: thread exiting with uncaught exception (group=0x416ed450)

12-06 10:38:39.460: E/AndroidRuntime(7458): FATAL EXCEPTION: main

12-06 10:38:39.460: E/AndroidRuntime(7458): java.lang.RuntimeException: Unable to instantiate service com.example.wifianalysis.WifiPullService: java.lang.NullPointerException
12-06 10:38:39.460: E/AndroidRuntime(7458): java.lang.RuntimeException: Unable to instantiate service com.example.wifianalysis.WifiPullService: java.lang.NullPointerException
12-06 10:38:39.460: E/AndroidRuntime(7458):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2368)

Here

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

you are trying to get Service context before creation so move initialization of wifi instance inside onStartCommand or onCreate method of WifiPullService service :

@Override
public void onCreate() {
    super.onCreate();
    // initialize wifi instace here
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    //....your code here...    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM