Android:按钮上的应用程序崩溃单击

问题描述:

我正在实施一个简单的照片共享客户端。Android:按钮上的应用程序崩溃单击

我在我的用户界面中有两个按钮:一个用于浏览图像,另一个用于发送它。

问题是,当我按下发送按钮浏览并选择图像后,应用程序突然崩溃。

下面的代码:

public class FileTransfer extends Activity { 
    /** Called when the activity is first created. */ 

    private static final int SELECT_PICTURE = 1; 

    private String selectedImagePath; 
    private ImageView img; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_fs); 
     img = (ImageView) findViewById(R.id.ivPic); 
     Button Browse = (Button) findViewById(R.id.bBrowse); 
     Button send = (Button) findViewById(R.id.bSend); 
     final TextView status = (TextView) findViewById(R.id.tvStatus); 

     Browse.setOnClickListener(new OnClickListener() { 
        public void onClick(View arg0) { 
         Intent intent = new Intent(); 
         intent.setType("image/*"); 
         intent.setAction(Intent.ACTION_GET_CONTENT); 
         startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); 

        } 
       }); 
     send.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       Socket sock; 
       try { 
        sock = new Socket("MY_PCs_IP", 1149); 
        File myFile = new File (selectedImagePath); 
        byte [] mybytearray = new byte [(int)myFile.length()]; 
        FileInputStream fis = new FileInputStream(myFile); 
        BufferedInputStream bis = new BufferedInputStream(fis); 


    bis.read(mybytearray,0,mybytearray.length); 
       OutputStream os = sock.getOutputStream(); 
       System.out.println("Sending..."); 
       os.write(mybytearray,0,mybytearray.length); 
       os.flush(); 
       sock.close(); 
      } catch (UnknownHostException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == SELECT_PICTURE) { 
      Uri selectedImageUri = data.getData(); 
      selectedImagePath = getPath(selectedImageUri); 
      TextView path = (TextView) findViewById(R.id.tvPath); 
      path.setText("Image Path : " + selectedImagePath); 
      img.setImageURI(selectedImageUri); 
     } 
    } 
} 

public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 
} 
+3

看起来你正在UI线程上进行网络操作。不要这样做。 Google“NetworkOnMainThreadException” – 323go 2013-02-11 15:18:33

+5

在寻求帮助时发生崩溃时,习惯张贴您的logcat输出是个好习惯。 – Karakuri 2013-02-11 15:45:24

+0

请发布logcat的详细信息。 – Pihu 2013-11-29 10:17:46

您正在执行长UI上运行的任务(主)线程。最好考虑使用AsyncTask来执行发送操作。