按下按钮时未打开活动

时间:2015-03-25 20:32:55

标签: android android-activity

我正在创建一个基本的Android应用程序,其中包含客户端 - 服务器聊天,指向网站的链接以及打开数据库的选项(我尚未创建)。我已经创建了一个按钮来打开数据库的活动,但按下按钮时没有任何反应。

这是我的应用程序主要活动;

public class AndroidChatApplicationActivity extends Activity {

private Handler handler = new Handler();
public ListView msgView;
public ArrayAdapter<String> msgList;

// public ArrayAdapter<String> msgList=new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1);;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

    msgList = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1);
    msgView.setAdapter(msgList);

    // msgView.smoothScrollToPosition(msgList.getCount() - 1);

    Button btnSend = (Button) findViewById(R.id.btn_Send);

    receiveMsg();
    btnSend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            final EditText txtEdit = (EditText) findViewById(R.id.txt_inputText);
            // msgList.add(txtEdit.getText().toString());
            sendMessageToServer(txtEdit.getText().toString());
            msgView.smoothScrollToPosition(msgList.getCount() - 1);

        }
    });

    Button websiteButton = (Button) findViewById(R.id.website_Button);
    websiteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendToWebsite();
        }
    });
}

protected void sendToWebsite() {
    String url = "https://www.ljmu.ac.uk/";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);

    Button databaseButton = (Button) findViewById(R.id.database);
    databaseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(AndroidChatApplicationActivity.this,
                    Database.class));

        }
    });
}

// receiveMsg();
// ----------------------------
// server msg receieve
// -----------------------

// End Receive msg from server//

public void sendMessageToServer(String str) {

    final String str1 = str;
    new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            // String host = "opuntia.cs.utep.edu";
            String host = "10.0.2.2";
            String host2 = "127.0.0.1";
            PrintWriter out;
            try {
                Socket socket = new Socket(host, 8008);
                out = new PrintWriter(socket.getOutputStream());

                // out.println("hello");
                out.println(str1);
                Log.d("", "test");
                out.flush();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.d("", "test2");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.d("", "test3");
            }

        }
    }).start();
}

public void receiveMsg() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub

            // final String host="opuntia.cs.utep.edu";
            final String host = "10.0.2.2";
            // final String host="localhost";
            Socket socket = null;
            BufferedReader in = null;
            try {
                socket = new Socket(host, 8008);
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            while (true) {
                String msg = null;
                try {
                    msg = in.readLine();
                    Log.d("", "MSGGG:  " + msg);

                    // msgList.add(msg);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (msg == null) {
                    break;
                } else {
                    displayMsg(msg);
                }
            }

        }
    }).start();

}

public void displayMsg(String msg) {
    final String mssg = msg;
    handler.post(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            msgList.add(mssg);
            msgView.setAdapter(msgList);
            msgView.smoothScrollToPosition(msgList.getCount() - 1);
            Log.d("", "Hi Test");
        }
    });

}

}

这是我的清单;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.UTEP.android"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:allowTaskReparenting="false"
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <activity
        android:name="androidChat.AndroidChatApplicationActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="androidChat.Database" >
    </activity>
</application>

<uses-permission android:name="android.permission.INTERNET" />

</manifest>

这里是数据库;

import android.app.Activity;
import android.os.Bundle;
import edu.UTEP.android.R;

public class Database extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.database);
}

}

1 个答案:

答案 0 :(得分:1)

正如你所说&#34;没有任何反应&#34;,我假设您的按钮clickListener甚至没有被调用。尝试将其移至OnCreate方法:

  Button databaseButton = (Button) findViewById(R.id.database);
databaseButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(AndroidChatApplicationActivity.this,
                Database.class));

    }
});