有人请解释错误

时间:2012-01-31 10:01:58

标签: java android android-emulator

我是android编程的新手。我一直在尝试在两个模拟器之间建立连接。虽然我的服务器模拟器启动并运行,但是客户端有问题。这是代码和logcat错误说明。请告诉我错误此

public class SocketClient extends Activity 
{

   private Button bt;
   private TextView tv;
   private Socket socket;
   private String serverIpAddress = "192.168.0.5";
   private static final int REDIRECTED_SERVERPORT = 5000;
   public void connect()
   {
   try 
   {
         InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
         tv.setText((CharSequence) serverAddr);
         socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
    }
    catch (UnknownHostException e1) 
    {
         e1.printStackTrace();
         System.out.println("Here");
    }
    catch (IOException e1)
    {
         e1.printStackTrace();
         System.out.println("Here too");
    }
    }


   @Override
    public void onCreate(Bundle savedInstanceState)
    {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  bt = (Button) findViewById(R.id.myButton);
  tv = (TextView) findViewById(R.id.myTextView);
  connect();
  bt.setOnClickListener(new OnClickListener()
  {
     public void onClick(View v)
     {
        try 
        {
           EditText et = (EditText) findViewById(R.id.EditText01);
           String str = et.getText().toString();
           PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
           out.println(str);
           Log.d("Client", "Client sent message");
        }
        catch (UnknownHostException e) 
        {
           tv.setText("Error1");
           e.printStackTrace();
        }
        catch (IOException e) 
        {
           tv.setText("Error2");
           e.printStackTrace();
        }
        catch (Exception e) 
        {
           tv.setText("Error3");
           e.printStackTrace();
        }
     }
  }
  );
 }
}

logcat错误是

01-31 04:42:51.170: W/dalvikvm(529): threadid=1: thread exiting with uncaught exception           (group=0x409c01f8)
01-31 04:42:51.187: E/AndroidRuntime(529): FATAL EXCEPTION: main
01-31 04:42:51.187: E/AndroidRuntime(529): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.app.ServerClient/com.app.ServerClient.SocketClient}:   java.lang.ClassCastException: java.net.Inet4Address cannot be cast to java.lang.CharSequence
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.os.Looper.loop(Looper.java:137)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-31 04:42:51.187: E/AndroidRuntime(529):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 04:42:51.187: E/AndroidRuntime(529):  at java.lang.reflect.Method.invoke(Method.java:511)
01-31 04:42:51.187: E/AndroidRuntime(529):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-31 04:42:51.187: E/AndroidRuntime(529):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-31 04:42:51.187: E/AndroidRuntime(529):  at dalvik.system.NativeStart.main(Native Method)
01-31 04:42:51.187: E/AndroidRuntime(529): Caused by: java.lang.ClassCastException: java.net.Inet4Address cannot be cast to java.lang.CharSequence
01-31 04:42:51.187: E/AndroidRuntime(529):  at com.app.ServerClient.SocketClient.connect(SocketClient.java:25)
01-31 04:42:51.187: E/AndroidRuntime(529):  at com.app.ServerClient.SocketClient.onCreate(SocketClient.java:48)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.Activity.performCreate(Activity.java:4465)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-31 04:42:51.187: E/AndroidRuntime(529):  ... 11 more

提前致谢。


谢谢David和Jitendra。

我纠正了错误,我在代码的一部分得到了一个nullPointerException。我做错了什么?

public void onClick(View v)
     {
        try                                                      // The error is in                this block
        {
           EditText et = (EditText) findViewById(R.id.EditText01);
           String str = et.getText().toString();
           PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
           out.println(str);
           Log.d("Client", "Client sent message");
        }
        catch (UnknownHostException e) 
        {
           tv.setText("Error1");
           e.printStackTrace();
        }
        catch (IOException e) 
        {
           tv.setText("Error2");
           e.printStackTrace();
        }
        catch (Exception f)                 // I get the error here. java.lang.NullPointerException
        {
           tv.setText("Error3");
           tv.setText(f.toString());
           f.printStackTrace();
        }

     }

2 个答案:

答案 0 :(得分:2)

您的错误就在这一行:

tv.setText((CharSequence) serverAddr);

serverAddr的类型为InetAddress,您正在尝试将其转换为无法完成的CharSequence。也许你的意思是:

tv.setText(serverIpAddress);

答案 1 :(得分:1)

例外情况如下:

tv.setText((CharSequence) serverAddr);

这是因为您正在尝试将serverAddr强制转换为CharSequence。

如果你真的想打印serverAddr,请使用

tv.setText((CharSequence) serverAddr.toString());