Question

i am developing a android app which uses nanohttpd to create a web server and when i run it it says the activity has stopped this is my code please help me any help will be appriciated.here goes the code:

  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
Was it helpful?

Solution

You can only declare classes that extend Android's "Activity" in the Manifest as activities. Try creating one, and in the Activity lifecycle (onCreate / onDestroy or onStart / onStop) try to start and stop your NanoHttpd server.

OTHER TIPS

MyHTTPD is not an activity so you can not add this to manifest file

Here there is an example:

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

is this example you only need to add in manifest the activity AndroidWebServerActivity

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top