Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid uncaught handler/uncaught exception
    primarykey
    data
    text
    <p>this is my first Android app and I'm trying to use Sockets to send text from my computer (server) to my phone (client). I've programmed in Java a lot before, but always with DrJava, not Eclipse, which is what I'm having to use for Android programming.</p> <p>The following code is simply an example Java program using Sockets that I just added and removed a few lines and pasted it into the Android code. The code is found here: <a href="http://zerioh.tripod.com/ressources/sockets.html" rel="nofollow">http://zerioh.tripod.com/ressources/sockets.html</a> If you don't trust links, you can Google "java socket example." It should be the first link.</p> <p>I'm getting an uncaught exception error, but I've looked through the code many times and I can't find a place without a catch line. The code works with Java, but not Android, so I think it's some issue with Android-specific syntax.</p> <p>Here is the Android code (client):</p> <pre><code>package com.example.helloandroid; import android.app.Activity; import android.widget.TextView; import android.os.Bundle; import java.io.*; import java.net.Socket; import java.net.UnknownHostException; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //TextView tv = new TextView(this); //tv.setText("Eclipse is a horrible program"); Requester client = new Requester(); client.run(); //setContentView(tv); } } class Requester{ Socket requestSocket; ObjectOutputStream out; ObjectInputStream in; String message; void run() { try{ //1. creating a socket to connect to the server requestSocket = new Socket("localhost", 2004); System.out.println("Connected to localhost in port 2004"); //2. get Input and Output streams out = new ObjectOutputStream(requestSocket.getOutputStream()); out.flush(); in = new ObjectInputStream(requestSocket.getInputStream()); //3: Communicating with the server try{ message = (String)in.readObject(); System.out.println("server&gt;" + message); sendMessage("asdfjksajdflksjdklfjsdklfjlsdf"); message = "bye"; sendMessage(message); } catch(ClassNotFoundException classNot){ System.err.println("data received in unknown format"); } } catch(UnknownHostException unknownHost) { System.err.println("You are trying to connect to an unknown host!"); } catch(IOException ioException) { ioException.printStackTrace(); } finally{ //4: Closing connection try{ in.close(); out.close(); requestSocket.close(); } catch(IOException ioException){ ioException.printStackTrace(); } } } void sendMessage(String msg) { try{ out.writeObject(msg); out.flush(); System.out.println("client&gt;" + msg); } catch(Exception e) { e.printStackTrace(); } /*catch(IOException ioException){ ioException.printStackTrace(); }*/ } } </code></pre> <p>The server code is the same as on the website.</p> <p>Here is the catlog lines that I think are relevant. If I missed some, let me know and I'll post those too:</p> <pre><code>11-20 22:37:35.314: W/System.err(545): java.net.SocketException: Permission denied (maybe missing INTERNET permission) 11-20 22:37:35.324: W/System.err(545): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method) 11-20 22:37:35.324: W/System.err(545): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:187) 11-20 22:37:35.324: W/System.err(545): at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:266) 11-20 22:37:35.324: W/System.err(545): at java.net.Socket.startupSocket(Socket.java:773) 11-20 22:37:35.324: W/System.err(545): at java.net.Socket.tryAllAddresses(Socket.java:192) 11-20 22:37:35.324: W/System.err(545): at java.net.Socket.&lt;init&gt;(Socket.java:256) 11-20 22:37:35.334: W/System.err(545): at java.net.Socket.&lt;init&gt;(Socket.java:220) 11-20 22:37:35.334: W/System.err(545): at com.example.helloandroid.Requester.run(HelloAndroid.java:33) 11-20 22:37:35.334: W/System.err(545): at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:19) 11-20 22:37:35.334: W/System.err(545): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-20 22:37:35.334: W/System.err(545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 11-20 22:37:35.334: W/System.err(545): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 11-20 22:37:35.344: W/System.err(545): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 11-20 22:37:35.344: W/System.err(545): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 11-20 22:37:35.344: W/System.err(545): at android.os.Handler.dispatchMessage(Handler.java:99) 11-20 22:37:35.344: W/System.err(545): at android.os.Looper.loop(Looper.java:123) 11-20 22:37:35.344: W/System.err(545): at android.app.ActivityThread.main(ActivityThread.java:4363) 11-20 22:37:35.344: W/System.err(545): at java.lang.reflect.Method.invokeNative(Native Method) 11-20 22:37:35.344: W/System.err(545): at java.lang.reflect.Method.invoke(Method.java:521) 11-20 22:37:35.344: W/System.err(545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 11-20 22:37:35.344: W/System.err(545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 11-20 22:37:35.354: W/System.err(545): at dalvik.system.NativeStart.main(Native Method) 11-20 22:37:35.354: D/AndroidRuntime(545): Shutting down VM 11-20 22:37:35.354: W/dalvikvm(545): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 11-20 22:37:35.354: E/AndroidRuntime(545): Uncaught handler: thread main exiting due to uncaught exception 11-20 22:37:35.364: E/AndroidRuntime(545): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroid}: java.lang.NullPointerException 11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 11-20 22:37:35.364: E/AndroidRuntime(545): at android.os.Handler.dispatchMessage(Handler.java:99) 11-20 22:37:35.364: E/AndroidRuntime(545): at android.os.Looper.loop(Looper.java:123) 11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread.main(ActivityThread.java:4363) 11-20 22:37:35.364: E/AndroidRuntime(545): at java.lang.reflect.Method.invokeNative(Native Method) 11-20 22:37:35.364: E/AndroidRuntime(545): at java.lang.reflect.Method.invoke(Method.java:521) 11-20 22:37:35.364: E/AndroidRuntime(545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 11-20 22:37:35.364: E/AndroidRuntime(545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 11-20 22:37:35.364: E/AndroidRuntime(545): at dalvik.system.NativeStart.main(Native Method) 11-20 22:37:35.364: E/AndroidRuntime(545): Caused by: java.lang.NullPointerException 11-20 22:37:35.364: E/AndroidRuntime(545): at com.example.helloandroid.Requester.run(HelloAndroid.java:60) 11-20 22:37:35.364: E/AndroidRuntime(545): at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:19) 11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 11-20 22:37:35.364: E/AndroidRuntime(545): ... 11 more 11-20 22:37:35.384: I/dalvikvm(545): threadid=7: reacting to signal 3 11-20 22:37:35.404: I/dalvikvm(545): Wrote stack trace to '/data/anr/traces.txt' </code></pre> <p>When I run this code on an Android emulator, it says, "Sorry! The application...has stopped unexpectedly. Please try again." Any help will be greatly appreciated! Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload