为什么我的活动自动重启? [英] Why is my activity restarting automatically?

查看:141
本文介绍了为什么我的活动自动重启?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用程序开发的初学者。最近,开始研制聊天应用。我实现了客户端服务器消息交换的基本功能。我面临着Android的活动就会自动重启的问题。正因为如此,创建视图一个新的实例,我不能看到列表视图中的消息。

I am beginner in android app development. Recently, started to develop a chat app. I achieved basic functionality of client server message exchange. I am facing an issue that android activity gets restarted automatically. Because of this, a new instances of views are created and am not able see the messages on list view.

从痕迹,我可以看到,活动停止,自动创建。因此,主要活动的新实例重新创建。

From the traces, I can see that activity is stopped and created automatically. Hence, a new instance of MAIN activity is created again.

12-21 14:47:50.744    1913-1913/? I/System.out﹕ **********MainActivity.onCreate()-->com.chat.client.MainActivity@1e611def
12-21 14:48:11.209    1913-1913/chat.com.android_client I/System.out﹕ ********onStop() app is killed!!!
12-21 14:48:11.216    1913-1913/chat.com.android_client I/System.out﹕ **********MainActivity.onCreate()-->com.chat.client.MainActivity@7ea43db

[ MainActivity.java ]

package com.chat.client;

import android.app.Activity;
import android.database.DataSetObserver;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.chat.client.config.ChatProtocolConstants;
import com.chat.client.config.ServerUriConstants;
import com.chat.client.connect.ChatServerListener;
import com.chat.client.connect.ChatWebSocketHandler;
import com.chat.client.connect.ConnectionManager;

import chat.com.android_client.R;
import de.tavendo.autobahn.WebSocketHandler;


public class MainActivity extends Activity {

    private ChatArrayAdapter chatArrayAdapter;
    private ListView listView;
    private EditText chatEditText;
    private Button buttonSend;

    private boolean side = false;

    private ConnectionManager connectionManager;
    private final String from = "Ramu", to = "Omkar";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("**********MainActivity.onCreate()-->"+this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        buttonSend = (Button) findViewById(R.id.buttonSend);

        listView = (ListView) findViewById(R.id.listView1);


        chatArrayAdapter = new ChatArrayAdapter(getApplicationContext(), R.layout.activity_chat_singlemessage);

        chatEditText = (EditText) findViewById(R.id.chatText);

        chatEditText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    ChatMessage chatMessage = new ChatMessage(false, chatEditText.getText().toString());
                    return sendChatMessage(chatMessage);
                }
                return false;
            }

        });

        buttonSend.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ChatMessage chatMessage = new ChatMessage(false, chatEditText.getText().toString());
                sendChatMessage(chatMessage);
            }
        });

        listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        listView.setAdapter(chatArrayAdapter);

        chatArrayAdapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                listView.setSelection(chatArrayAdapter.getCount() - 1);
            }
        });

        // Connect to chat server
        connectToServer();

    }

    @Override
    protected void onStop() {
        super.onStop();

        System.out.println("********onStop() app is killed!!!");
    }

    private boolean sendChatMessage(ChatMessage chatMessage) {
        connectionManager.sendMessage(from + ChatProtocolConstants.DELIMITER + to + ChatProtocolConstants.DELIMITER + chatMessage.message);
        return updateChatList(chatMessage);
    }

    private boolean updateChatList(ChatMessage chatMessage) {
        chatArrayAdapter.add(chatMessage);
        chatEditText.setText("");

        return true;
    }


    private void connectToServer() {

        connectionManager = ConnectionManager.getInstance();
        WebSocketHandler chatWebSocketHandler = new ChatWebSocketHandler(from);

        ((ChatWebSocketHandler) chatWebSocketHandler).setWebSocketListener(new ChatServerListener() {
                                                                               @Override
                                                                               public void messageReceived(String message) {
                                                                                   ChatMessage chatMessage = new ChatMessage(true, message);
                                                                                   System.out.println("Received message from web socket handler: " + message);
                                                                                   updateChatList(chatMessage);
                                                                               }
                                                                           }
        );
        connectionManager.connect(ServerUriConstants.serverUri + ServerUriConstants.connectEndPoint, chatWebSocketHandler);

    }
}

[ activity_main.xml中]

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="80dp"></ListView>

<RelativeLayout
    android:id="@+id/form"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="vertical">

    <EditText
        android:id="@+id/chatText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/buttonSend"
        android:ems="10"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/chatText"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:text="@string/button_send" />

</RelativeLayout>

我评论呼吁connectToServer()来检查的行为却是相同的。所以没有造成connectToServer()。

I commented call to connectToServer() to check the behaviour but it is the same. So there is no issue caused by connectToServer().

以下是我使用在Android Studio中来运行这个应用程序模拟器

由于活动的自动重启预计不会有什么可以在这里失踪。请建议。

Following is the emulator which I am using in android studio to run this app Since automatic restarting of an activity is NOT expected, what could be missing here. Please suggest.

推荐答案

是的,有采用的是Android 1.0的工作室和Ubuntu 14.04使用模拟器棒棒糖一个问题。
它被赋予以下错误:

Yes there is a problem with emulator for Lollipop in android studio 1.0 and Ubuntu 14.04. It was giving following error:

12-21 15:47:02.512    1900-1916/chat.com.android_client W/EGL_emulation﹕ eglSurfaceAttrib not implemented
12-21 15:47:02.512    1900-1916/chat.com.android_client W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6da4140, error=EGL_SUCCESS

由于此的onCreate的()是得到在这种情况下叫了两声。

Because of this onCreate() was getting called twice in this case.

我切换到奇巧模拟器,它现在是罚款。

I switched to KitKat emulator and it is fine now.

这篇关于为什么我的活动自动重启?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆