我的应用程序获取安装在Android模拟器上,但它无法运行,并且没有错误

问题描述:

我开发了一个使用套接字编程的聊天应用程序,有1个客户端和1个服务器。 我正在运行2个项目。 1个用于客户端,1个用于服务器。 我的客户端应用程序安装在模拟器上,但当我点击图标时,它不运行。 有没有错误:我的应用程序获取安装在Android模拟器上,但它无法运行,并且没有错误

package com.client.chat; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.*; 
import android.view.*; 
import android.view.View.OnClickListener; 
import java.net.*; 
import java.io.*; 

public class ClientActivity extends Activity{ 

    /** Called when the activity is first created. */ 
    private String serverIpAddress = "10.0.2.2"; 
    private static final int TCP_SERVER_PORT = 6000; 
    private Button bt; 
    private Socket s; 
    private TextView tv; 
    String error; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    // bt=(Button)findViewById(R.id.cbtn); 
     tv=(TextView)findViewById(R.id.clienttext); 
     tcpclient(); 
    } 

    private void tcpclient(){ 
    try{ 
     InetAddress serverAddr = InetAddress.getByName(serverIpAddress); 
     s = new Socket(serverAddr, TCP_SERVER_PORT); 
    }catch(UnknownHostException e){e.printStackTrace(); error=e.toString(); 
     tv.setText(error); 
    }catch(IOException e){e.printStackTrace(); 
     error=error.toString(); 
     tv.setText(error); 
    } 

     try{ 
        EditText et = (EditText) findViewById(R.id.clientedit); 
        String str = et.getText().toString(); 
        BufferedWriter out = null; 
        out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 
       String outMsg=str+System.getProperty("line.separator"); 

    // String outMsg = "TCP connecting to " + TCP_SERVER_PORT + System.getProperty("line.separator"); 
     out.write(str); 
     out.flush(); 
     Log.i("TcpClient", "sent: " + outMsg); 
        Log.d("Client", "Client sent message"); 
    }catch(UnknownHostException e){e.printStackTrace(); 
    error=e.toString(); 
    tv.setText(error); 
    }catch(IOException e){e.printStackTrace(); 
    error=e.toString(); 
    tv.setText(error); 
    } 

     //accept server response 
     BufferedReader in=null; 
     try{ 

     in= new BufferedReader(new InputStreamReader(s.getInputStream())); 
     String inMsg = in.readLine() + System.getProperty("line.separator"); 
     Log.i("TcpClient", "received: " + inMsg); 

     //close connection 
     s.close(); 
     }catch(UnknownHostException e){error=e.toString(); 
      tv.setText(error); 
     }catch(IOException e){error=e.toString(); 
      tv.setText(error); 
     } 
     finish(); 
    } 
} 

请帮忙!

+3

尝试运行“亚行logcat”,或查看logcat的标签,如果你是从Eclipse中运行,这可能会给你一些有用的信息 – crobicha 2012-02-01 19:55:07

删除finish()或在其上放置一个断点。我敢打赌它击中并退出。另外,我没有看到一个while循环,或者一个新的线程或任何机制来保持客户端的活着,tcpClient()将运行到最后,然后退出活动,因为你只有一个活动,它会退出应用程序。

+0

没错..光洁度创建人d问题感谢名单! :) – rockernerd 2012-02-11 10:23:19

在线程中运行tcpClient(),它应该工作。

Thread t = new Thread(){ 
      @Override 
      public void run() { 
       tcpclient(); 
      } 
     }; 
     t.start(); 

在线程中运行tcpClient()应该完成,是的。但是如果在循环结束时不将readline()封装在带有Thread.Sleep()的while循环中,则该线程将运行到finish()并退出。我不确定你的活动是否会退出它可能..你需要通过一个带有sleep()的循环让线程继续运行。只是把它放在一个线程中将无所作为。

in= new BufferedReader(new InputStreamReader(s.getInputStream())); 
while(!shutDown){ 
     String inMsg = in.readLine() + System.getProperty("line.separator"); 
     Log.i("TcpClient", "received: " + inMsg); 
     Thread.Sleep(1000); // sleep one second 
}