我的应用程序安装在android emulator上,但无法运行,并且没有错误

我的应用程序安装在android emulator上,但无法运行,并且没有错误,android,Android,我已经开发了一个聊天应用程序,使用套接字编程,有一个客户端和一个服务器。 我正在运行2个项目。1个用于客户端,1个用于服务器。 我的客户端应用程序安装在模拟器上,但当我单击图标时,它不会运行。 没有错误: package com.client.chat; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.*; import android.v

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

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();
    }
}
请帮忙

移除饰面()或在其上放置断点。我打赌它击中那个然后退出

此外,我没有看到while循环、新线程或任何保持客户端活动的机制,tcpClient()将运行到底并退出活动,因为您只有一个活动,它将退出应用程序。

删除finish()或在其上放置断点。我打赌它击中那个然后退出

此外,我没有看到while循环、新线程或任何保持客户端活动的机制,tcpClient()将运行到底,并退出活动,因为您只有一个活动,它将退出应用程序。

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

Thread t = new Thread(){
            @Override
            public void run() {
                tcpclient();
            }
        };
        t.start();
在线程中运行tcpClient(),它应该可以工作

Thread t = new Thread(){
            @Override
            public void run() {
                tcpclient();
            }
        };
        t.start();
应该在线程中运行tcpClient(),是的。但是,如果不在while循环中使用循环末尾的Thread.Sleep()来包装readline(),则该线程将运行到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
}
应该在线程中运行tcpClient(),是的。但是,如果不在while循环中使用循环末尾的Thread.Sleep()来包装readline(),则该线程将运行到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
}
尝试运行“adb logcat”,或者如果您是从eclipse运行的,请查看logcat选项卡,这可能会为您提供一些有用的信息尝试运行“adb logcat”,或者如果您是从eclipse运行的,请查看logcat选项卡,这可能会为您提供一些有用的信息