Home > front end >  why OnClickListner is not workinhg?
why OnClickListner is not workinhg?

Time:06-09

I code the above codes by using the android studio in java The code work fine until I add the onclicklistner and after that, app keeps crashing. I think the problem is on ////(new View.OnClickListener()////the code as the new is not turned to orange color. Highly appriciate if someone can help me with this issue

 public class MainActivity extends AppCompatActivity {

    TextView textView;
    Button button;

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

        //setting the opening text
        textView = findViewById(R.id.text1);
        
        //setting the button
        button = findViewById(R.id.button1);

        //setting click listner
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                        newmethod();
            }
        });

    }
    //declaring new method
    public void newmethod(){

        textView.setText("button clicked");
    }
}

CodePudding user response:

TRY This

Class file

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //setting the opening text
        textView = findViewById(R.id.text1);
        //setting the button
        button = findViewById(R.id.button1);

        //setting click listner
        button.setOnClickListener(v -> newMethod());

    }

    //declaring new method
    private void newMethod() {
    Toast.makeText(getApplicationContext(),"CLICKED",Toast.LENGTH_LONG).show();

        textView.setText("button clicked");
    }
}

XML File

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@ id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="@string/app_name" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@ id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="@string/app_name" />

</androidx.appcompat.widget.LinearLayoutCompat>

CodePudding user response:

The reason the "new" is not turned orange is because Android studio is suggesting you that you can replace this with lambda. It is NOT the reason the app is crashing.

Maybe the application is crashing due to the initialization of button from xml. Check that in xml you have selected "Button" class widget or "AppCompatButton".

  • Related