【问题标题】:why my AsyncTask class isnt debugged?为什么我的 AsyncTask 类没有被调试?
【发布时间】:2016-04-20 19:04:22
【问题描述】:

我对包含 AsyncTask 的 android 应用程序有疑问。当我尝试调试它时,androidStudio 将我带到 asynctask 的 cmets 部分。我不明白这个问题是由库问题引起的,还是问题出在构造逻辑上:我有一个类

-从 UI 中获取一个整数,然后

-lunch asynctask 连接到服务器并接收数据(将其放在变量上),然后

-重绘用户界面并在文本视图上设置数据

喜欢代码:

public class MainActivity extends Activity
{
    LinearLayout spazioGen;
    TextView[] cell;
    LinearLayout[] rows;
    LinearLayout def;
    LinearLayout solution;
    private Socket client;
    private PrintWriter printwriter;
    private BufferedReader in;
    private EditText textField;
    private TextView serverOut;
    private Button button;
    private String messsage;
    private String inputLine;
    private Integer dimensionChoosed = 10;
    private String word1 = "";
    private String word2 = "";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textField = (EditText) findViewById(R.id.dimension);    
        button = (Button) findViewById(R.id.findCenter);            

        button.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                messsage = textField.getText().toString();    // get the text message on the text field
                textField.setText("");                        // Reset the text field to blank
                dimensionChoosed = Integer.parseInt(messsage.toString());
                SendMessage sendMessageTask = new SendMessage();
                sendMessageTask.execute();
                createGriglia();
            }
        });
    }

    private void createGriglia(Integer dimensionChoosed)
    {
        int i,j;
        spazioGen = new LinearLayout(this);
        spazioGen.setOrientation(LinearLayout.VERTICAL);
        spazioGen.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        spazioGen.setBackgroundDrawable(getResources().getDrawable(R.drawable.border_ext));
        spazioGen.setPadding(5, 5, 5, 5);

        //create an array of textview to fillup with data received from server

        setContentView(spazioGen);
    }

    private class SendMessage extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected Void doInBackground(Void... params)
        {
            try
            {
                client = new Socket("192.168.0.104", 4444); // connect to the server
                printwriter = new PrintWriter(client.getOutputStream(), true);
                printwriter.println(messsage); // write the message to output stream
                printwriter.flush();

                InputStreamReader inputStreamReader = new InputStreamReader(client.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader); // get the client message
                String message = "";


                while ((message = bufferedReader.readLine()) != null && !(message.equals("flag")))
                {
                    word1 = message;
                    //do stuff with data received
                }

                while ((message = bufferedReader.readLine()) != null)
                {
                    word2 = message;
                    //do stuff with data received
                }

                inputStreamReader.close();
                System.out.println(message);
                printwriter.close();
                client.close(); // closing the connection

            } catch (UnknownHostException e)
            {
                e.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid)
        {
            createGriglia(dimensionChoosed);
        }
    }

}

提前致谢

【问题讨论】:

  • 从您的设备中删除您的应用。清理您的项目并再次安装应用程序,看看它是否对您有帮助。
  • 清洁项目没有变化...现在我尝试重新启动并安装最新版本的 android studio...
  • 如果您在client = new Socket("192.168.0.104", 4444); 行设置断点,调试器是否会暂停应用程序?异常是否被doInBackground 中的任何catch 块捕获?您遇到什么类型的问题?
  • 当我尝试进入 SendMessage 类(我使用 AndroidStudio)时,调试器“登陆”了 AsyncTask 超类的注释。所以我永远不会到达“client = new Socket("192.168.0.104", 4444);"行。
  • 删除你的应用,清理并重建项目,Goog Luck

标签: java android android-studio android-asynctask


【解决方案1】:

感谢所有帮助我的人。
我在安装最新版本的 android studio 时解决了这个问题。
我希望这可以帮助遇到我同样问题的人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    相关资源
    最近更新 更多