Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法实例化服务-Java.NullPointerException_Java_Android_Service_Nullpointerexception - Fatal编程技术网

无法实例化服务-Java.NullPointerException

无法实例化服务-Java.NullPointerException,java,android,service,nullpointerexception,Java,Android,Service,Nullpointerexception,所以基本上我对android开发还比较陌生,我想创建一个在后台运行的应用程序。我决定使用这项服务,因为我需要应用程序不断地获取有关Wifi的信息,只要切换按钮在应用程序中保持打开状态。如果切换按钮关闭,服务将关闭 然而,我在让服务运行时遇到了很多麻烦,出现的错误是标题中指定的错误 有人能帮我弄清楚吗 这是我的舱单。您在这里看到其他活动的原因是,我正在尝试将以前没有在后台运行的程序迁移到在后台运行的服务中。所以现在我唯一使用的活动是带有切换按钮的主活动和服务类 <?xml versi

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

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

有人能帮我弄清楚吗

这是我的舱单。您在这里看到其他活动的原因是,我正在尝试将以前没有在后台运行的程序迁移到在后台运行的服务中。所以现在我唯一使用的活动是带有切换按钮的主活动和服务类

    <?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;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.ToggleButton;
//导入android.net.wifi.WifiManager;
公共类MainActivity扩展了活动{
public void onToggleClicked(视图){//当按下backbutton时返回主视图
//对按钮做些反应
布尔on=((ToggleButton)视图).isChecked();
试一试{
如果(打开){
//Intent backIntent=newintent(“android.Intent.action.CHECK”);
Intent i=新Intent(这个,WifiPullService.class);
startService(i);//如果选中,则启动服务
}
否则{
stopService(新意图(this,WifiPullService.class));//如果未选中,则停止服务
}
}
捕获(例外e){
System.err.println(“无法启动/停止服务:+e.getMessage());
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回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)
这里

您试图在创建之前获取服务上下文,因此将
wifi
实例的初始化移动到
onStartCommand
onCreate
方法的
WifiPullService
服务中:

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