0

Edit: My original question is below the line. I decided to go with a much simpler approach to setting up a button and assigning a click function. I found it at the following link. He does a good job of explaining the difference between the 2 approaches...

Android User Interface Design: Basic Buttons


I realize this is a popular question, but in all of the examples I've looked at the problem seems to be a simple detail that's been overlooked, and the detail is never the same. I'm sure this is basic. I'm just starting out with programming for Android and this is a modification of existing code.

The app has one button on a blank page, and I want the button click to send an int to my Arduino via the Amarino API. Here is my MainActivity code

package com.example.buttontest1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import at.abraxas.amarino.Amarino;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;


public class MainActivity extends Activity{

    private Button button;
    private static final String DEVICE_ADDRESS = "00:06:66:4B:E4:23";
    public Context foo1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        Amarino.connect(this, DEVICE_ADDRESS);        
        setContentView(R.layout.main); 
        addListenerOnButton();        
    }

    public void addListenerOnButton() {

        //Select a specific button to bundle it with the action you want
        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);
            }

        });

    }

    protected void onStop() {
        super.onStop();
        // stop Amarino's background service, we don't need it any more 
        Amarino.disconnect(this, DEVICE_ADDRESS);
    }
}

The error I see is this, referring to line 38:

The method sendDataToArduino(Context, String, char, int) in the type Amarino is not applicable for the arguments (new View.OnClickListener(){}, String, char, int)

So there's a problem with the context and the method?

3 Answers3

0

Teh api expects the object of Context but you pass this to:

            Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);

this is not an object of Context instead it is OnClickListener object.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);

this here refers toView.OnClickListener's current instance. The compilation error basically says, sendDataToArduino() expects the first argument as Context but you are passing a OnClickListener

rocketboy
  • 9,573
  • 2
  • 34
  • 36
0

sendDataToArduino expects its first argument to be of type Context. You are passing it a View.onClickListener. Instead of passing this as the first argument, try setting up a context as mentioned here and pass that as the first argument.

Try adding the following in your onCreate method after the super call:

MainActivity.context = getApplicationContext();

Also add the following method after onCreate:

public static Context getAppContext() {
    return MainActivity.context;
}

Now call the method with:

Amarino.sendDataToArduino(getApppContext(), DEVICE_ADDRESS, 'j', 1);
Community
  • 1
  • 1
Adam Johns
  • 35,397
  • 25
  • 123
  • 176
  • I have tried replacing "this" with "this.getApplicationContext()", "getContext()", "Amarino.this", and "MainActivity.this". None have worked. – user2691214 Aug 17 '13 at 04:28
  • did you try setting up a context as mentioned in the link? – Adam Johns Aug 17 '13 at 04:29
  • I'm having trouble following those instructions because I'm still new to this. It says to add "android:name="com.xyz.MyApplication">" to my Manifest file. Am I supposed to replace "xyz.MyApplication" with my information or should I type that verbatim? – user2691214 Aug 17 '13 at 04:36
  • I don't think you'll have to worry about the manifest part since you already have a working activity. I will update my answer to give more details. – Adam Johns Aug 17 '13 at 04:38
  • Also, I don't know where to put the "public class MyApplication..." declaration. I get errors when I put it in the MainActivity. – user2691214 Aug 17 '13 at 04:47
  • Yeah don't use MyApplication. Your class is called MainActivity. See my updated answer. – Adam Johns Aug 17 '13 at 04:48
  • Regarding the updated answer, the MainActivity.context line yields the error: "context cannot be resolved or is not a field". Same for the "return..." line. – user2691214 Aug 17 '13 at 05:00