简体   繁体   English

是什么导致此按钮崩溃?

[英]What is causing this button to crash?

I'm writing an app that allows me to send a command through TCP/IP by clicking a button. 我正在写一个应用程序,它允许我通过单击按钮通过TCP / IP发送命令。 However when I click to the button in the emulator it comes back with a message saying the button has stopped working. 但是,当我单击模拟器中的按钮时,它会返回一条消息,提示该按钮已停止工作。 I was wondering if anyone could spot the error in my code. 我想知道是否有人可以在我的代码中发现错误。

CODE: 码:

package button.test;  
import java.io.IOException;  
import java.io.OutputStream;  
import java.net.Socket;  
import java.net.UnknownHostException;  
import android.app.Activity;  
import android.os.Bundle;
import android.view.View;  

public class ButtonActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void onClick(View view) throws UnknownHostException, IOException
    {
        Socket socket = new Socket("192.168.2.92", 60128);
        try{
            dataOutputStream.Object BUFFER = null;
            write(BUFFER);
            String buffer="ISCP000000100000000701000000!1PWR010D";
            OutputStream os=socket.getOutputStream();
            os.write(buffer.getBytes()); 
        } catch(IOException e)
        {
            //error code
        }

    }
    private void write(Object BUFFER) {
        // TODO Auto-generated method stub

    }
}

1. You missed declaring the button, and initializing it... 1.您错过了声明按钮并对其进行初始化的操作...

Eg: 例如:

public class ButtonActivity extends Activity {

Button mbutt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mbutt = (Button)findViewById(R.id.Button_Click);
    }

- Make a note that you must initialize the views only after the setContentView() , else your views won't get the id and will make your App to crash. -请注意, 您必须仅在setContentView()之后初始化视图 ,否则您的视图将无法获取ID ,从而使您的应用崩溃。

2. Its always advisable to keep the UI work on UI thread, and Non-UI work on Non-UI thread , but that became a law with the arrival of HoneyComb android version. 2.始终建议使UI在UI线程上工作,而Non-UI在Non-UI线程上工作 ,但这随着HoneyComb android版本的到来而成为法律。

3. You can use Thread with a Handler to sync UI and Non-UI thread. 3.您可以将Thread with a Handler 一起使用来同步UI和Non-UI线程。

4. AsyncTask which is known as Painless Threading was introduced specially in android for this. 4.为此专门在android中引入了称为无痛线程处理的 AsyncTask

See this link for tutorials on Threads, Handlers and AsyncTask: 请参阅此链接以获取有关线程,处理程序和AsyncTask的教程

http://www.vogella.com/articles/AndroidPerformance/article.html http://www.vogella.com/articles/AndroidPerformance/article.html

It looks like the socket connection work is taking a long time. 看起来套接字连接工作需要很长时间。 Suggest putting this functionality inside an AsyncTask. 建议将此功能放在AsyncTask中。

您尚未在onCreate()声明按钮

You don't need to declare the button if you are inflating an xml. 如果要扩展xml,则无需声明按钮。 Your problem is that you run a Network connectivity on a main Thread which is not allowed since api 11. Use a asynctask or a thread for this. 您的问题是您在自api 11以来不允许的主线程上运行网络连接。为此请使用asynctask或线程。

eg 例如

new Thread() {
public void run() {
    Socket socket = new Socket("192.168.2.92", 60128);
    try{
        dataOutputStream.Object BUFFER = null;
        write(BUFFER);
        String buffer="ISCP000000100000000701000000!1PWR010D";
        OutputStream os=socket.getOutputStream();
        os.write(buffer.getBytes()); 
    } catch(IOException e)
    {
        //error code
    }
  }
}.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM