我对Java和Android SDK都很陌生,所以如果这个问题很愚蠢,我很抱歉。我正在写一个应用程序,应该显示一个简单的按钮,并在点击时发送消息到本地服务器。然而,当我点击按钮时,什么也没发生。下面是我的代码:
. java
package com.example.hellokindlefire;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HelloKindleFireActivity extends Activity implements OnClickListener {
private Socket socket;
private PrintWriter pw;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_kindle_fire);
button = (Button)findViewById(R.id.send_button);
button.setOnClickListener(this);
}
public void onClick(View v) {
sendSignal(v);
}
public void sendSignal(View v) {
System.out.println("Sending...");
try {
socket = new Socket("192.168.1.100", 63400);
pw = new PrintWriter(socket.getOutputStream(), true);
pw.println("Hello!");
} catch(IOException e) {
System.out.println(e);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_kindle_fire, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
. xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hellokindlefire.HelloKindleFireActivity" >
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/signal"
android:id="@+id/send_button" />
</RelativeLayout>
我看了所有的按钮文档,也看了这里类似的问题,但似乎没有工作。我已经确保在一个单独的程序中测试sendSignal内部的代码,并且消息被服务器接收,没有问题。
你需要使用on click的@override方法,在此之间你需要找到你的按钮的id。
public class HelloKindleFireActivity extends Activity implements
OnClickListener {
private Socket socket;
private PrintWriter pw;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.send_button);
button.setOnClickListener(this);
}
public void sendSignal(View v) {
System.out.println("Sending...");
try {
socket = new Socket("192.168.1.100", 63400);
pw = new PrintWriter(socket.getOutputStream(), true);
pw.println("Hello!");
} catch (IOException e) {
System.out.println(e);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.send_button) {
sendSignal(v);
}
}
}