Setonclicklistener() on a Null Object Reference Error

ImageView.setOnClickListener ERROR null object reference

The correct order should be :

     setContentView(R.layout.main);
first = (ImageView) findViewById(R.id.first_image);
second_img = (ImageView)findViewById(R.id.second_image);

Otherwise, first and second_img will be null.

OnClickListener on a null object reference

Replace

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

with

    btn_back = (Button) view.findViewById(R.id.btn_back);

Do the same for all the other layout elements you wanna initiate.

The reason is: since you inflating a layout file in your Fragment, you have to refer all UI elements to THAT file, that you called "view" here:

     View view = inflater.inflate(R.layout.add_ip, container, false);

So the istruction "findViewById" has to be refered to that View view.

How to fix OnClickListener being a null object reference

Your button object may be null. Make sure to have

Button button = (Button) findViewById(my_button_id);

in your code, and make sure that you are getting a valid ID. However, I don't know for sure until you share the rest of your code.

Error:android.view.View$OnClickListener on a null object reference

Problem is:

You activity is using following layout: R.layout.activity_funny_quotes

But your button is inside: R.layout.activity_list_funny

This way, findViewById returns null and i == null

When will not be null

i=(ImageView)findViewById(R.id.imgbtn_favorite1); will return a valid ImageView only after you set the adapter to your ListView (list.setAdapter(adapter)). This way, the ImageView will be added to the screen and findViewById(R.id.imgbtn_favorite1) may return a valid ImageView

Even then, your list must not be empty. If the list is empty, no ImageView will be returned to the screen and you will keep receiveing null

There's another problem: if your list has several items, findViewById(R.id.imgbtn_favorite1) will return only the ImageView of the first item.

How to Fix

It's hard to tell you how to proper fix your issue.

You are trying to add a clickListener to an ImageView that will be displayed in your ListView.

Maybe, it is better to define a ListView.setOnItemClickListener() instead to add a clickListener to your ImageView

setOnClickListener Error in Android Studio

you have to initialize back_button before you attach anything to it

back_button = findViewById(R.id.back_button);
back_button.setOnClickListener{ ... rest of code

you have to get reference to every View you want to set up by code further, so also button_login_sekarang and buatAkun

alsoe ensure that all these Views are placed inside set layout (R.layout.daftar)

ImageView setOnClickListener on a null object reference in custom adapter

(android.view.View$OnClickListener)' on a null object reference Kotlin

Your next1 variable is never declared or initialized. I hope this is declared somewhere, otherwise, the code should have got a compilation error.

Hence my observation is the next1 variable was not initialized. You need to declare the next1 as a button and then initialize the next1 variable using the findViewById method to tag this button with the specific view reference id from your layout.

class MainActivity : AppCompatActivity() {

var next1: Button? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
next1 = findViewById(R.id.button) as Button
NumberVerButton()
}

private fun NumberVerButton() {
next1?.setOnClickListener {
startActivity(Intent(this, MainActivity::class.java))
}
}
}

Hope that helps.



Related Topics



Leave a reply



Submit