我创建了一个 android 应用程序,它创建一个网络服务器并在端口 1234 上侦听问题是,当我点击启动服务器按钮时它工作正常,当我点击停止按钮时,它在 logcat 中给我一个错误并重新启动..这个是我的服务,任何帮助将不胜感激。
代码在这里...
package dolphin.developers.com;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class hot1 extends Service {
private static final String TAG = null;
JHTTP pro;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Server Started.", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
try{
File documentRootDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/");
pro = new JHTTP(documentRootDirectory, 1234);
}
catch(IOException e ){
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
pro.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
pro.start();
}
}
日志猫:
08-05 12:51:54.210: E/AndroidRuntime(511): FATAL EXCEPTION: main
08-05 12:51:54.210: E/AndroidRuntime(511): java.lang.RuntimeException: Unable to start service dolphin.developers.com.hot1@45fe6990 with Intent { cmp=dolphin.devlopers.com/dolphin.developers.com.hot1 }: java.lang.NullPointerException
08-05 12:51:54.210: E/AndroidRuntime(511): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
08-05 12:51:54.210: E/AndroidRuntime(511): at android.app.ActivityThread.access$3600(ActivityThread.java:125)
08-05 12:51:54.210: E/AndroidRuntime(511): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
08-05 12:51:54.210: E/AndroidRuntime(511): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 12:51:54.210: E/AndroidRuntime(511): at android.os.Looper.loop(Looper.java:123)
08-05 12:51:54.210: E/AndroidRuntime(511): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-05 12:51:54.210: E/AndroidRuntime(511): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 12:51:54.210: E/AndroidRuntime(511): at java.lang.reflect.Method.invoke(Method.java:521)
08-05 12:51:54.210: E/AndroidRuntime(511): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-05 12:51:54.210: E/AndroidRuntime(511): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-05 12:51:54.210: E/AndroidRuntime(511): at dalvik.system.NativeStart.main(Native Method)
08-05 12:51:54.210: E/AndroidRuntime(511): Caused by: java.lang.NullPointerException
08-05 12:51:54.210: E/AndroidRuntime(511): at dolphin.developers.com.hot1.onStart(hot1.java:72)
08-05 12:51:54.210: E/AndroidRuntime(511): at android.app.Service.onStartCommand(Service.java:420)
08-05 12:51:54.210: E/AndroidRuntime(511): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
08-05 12:51:54.210: E/AndroidRuntime(511): ... 10 more