繁体   English   中英

android模拟器内存​​选项阻止我的应用程序运行

[英]android emulator memory options stop my app from running

我的日食模拟器似乎出现了主要问题,并且不知道它是内存大小还是代码。 我认为我的代码应该运行,就像在Java netbeans项目中那样。

每次我运行应用程序并按下connect按钮时,我都想获取服务器发送回的字符串,然后对其进行处理。 我有一个“过程连接”方法,它读取字符串,但是当我返回它并实际使用返回的内容时,模拟器崩溃

我的代码如下:

  package za.nmmu.wrap302.networks.example02;

  import java.io.IOException;
  import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.net.InetAddress;
 import java.net.Socket;
 import java.util.ArrayList;

 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Context;
 import android.util.Log;
 import android.view.KeyEvent;
  import android.view.View;
 import android.view.View.OnKeyListener;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ListView;
  import android.widget.Toast;

 public class MainActivity extends Activity {
private ListView lstMessages;
private EditText txtMessage;

private ArrayList<String> messages;
private ArrayAdapter<String> adapter;
String message = "";
private ServerConnection connection;

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

    // get references to View objects
    txtMessage = (EditText) findViewById(R.id.txtMessage);
    lstMessages = (ListView) findViewById(R.id.lstMessages);

    // set up adapter
    messages = new ArrayList<String>();
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, messages);
    lstMessages.setAdapter(adapter);

    // attach event listener
    txtMessage.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_ENTER)
                    && (event.getAction() == KeyEvent.ACTION_DOWN)) {
                try {
                    onTxtMessageEnterPressed();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return true;
            }

            return false;
        }
    });
}

public void onBtnConnectClicked(View view) {
    clearMessages();
    connection = new ServerConnection();
    connection.start();
}

public void onTxtMessageEnterPressed() throws IOException {
    if (connection != null) {
        String message = txtMessage.getText().toString();
        txtMessage.getText().clear();
        connection.sendData(message);
    }
}

public void addMessage(String message) {
    adapter.add(message);
}

public void clearMessages() {
    adapter.clear();
}

// the thread that will be communicating with the server
public class ServerConnection extends Thread {
    // the I/O streams that will be receiving/sending data from/to the
    // server
    private ObjectOutputStream output;
    private ObjectInputStream input;

    private Socket client;

    @Override
    public void run() {
        try {
            // Step 1: Create a Socket to make connection
            connectToServer();

            // Step 2: Get the input and output streams
            getStreams();

            // Step 3: Process connection
            processConnection();


            // Step 4: Close connection
            //closeConnection();
        } catch (IOException e) {
            Log.e("CONNECTION", e.getMessage());
        }
    }

    public void addMessage(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MainActivity.this.addMessage(message);
            }
        });
    }

    private void connectToServer() throws IOException {
        addMessage("Attempting connection\n");
        client = new Socket("10.0.0.7", 5001);
        addMessage("Connected to: " + client.getInetAddress().getHostName());
    }

    private void getStreams() throws IOException {
        output = new ObjectOutputStream(client.getOutputStream());
        output.flush();
        input = new ObjectInputStream(client.getInputStream());
        addMessage("Got I/O streams");
    }

           //I would like to call the message below and return it to anywhere else in the code 

    private String processConnection() throws IOException 
    {       
        do {
            try {
                message = (String) input.readObject();
                addMessage(message);
                return message;

            } 
            catch (ClassNotFoundException classNotFoundException) 
            {
                addMessage("ERROR: Unknown object type received");
            }
            return message;
        } while (!message.equals("SERVER>>> TERMINATE"));
    }

    private void sendData(String message) {
        try {
            output.writeObject(message);
            output.flush();
            addMessage("CLIENT>>>" + message);
        } catch (IOException ioException) {
            addMessage("ERROR: Error writing object");
        }
    }

    private void closeConnection() throws IOException {
        addMessage("Closing connection");
        output.close();
        input.close();
        client.close();
    }

}
}

每当我从任何地方调用processConnection方法时,我的应用程序似乎就会崩溃。 我的服务器发现我已经发送了邮件,但是客户端没有读取。

http://i.stack.imgur.com/cNu7m.png

我的日志显示:

 06-13 08:18:00.460: D/dalvikvm(1145): GC_FOR_ALLOC freed 45K, 4% free 3076K/3204K, paused 293ms, total 296ms
 06-13 08:18:00.460: I/dalvikvm-heap(1145): Grow heap (frag case) to 3.687MB for 635812-byte allocation
06-13 08:18:00.530: D/dalvikvm(1145): GC_FOR_ALLOC freed 1K, 4% free 3695K/3828K, paused 55ms, total 55ms
 06-13 08:18:02.220: I/Choreographer(1145): Skipped 172 frames!  The application may be doing too much work on its main thread.
06-13 08:18:02.240: D/gralloc_goldfish(1145): Emulator without GPU emulation detected.
06-13 08:18:03.100: I/Choreographer(1145): Skipped 198 frames!  The application may be doing too much work on its main thread.
06-13 08:18:27.660: E/InputEventSender(1145): Exception dispatching finished signal.
06-13 08:18:27.660: E/MessageQueue-JNI(1145): Exception in MessageQueue callback: handleReceiveCallback

模拟器中的ram是否会对此产生影响? 我究竟做错了什么?

您需要使用异步任务来生成上载到其他线程的操作。 Android在单线程模型上工作,使用同一线程进行HTTPRequest可能会导致FATAL异常。 创建一个异步任务并生成上传到它的任务。

AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute(<pass the required parameters here for file upload>);

private class AsyncTaskRunner extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            //Call the function to upload the file here

        }

这是logcat通过Main线程告诉您的任务太多。

暂无
暂无

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

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