-1

I have searched all over stackoverflow for a solution to my problem, but I can't find any. I have a button with an id clearly written in my layout file, and using findViewById I am able to get that id. However, when I use setOnClickListener(), I keep receiving the same error.

Edit: I I have tried removingthe OnClickListener() method and created the proceedToNext() method, but when the app launches the TOAST does not show up when I tap the button.

XML layout:

<?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="com.example.enigmadx.flccounter.Main

<Button
    android:id="@+id/btnProceed"   //button id
    android:layout_width="142dp"
    android:layout_height="62dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@color/colorPrimary"
    android:drawableEnd="@drawable/newarr"
    android:drawableRight="@drawable/newarr"
    android:drawableTint="@color/colorWhite"
    android:gravity="center"
    android:text="@string/proceed"
    android:textAlignment="inherit"
    android:textColor="@color/colorWhite"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.666"
    app:layout_constraintStart_toEndOf="@+id/textView"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.923"
    tools:targetApi="m"
    android:layout_marginRight="16dp"
    android:layout_marginLeft="8dp" />

<TextView
    android:id="@+id/txTotal"
    android:layout_width="94dp"
    android:layout_height="44dp"
    android:layout_marginBottom="52dp"
    android:layout_marginStart="52dp"
    android:layout_marginTop="8dp"
    android:text="@string/total"
    android:textAlignment="center"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.982"
    android:layout_marginLeft="52dp" />

Main Class:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Main extends AppCompatActivity implements View.OnClickListener
{

private static final String TAG = Main.class.getSimpleName();

Button proceedBtn;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    proceedBtn = findViewById(R.id.btnProceed);

    //error here
    proceedBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT ).show();
            }
        });
    }

}

Error:

 FATAL EXCEPTION: main
                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.enigmadx.flccounter/com.example.enigmadx.flccounter.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
                   at android.app.ActivityThread.-wrap11(Unknown Source:0)
                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
                   at android.os.Handler.dispatchMessage(Handler.java:105)
                   at android.os.Looper.loop(Looper.java:164)
                   at android.app.ActivityThread.main(ActivityThread.java:6938)
                   at java.lang.reflect.Method.invoke(Native Method)
                   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                   at com.example.enigmadx.flccounter.Main.onCreate(Main.java:33)
                   at android.app.Activity.performCreate(Activity.java:7183)
                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) 
                   at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
                   at android.os.Handler.dispatchMessage(Handler.java:105) 
                   at android.os.Looper.loop(Looper.java:164) 
                   at android.app.ActivityThread.main(ActivityThread.java:6938) 
                   at java.lang.reflect.Method.invoke(Native Method) 
                   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

I would greatly appreciate any help in resolving this.

EnigmaTech
  • 337
  • 5
  • 12

2 Answers2

0

You have already set the onclick event of your button in xml file. Check the

android:onClick="proceedToNext"

line. You can remove it.

0

You have attached the listener within the xml code by using this line

android:onClick="proceedToNext"

Create a method proceedToNext() outside onCreate{}

public void proceedToNext(){
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT ).show();
}

Try this code instead of OnClickListener.

Manzoor Ahmad
  • 481
  • 6
  • 21
  • I have tried that. I removed the OnClickListener and created the `proceedToNext()` method, but it did not show anything – EnigmaTech Sep 07 '18 at 14:15