我第一次在Android上使用服务,我阅读了所有文档和示例,我想从活动中实现绑定服务,我的服务扫描可用的wifi网络和我的活动获取wifi列表,但我卡住了,我不&# 39;知道我的错误在哪里,我的日志说无法启动活动nullpointerexception。清单是可以的,活动和服务是在不同的包。这是我的代码:Activity,然后是Service
public class StarterService extends Activity {
final String tag = "WifiConn:...";
//EditText txt;
WifiManager myWifi;
ScanService scan;
List<ScanResult> sRedes;
//private ScanService scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doBindService();
scan=new ScanService();
//sRet.clear();
sRedes=scan.getWifi();
for (int i=0; i<sRedes.size(); i++)
{
ScanResult retS = sRedes.get(i);
// txt.append("resT: " + retS.SSID +" " + retS.BSSID + "\n");
Log.d(tag, "resT: " + retS.SSID +" " + retS.BSSID);
if (retS.SSID.equalsIgnoreCase("Red1"))
{
// txt.append("Red encontrada: " + retS.SSID +" " + retS.BSSID + "\n");
WifiConfiguration wc = new WifiConfiguration();
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wc.SSID = "\""+retS.SSID+"\"";
wc.BSSID = retS.BSSID;
wc.status = WifiConfiguration.Status.ENABLED;
wc.hiddenSSID = true;
int netID = myWifi.addNetwork(wc); // añade red
// txt.append("NetID"+ Integer.toString(netID) +"\n");
if(myWifi.enableNetwork(netID, true)) // Valida conexión
{
// txt.append("Estado conexión: Ok\n");
Log.d("Servicio", "Activado");
}
} else {
// txt.append("No encontrado: Ok\n");
Log.d("Servicio", "Activado");
//this.stopSelf();
}
}
}
private ServiceConnection mConnection=new ServiceConnection(){
public void onServiceConnected(ComponentName classname, IBinder binder){
scan=((ScanService.MyBinder)binder).getService();
Toast.makeText(StarterService.this, "Corriendo", Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName classname) {
scan= null;
}
};
protected void doBindService() {
bindService(new Intent(this, ScanService.class), mConnection, Context.BIND_AUTO_CREATE);
} }
此处服务
public class ScanService extends Service {
Timer timer = new Timer();
EditText txt;
WifiManager mWifi;
private static final long UPDATE_INTERVAL = 5000;
private final IBinder mBinder = new MyBinder();
final String tag = "WifiConn:...";
private List<ScanResult> sRet;
public void onCreate() {
super.onCreate();
timerScan();
}
private void timerScan(){
timer.scheduleAtFixedRate(new TimerTask(){
public void run(){
mWifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
mWifi.startScan(); //Inicio Scan
Log.d("Service", "Inicio Scan()");
sRet = mWifi.getScanResults(); //ResultadosScan
}
},0,UPDATE_INTERVAL);
Toast.makeText(this, "Servicio Iniciado", Toast.LENGTH_LONG).show();
Log.i(getClass().getSimpleName(),"Tiempo iniciado");
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public class MyBinder extends Binder {
public ScanService getService(){
return ScanService.this;
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("Servicio", "Destruido");
}
public List<ScanResult> getWifi(){
return sRet;
} }
当我调试时,错误出现在Activity:
上doBindService();
sRedes=scan.getWifi();
我的日志
03-24 00:24:46.814: E/AndroidRuntime(20798): FATAL EXCEPTION: main
03-24 00:24:46.814: E/AndroidRuntime(20798): Process: red1.pe, PID: 20798
03-24 00:24:46.814: E/AndroidRuntime(20798): java.lang.RuntimeException: Unable to start activity ComponentInfo{wigo.pe/wigo.pe.StarterService}: java.lang.NullPointerException
03-24 00:24:46.814: E/AndroidRuntime(20798): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
03-24 00:24:46.814: E/AndroidRuntime(20798): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
03-24 00:24:46.814: E/AndroidRuntime(20798): at android.app.ActivityThread.access$800(ActivityThread.java:139)
03-24 00:24:46.814: E/AndroidRuntime(20798): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
03-24 00:24:46.814: E/AndroidRuntime(20798): at android.os.Handler.dispatchMessage(Handler.java:102)
03-24 00:24:46.814: E/AndroidRuntime(20798): at android.os.Looper.loop(Looper.java:136)
03-24 00:24:46.814: E/AndroidRuntime(20798): at android.app.ActivityThread.main(ActivityThread.java:5102)
03-24 00:24:46.814: E/AndroidRuntime(20798): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 00:24:46.814: E/AndroidRuntime(20798): at java.lang.reflect.Method.invoke(Method.java:515)
03-24 00:24:46.814: E/AndroidRuntime(20798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-24 00:24:46.814: E/AndroidRuntime(20798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-24 00:24:46.814: E/AndroidRuntime(20798): at dalvik.system.NativeStart.main(Native Method)
03-24 00:24:46.814: E/AndroidRuntime(20798): Caused by: java.lang.NullPointerException
03-24 00:24:46.814: E/AndroidRuntime(20798): at wigo.pe.StarterService.onCreate(StarterService.java:35)
03-24 00:24:46.814: E/AndroidRuntime(20798): at android.app.Activity.performCreate(Activity.java:5248)
03-24 00:24:46.814: E/AndroidRuntime(20798): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
03-24 00:24:46.814: E/AndroidRuntime(20798): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
03-24 00:24:46.814: E/AndroidRuntime(20798): ... 11 more
答案 0 :(得分:0)
您的Service
实例没问题。没有问题。
问题是您没有初始化EditText
实例txt
。在使用onCreate()
执行任何操作之前,在txt
方法中对其进行初始化。
另一件事,在初始化任何View
之前,使用`setContentView()'方法将布局设置为Activity
。
EditText txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_xml);
txt = (EditText) findViewById(R.id.edittext);
...........
}
答案 1 :(得分:0)
在 ScanService 类中为ScanService构建构造函数。
public ScanService() {
}
现在在您的活动中进行initiazlize
scan = new ScanService();
sRedes=scan.getWifi();