Question

hello all i am creating a web server for android with NanoHttpd and when i run it .It says that the Activity has stopped working please help what should i do. This is the code which i am using. Here goes the Code:

    package dolphin.developers.com;

import java.io.IOException;
import java.util.Map;

public class  MyHTTPD  extends NanoHTTPD {

    /**
    * Constructs an HTTP server on given port.
    */
   public MyHTTPD()throws IOException {
       super(8080);
   }


@Override
   public Response serve( String uri, Method method,
           Map<String, String> header, Map<String, String> parms,
           Map<String, String> files )
   {
       System.out.println( method + " '222" + uri + "' " );
       String msg = "<html><body><h1>Hello server</h1>\n";
       if ( parms.get("username") == null )
           msg +=
               "<form action='?' method='get'>\n" +
               "  <p>Your name: <input type='text' name='username'></p>\n" +
               "</form>\n";
       else
           msg += "<p>Hello, " + parms.get("username") + "!</p>";

       msg += "</body></html>\n";
       return new NanoHTTPD.Response(msg );
   }


   public static void main( String[] args )
   {
       try
       {
           new MyHTTPD();
       }
       catch( IOException ioe )
       {
           System.err.println( "Couldn't start server:\n" + ioe );
           System.exit( -1 );
       }
       System.out.println( "Listening on port 8080. Hit Enter to stop.\n" );
       try { System.in.read(); } catch( Throwable t ) {
           System.out.println("read error");
       };
   }

}

LogCat:

07-14 22:35:26.394: E/AndroidRuntime(581): FATAL EXCEPTION: main
07-14 22:35:26.394: E/AndroidRuntime(581): android.content.ActivityNotFoundException: Unable to find explicit activity class {dolphin.devlopers.com/dolphin.developers.com.MyHTTPD}; have you declared this activity in your AndroidManifest.xml?
07-14 22:35:26.394: E/AndroidRuntime(581):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
07-14 22:35:26.394: E/AndroidRuntime(581):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
07-14 22:35:26.394: E/AndroidRuntime(581):  at android.app.Activity.startActivityForResult(Activity.java:2817)
07-14 22:35:26.394: E/AndroidRuntime(581):  at android.app.Activity.startActivity(Activity.java:2923)
07-14 22:35:26.394: E/AndroidRuntime(581):  at dolphin.developers.com.facebook1$DownloadFileFromURL.onPostExecute(facebook1.java:167)
Was it helpful?

Solution

Look in your AndroidManifest.xml and make sure that your main activity is registered (in your case MyHTTPD). Heres a very basic manifest file, take a look at the tag. I put in your class as an entry.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.epstest"
    android:versionCode="1"
    android:versionName="1.0" >
    ....
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

--------------------------------------------------------------------------------------------------

        <activity
            android:name="dolphin.developers.com.MyHTTPD"
            android:label="@string/title"
            android:configChanges="orientation" >
        </activity>

--------------------------------------------------------------------------------------------------

    </application>

</manifest>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top