1

我正在开发一个使用 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);
       }
}

日志猫:

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
4

2 回答 2

3

您只能在 Manifest 中将扩展 Android 的“Activity”的类声明为活动。尝试创建一个,并在 Activity 生命周期(onCreate / onDestroy 或 onStart / onStop)中尝试启动和停止 NanoHttpd 服务器。

于 2013-07-31T13:03:59.973 回答
3

MyHTTPD 不是一项活动,因此您不能将其添加到清单文件中

这里有一个例子:

https://gist.github.com/komamitsu/1893396

这个例子是你只需要在清单中添加活动 AndroidWebServerActivity

于 2013-07-17T12:35:36.687 回答