无法实例化服务 - Java.NullPointerException

时间:2013-12-06 16:26:46

标签: java android service nullpointerexception

所以基本上我对android开发很新,我想创建一个在后台运行的应用程序。我决定使用服务,因为我需要应用程序不断获取有关Wifi的信息,只要切换按钮在应用程序本身中保持打开状态。如果切换按钮关闭,服务将关闭。

但是,我在使服务运行时遇到了很多麻烦,而且出现的错误是标题中指定的错误。

任何人都可以帮我解决这个问题吗?

这是我的清单。您在此处看到其他活动的原因是因为我正在尝试将之前未在后台运行的程序迁移到在后台运行的服务。所以我现在唯一使用的活动是Main Activity,它有一个切换按钮和服务类。

    <?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>

我的服务类代码:

曾经有过这里写的处理信息的代码,但我决定首先尝试服务的准系统来弄清楚出了什么问题。

   
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();
    }


}

我的主要活动:

 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错误:

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)

1 个答案:

答案 0 :(得分:2)

下面

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

您正在尝试在创建之前获取服务上下文,因此在wifi或[{1}}} onStartCommand方法的onCreate方法中移动WifiPullService实例的初始化:

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