我创建了一个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();
}
}
logcat的:
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
答案 0 :(得分:1)
检查以避免在函数为空时调用该函数
if(pro != null){
pro.stop();
}
如何启动JHHTP客户端?
JHHTP pro = new JHTTP(rootDir, 1234) //second one is Port
因为构造函数看起来像这样:
public JHTTP(File documentRootDirectory, int port)
throws IOException { //blah blah blah }