Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

java - Android Studio: OnClickListener grayed out and won't implement

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.

question from:https://stackoverflow.com/questions/66056748/android-studio-onclicklistener-grayed-out-and-wont-implement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
btn_add.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
    }
    });

Where have you defined btn_add in your code ? That is what is causing the error


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...