0

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

6
  • Are you asking a question? It looks like you have some code, but what is the problem? Commented May 14, 2015 at 6:13
  • what is there in main Activity line number 133? Commented May 14, 2015 at 6:17
  • try to call setContentView(R.layout.hotspot1); berfore spot1 initialization. Commented May 14, 2015 at 6:18
  • @DroidMind public void info(View view){ setContentView(R.layout.spotlist); } Commented May 14, 2015 at 6:19
  • It seems to be a nullpointer, and it may be because "spot1==null". Try: 1-setting your content view before this call, 2-checking if you have initialized your variable, 3- checking if the id for your "findViewById" is the correct one. Commented May 14, 2015 at 6:21

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.