So, basically, I have an ImageButton called boutonPortail
, and another called logo
. Initializing boutonPortail
works fine, but when I assign it its listener with boutonPortail.setOnClickListener(boutonPortailListener);
the app crashes, and I don't know why at all. It's not a problem with the listener since when I assign it to logo
it works just fine. I doubt it is a problem with the XML since I copy/pasted the code of logo
.
My app prompts for a password, and if the password is correct it switches to another view with the logo
and the boutonPortail
. Note that the password prompt view also has the logo
.
Is the problem that boutonPortail
is not on the main view? I tried assigning the listener after switching views, but it still crashes.
EDIT: After putting the button in the main view, the problem is definitely that the button is not in the main view, when I put it in the main view it works fine. Why does it crashes though?
Also, for some reason I can't manage to change the image of the button with boutonPortail.setImageResource(R.drawable.boutonfermer);
. (this doesn't happen when it is in the main view)
onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logo = (ImageButton)findViewById(R.id.logo);
boutonPortail = (ImageButton)findViewById(R.id.boutonPortail);
codeEntered = (EditText)findViewById(R.id.codeEntered);
codeSurNotice = (TextView)findViewById(R.id.codeSurNotice);
//attribute all listeners
logo.setOnClickListener(boutonPortailListener);
codeEntered.addTextChangedListener(textWatcher);
codeEntered.setOnKeyListener(codeEnteredListener);
Method to change view:
void codeCorrect() {
setContentView(R.layout.activity_readytopress);
boutonPortail.setOnClickListener(boutonPortailListener); //this line crashes the app, even if put in the onCreate
}
Listener:
private OnClickListener boutonPortailListener = new OnClickListener() {
@Override
public void onClick(View v) {
boutonState++;
if(boutonState>=4)
boutonState=0;
boutonPortail.setImageResource(R.drawable.boutonfermer);
}
};
XML:
<ImageButton
android:id="@+id/boutonPortail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="40dip"
android:src="@drawable/boutonouvrir"
android:background="#00000000"
style="#00000000"
android:layout_gravity="center"
/>
Here's my full code if you want to test it (note that you'll have to call the codeCorrect()
method manually since you don't have access to the bluetooth device I use):
MainActivity.java
http://pastebin.com/ZXDahPZ6 activity_main.xml
http://pastebin.com/f14cVBKj activity_readytopress.xml
http://pastebin.com/0iZm91eq boutonouvrir.png
http://puu.sh/mLGeU.png ouvertureencours.png
http://puu.sh/mLGfI.png boutonfermer.png
http://puu.sh/mLGe5.png fermetureencours.png
http://puu.sh/mLGgW.png
Thanks :)
You do not have a button with the id boutonPortail
in activity_main.xml
It is crashing with a Null Pointer Exception. You cannot add a listener to a null object.
Here is your relevant code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// On récupère toutes les vues dont on a besoin
logo = (ImageButton)findViewById(R.id.logo);
boutonPortail = (ImageButton)findViewById(R.id.boutonPortail);
So you call setContentView(R.layout.activity_main)
and then you call findViewById(R.id.boutonPortail)
The findContentView()
will return null, because acitivty_main.xml
does not have a view ID of that value. So now boutonPortail
is null.
Then you call boutonPortail.setOnClickListener(boutonPortailListener)
which will crash with a Null Pointer Exception because boutonPortail
is null
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.