這是我的第一個Android應用程序,我試圖使用套接字將文本從我的計算機(服務器)發送到我的手機(客戶端)。我之前已經用Java編寫了很多程序,但是總是使用DrJava,而不是Eclipse,這是我必須用於Android編程的東西。Android未捕獲的處理程序/未捕獲的異常
以下代碼僅僅是一個使用Sockets的Java程序示例,我剛剛添加並刪除了幾行並將其粘貼到Android代碼中。代碼在這裏找到:http://zerioh.tripod.com/ressources/sockets.html 如果你不信任鏈接,你可以谷歌「java套接字示例」。它應該是第一個鏈接。
我得到一個未捕獲的異常錯誤,但我已經查看了很多次代碼,找不到沒有catch line的地方。該代碼適用於Java,但不適用於Android,所以我認爲這是Android特定語法的一個問題。
下面是Android的代碼(客戶端):
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>" + 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>" + msg);
}
catch(Exception e) {
e.printStackTrace();
}
/*catch(IOException ioException){
ioException.printStackTrace();
}*/
}
}
服務器的代碼是一樣的網站上。
這是我認爲相關的catlog行。如果我錯過了一些,讓我知道,我會發布這些呢!。
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.<init>(Socket.java:256)
11-20 22:37:35.334: W/System.err(545): at java.net.Socket.<init>(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'
當我運行在Android模擬器此代碼,它說:「對不起......意外停止的應用程序,請嘗試再次。」任何幫助將不勝感激!謝謝!