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

时间:2014-06-13 11:40:51

标签: java android eclipse android-emulator

我的eclipse模拟器似乎遇到了重大问题,并且不知道它是ram大小还是我的代码。在我看来,我的代码应该运行,就像在我的java netbeans项目中一样。

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

我的代码如下:

  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

我的logcat显示:

 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会影响这个吗?我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要使用Async任务生成上传到其他线程。 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通过主线程告诉你的工作太多了。