Android Studio: OnClickListener grayed out and won’t implement

  android, java, onclicklistener

First time, long time. I have tried every which way to get the OnClickListener adapter work and every time I try to implement it is grayed out using the method below. It has been occurring across mulitple different projects.

'
   btn_add.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
        }
        });

'

Obviously, when I run the code it gives a nullpointerexception.

Using the code I’ve included below one listener, AddListener works and ViewAllListener does not. I get a nullpointerexception for the ViewAllListener again. I was following along with a tutorial and ran into the same problem. My work around was to use similar code to what I submitted below. It fixed it for that project, but now I’m having it in a completely different project. Except this time it is only with one listener and not the other. I’m stumped. For a different project I tried the " implements View.OnClickListener" attached after the extends AppCompatActivity. I still ran into the same problem with the OnClickListner grayed out and throwing a nullpointerexception when run. Any help would be greatly appreciated.

`

    import androidx.appcompat.app.AppCompatActivity; 
    import android.view.View.OnClickListener;
    
    public class MainActivity extends AppCompatActivity {
        Button btn_add_horse_ll, btn_viewAll_LL;
        private final OnClickListener AddListener = new OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Success " + success, Toast.LENGTH_SHORT).show();
            }
        };  
        private final OnClickListener ViewAllListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                DatabaseHelper dataBaseHelper = new DatabaseHelper(MainActivity.this);
                ShowHorsesOnListView(dataBaseHelper);
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);       
            btn_add_horse_ll = findViewById(R.id.btn_add_horse_ll);
            btn_viewAll_LL = findViewById(R.id.btn_viewAll_LL);
            btn_add_horse_ll.setOnClickListener(AddListener);
            btn_viewAll_LL.setOnClickListener(ViewAllListener);
      }
        public OnClickListener getAddListener() {
            return AddListener;
        }
        public OnClickListener getViewAllListener(){
            return ViewAllListener;
        }

`

Here’s the xml:

`  
 

    <LinearLayout>
        <Button android:id="@+id/btn_add_horse_ll"/>
       </LinearLayout>
      <LinearLayout>   
      <Button android:id="@+id/btn_viewAll_LL"/>  
      </LinearLayout>

`

I’m not sure what else to say or what other questions to ask, but it keeps telling me I have too much code. I have tried starting over from a new program and still end up with the same problem.

Source: Android Questions

One Reply to “Android Studio: OnClickListener grayed out and won’t implement”

Leave a Reply to Leandro Remorini Cancel reply