gpt4 book ai didi

java - 安卓服务

转载 作者:行者123 更新时间:2023-11-29 22:20:48 25 4
gpt4 key购买 nike

我正在为 android 编写一个网络服务器。实际上它适用于线程,但现在我会使用服务使其在后台运行。我把线程换成service了,不知道这样设计好不好。。。当我按下一个按钮时,它会启动,然后一直处于聆听状态。当有请求时,服务创建一个线程来服务它。这是我的结构:

WebServer.java

class WebServer extends Service{
onCreate(){...}
onDestroy(){...}
}

DroidServer.java

class DroidServer extends WebServer{...}

MyActivity.java

MyActivity extends Activity{
boolean isOn=false;
btn.setOnClickListener(new OnClickListener(){
public void onClick(View V){
if(!isOn){
startService(new Intent(this, DroidWebServer.class));
btn.setText("Stop");
}else{
stopService(new Intent(this, DroidWebServer.class));
btn.setText("Start");
}}});
}

AndroidManifest.xml

... <service android:name='DroidWebServer'/> ...

但是当我点击按钮时,我得到了这个异常:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to Instantiate service it.giox.ws.DroidWebServer: java.lang.InstantiationException: it.giox.ws.DroidWebServer
at android.app.ActivityThread.handleCreateService(ActivityThread.java:1933)
at android.app.ActivityThread.access$2500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3729)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: it.giox.ws.DroidWebServer
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1409)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:1930)
... 10 more

问题出在哪里?

最佳答案

你见过this吗?邮政?它基本上说您的构造函数中存在错误。他的解决方法是删除参数,并确保调用 super 的构造函数...

InstantiationException occurs when an exception is thrown out of a constructor. Generally there should be another exception reported as causing the InstantiationException, and the stack trace should point to the specific lines that are in error. Stack trace is your friend, and you should be grateful -- their are poor children programming on Symbian devices who have no stack trace.

关于java - 安卓服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7270798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com