android与pc通讯,android端一直出现“unfortunately,has stopped”

分类:手机开发| 发布:佚名| 查看: | 发表时间:2013/12/26

求助:android与pc通讯,android端一直出现“unfortunately,**has stopped”。
在网上找了个android与pc通信的问题,可是android端一直出现“unfortunately,**has stopped”,以下是程序的所有问题,求高手解答!注:在AndroidManifest.xml中已经添加了网络权限<uses-permissionandroid:name="android.permission.INTERNET"/> 
soketactivity.java文件:
package com.test.socket;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

 

public class SocketActivity extends Activity implements OnClickListener {

  /** Called when the activity is first created. */

 private TextView tv;

 private Button btn, btn_exit;

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  tv = (TextView)findViewById(R.id.message);

  tv.setText("Ready");

  btn = (Button)findViewById(R.id.send);

  btn.setOnClickListener(this);

  btn_exit = (Button)findViewById(R.id.exit);

  btn_exit.setOnClickListener(this);

  }

  public void onClick(View v) {

  if (v == btn) {

  tv.setText("Start");

  int port = 7100;

  try {

  Socket socket = new Socket("121.48.240.56", port);//此处已经改为PC端的ip

  OutputStream out = socket.getOutputStream();

  DataOutputStream dout = new DataOutputStream(out);

  dout.writeUTF("Jordan");

  InputStream in = socket.getInputStream();

  DataInputStream din = new DataInputStream(in);

  String str = din.readUTF();

  tv.setText(str);

  in.close();

  out.close();

  socket.close();

  }catch (UnknownHostException e) {

 // TODO: handle exception

  tv.setText("UnknownHostError");

 }catch (IOException e) {

 // TODO: handle exception

 tv.setText("IOError");

 }

  } else if (v == btn_exit) {

  finish();

  }

  }

}
=============================================================================================
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <TextView
  android:id="@+id/message"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="welcome, sandy!" />
   
  <button
  android:id="@+id/send"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  android:text="send" />

<button
  android:id="@+id/exit"
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"
  android:text="exit" />
    
</LinearLayout>

===========================================
server端,Socketserver.JAVA
import java.net.*;

import java.io.*;

 

public class SocketAppServer {

 

 /**

  * @param args

  */

 public static void main(String[] args) {

 // TODO Auto-generated method stub

 ServerSocket ss = null;

 int port = 7100;

 String hello="From Server:Hello World!";

 //create ServerSocket

 try {

 ss = new ServerSocket(port);

 }catch (IOException e) {

 // TODO: handle exception

 System.out.println(e);

 System.exit(1);

 }

 //accept client connection

 while(true) {

 try {

 System.out.println("Waiting for connection on port="+port);

 Socket cs = ss.accept();

 InputStream input = cs.getInputStream();

 DataInputStream din = new DataInputStream(input);

 String name = din.readUTF();

 OutputStream output = cs.getOutputStream();

 DataOutputStream dout = new DataOutputStream(output);

 dout.writeUTF(hello+"Your name:"+name);

 System.out.println("Connection OK port="+port);

 System.out.println("=========================");

 din.close();

 output.close();

 cs.close();

 }catch (IOException e) {

 // TODO: handle exception

 System.out.println(e);

 }

 }

 }

}
一直就出现stopped这个问题,是怎么回事呢?


------解决方案--------------------
需要log,看看logcat中显示的异常信息 
------解决方案--------------------
SocketActivity的xml文件在绘制的时候出现问题
你看看这个xml文件是不是有问题

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/mobile-dev/799.html