I'm very new to both Java and the Android SDK, so I apologize if this question is silly. I'm writing an app that should display a simple button, and send a message to a local server when clicked. However, when I tap the button, nothing happens. Here's my code:
.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>
I've looked through all the button documentation and also looked at similar questions here, but nothing seems to work. I've made sure to test the code inside sendSignal in a separate program and the message is received by the server with no issues.