3

I created a simple activity that is intended to send two pieces of information to another activity. However, my button isn't registering any click events. Does anyone have any idea why this would be? I expected that by adding the onClick = "onClickWrite" to the XML file that they would be linked but there is no response when clicked. If anyone could help me out, I would greatly appreciate it. I have tried implementing the onClickListener, however, with that implementation, it throws an error on my Toasts.

Activity

public class InitializeClass extends Activity {

    private Service service;
    private Button button;
    EditText infoOne; 
    EditText infoTwo; 

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_control);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(clicked);
        infoOne= (EditText) findViewById(R.id.editText);
        infoTwo= (EditText) findViewById(R.id.editText1);

    }

    private View.OnClickListener clicked = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            if (service != null) {

                int size = 100;
                byte[] byteArray = new byte[size];
                byte[] byteArrayTwo = new byte[size];
                byteArray = infoOne.getText().toString().getBytes(Charset.defaultCharset());
                byteArrayTwo= infoTwo.getText().toString().getBytes(Charset.defaultCharset());

                if ((!(infoOne.getText().toString().isEmpty())) && (!(infoTwo.getText().toString().isEmpty()))) {
                    service.setInfo(byteArray);
                    service.setInfoTwo(byteArrayTwo);
                    intentMethod();
                }
            }
        }
    };

    public void intentMethod() {
        Intent intent = new Intent(this, DeviceScanActivity.class);
        startActivity(intent);
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".InitializeClass">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="@string/send_info"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText1" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="150dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColorHint="@android:color/white"
        android:textCursorDrawable="@drawable/color_cursor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="info"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColorHint="@android:color/white"
        android:textCursorDrawable="@drawable/color_cursor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

</android.support.constraint.ConstraintLayout>
Community
  • 1
  • 1
  • Have a look at [this](https://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene) – deHaar Aug 07 '18 at 14:11
  • your java class name is not matching with your xml file – karthik Aug 07 '18 at 14:12
  • @kartarkat Sorry fixed that. I originally changed to class name to focus on the button issue. – LostandConfused Aug 07 '18 at 14:14
  • @deHaar If I'm reading this correctly then my implementation should be correct as I'm implementing the onClick handler in my XML file. – LostandConfused Aug 07 '18 at 14:17
  • @LostandConfused I think so, do you have any error messages or warnings (see LogCat)? Have you tried implementing it in Java only? Does that work? – deHaar Aug 07 '18 at 14:23
  • @deHaar I have no errors or warnings in the LogCat and I have implemented the advice listed below for doing it in Java only with no luck. Will update my code. – LostandConfused Aug 07 '18 at 14:31
  • Maybe you need to initialize the `Button` first? I don't really know if it is necessary and cannot test it at the moment... – deHaar Aug 07 '18 at 14:37
  • @deHaar I actually did that in my updated version. – LostandConfused Aug 07 '18 at 14:41

5 Answers5

3

You need to initialize your button first.

Button button = (Button) findViewById(R.id.button);

Hope this helps!

Angus
  • 3,680
  • 1
  • 12
  • 27
  • Sadly I had no luck with this. I updated my code so that the I was doing it in Java. Any other ideas or mistakes that you might see? – LostandConfused Aug 07 '18 at 14:39
1

In your activity class assign the button to the id of the button in the xml:

private Button btn = findViewById(R.id.button);

You will then create an OnClickListener:

    private View.OnClickListener clicked = new View.OnClickListener(){
    @Override
    public void onClick(View view) {
        //Insert Code here
    }
};

In your onCreate method in the same activity class you will instantiate the button and assign the button to the onClickListener:

btn = new Button(this);
btn.setOnClickListener(clicked);
1

There are couple of things you need to do for that.

  1. Create Field.

    private Button button;

  2. Initialize button.

    Button button = (Button) findViewById(R.id.button);

  3. Set Listener on button.

    button.setOnClickListener();

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
Shaiv
  • 41
  • 2
  • Sadly I had no luck with this. I updated my code so that the I was doing it in Java. Any other ideas or mistakes that you might see? – LostandConfused Aug 07 '18 at 14:40
0

I found the answer. Service in if (service != null) was never being initialized. Stupid mistake on my part. Thanks, everyone for the help and directing me in a way to implement onClickListener!

0

You need to cast the button (name) with you Java code . for example on xml you have id/button (name) declare it on your Java file, and do the casting. and the onClickListener will look like this :

Cast the button : Button button ;

Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
Zoe
  • 27,060
  • 21
  • 118
  • 148
Elio Lako
  • 1,333
  • 2
  • 16
  • 26