0

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.

Liyara
  • 1
  • Try refreshing your workspace, at a glance your code looks good. You can also try adding `android:onClick="sendSignal"` to your buttons xml to directly call sendSignal without having to programmatically assign the listener. – zgc7009 Aug 18 '14 at 20:59
  • I refreshed as you suggested, but it had no effect. I had actually tried the 'android:onClick="sendSignal"' method initially, since it seemed much easier, but I switched to this method because that one wasn't working either. – Liyara Aug 18 '14 at 21:21
  • Other than noticing you missed `@Override` for your onClick, which may solve it (but doesn't solve why setting it in xml wouldn't work), have you tried making sure you have the `` header to your xml file? – zgc7009 Aug 18 '14 at 21:24

1 Answers1

0

You need to use the @override method of on click and in between that you need to find the id of your 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_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);
    }

  }

}
Ende Neu
  • 15,581
  • 5
  • 57
  • 68
TaRan LaYal
  • 148
  • 1
  • 9