Reputation: 1
I write an travel APP.There are 20 QR Code in every spot.
When tourist use QR Code scanner scan the QR Code,the ImageButton's image have to change into another image.
The problem on this line : spot1.setImageResource(R.drawable.hotspot1);
If I delete this line, there is no problem.
I don't know how to fix it.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (0 == requestCode && null != data && data.getExtras() != null) {
String result = data.getExtras().getString("la.droid.qr.result");
int spotnum=Integer.valueOf(result);
switch(spotnum){
case 1:
ImageButton spot1=(ImageButton)findViewById(R.id.imageButton1);
spot1.setImageResource(R.drawable.hotspot1);
setContentView(R.layout.hotspot1);
break;
case 2:
setContentView(R.layout.hotspot2);
break;
}
}
}
Here is my Logcat:https://i.sstatic.net/6y2UQ.png
Upvotes: 0
Views: 113
Reputation: 27549
You are not suppose to call setContentView
after setting view resource. call set contentView once in onCreate
without setting any view from xml.
You can then change the layout contents, but do not call setContentView
again.
You are using a image view
, but not calling setContentView
before it. This makes the ImageView
as null, hence the error.
Try following above suggestions,and it will go. Happy Coding.
Upvotes: 0
Reputation: 24848
You can not initialize any Views from xml before calling setContentView :
setContentView(R.layout.hotspot1);
ImageButton spot1=(ImageButton)findViewById(R.id.imageButton1);
spot1.setImageResource(R.drawable.hotspot1);
Upvotes: 1