我正在开发一个使用nanohttpd创建Web服务器的android应用,当我运行它时它说活动已经停止了,这是我的代码,请帮助我提供任何帮助。代码如下:
package dolphin.developers.com;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import android.content.Context;
import android.os.Environment;
public class MyHTTPD extends NanoHTTPD{
private Context ctx;
public MyHTTPD(Context ctx) throws IOException {
super(8080);
this.ctx = ctx;
}
@Override
public Response serve( String uri, Method method,
Map<String, String> header, Map<String, String> parms,
Map<String, String> files )
{
String html = null;
InputStream is = null;
try {
is = ctx.getAssets().open(Environment.getExternalStorageDirectory()+"/index.htm");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] b;
try {
b = new byte[is.available()];
is.read(b);
html = new String(b);
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
return new NanoHTTPD.Response(html);
}
}
Logcat:
07-17 12:06:22.538: E/AndroidRuntime(1137): FATAL EXCEPTION: main
07-17 12:06:22.538: E/AndroidRuntime(1137): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{dolphin.devlopers.com/dolphin.developers.com.MyHTTPD}: java.lang.ClassCastException: dolphin.developers.com.MyHTTPD cannot be cast to android.app.Activity
07-17 12:06:22.538: E/AndroidRuntime(1137): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-17 12:06:22.538: E/AndroidRuntime(1137): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-17 12:06:22.538: E/AndroidRuntime(1137): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-17 12:06:22.538: E/AndroidRuntime(1137): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-17 12:06:22.538: E/AndroidRuntime(1137): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 12:06:22.538: E/AndroidRuntime(1137): at android.os.Looper.loop(Looper.java:137)
07-17 12:06:22.538: E/AndroidRuntime(1137): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-17 12:06:22.538: E/AndroidRuntime(1137): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 12:06:22.538: E/AndroidRuntime(1137): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 12:06:22.538: E/AndroidRuntime(1137): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-17 12:06:22.538: E/AndroidRuntime(1137): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-17 12:06:22.538: E/AndroidRuntime(1137): at dalvik.system.NativeStart.main(Native Method)
07-17 12:06:22.538: E/AndroidRuntime(1137): Caused by: java.lang.ClassCastException: dolphin.developers.com.MyHTTPD cannot be cast to android.app.Activity
07-17 12:06:22.538: E/AndroidRuntime(1137): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-17 12:06:22.538: E/AndroidRuntime(1137): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-17 12:06:22.538: E/AndroidRuntime(1137): ... 11 more
最佳答案
您只能将清单中扩展Android的“活动”的类声明为活动。尝试创建一个,并在Activity生命周期(onCreate / onDestroy或onStart / onStop)中尝试启动和停止您的NanoHttpd服务器。