-5

all. I've searched, but can't find anything that helps me fix this problem.

My app quits almost immediately on launch due to a NullPointerException. I suspect maybe the problem is that I'm using a drawable image (ImageView) rather than a button (View) as the thing you click on.

Thanks for any help. This has been driving me bonkers for hours!

Code is as follows:

package com.example.imageswitchermk2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener{
ImageView blankTile;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    blankTile = (ImageView) findViewById(R.drawable.blank); 
    blankTile.setOnClickListener(this); //LINE 19
}
    public void onClick(View v) {
        blankTile.setId(R.drawable.zero);
    }
}

and stack is as follows:

Thread [<1> main] (Suspended (exception NullPointerException))  
<VM does not provide monitor information>   
MainActivity.onCreate(Bundle) line: 19  
MainActivity(Activity).performCreate(Bundle) line: 5231 
Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1087   
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2148    
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2233 
ActivityThread.access$800(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 135    
ActivityThread$H.handleMessage(Message) line: 1196  
ActivityThread$H(Handler).dispatchMessage(Message) line: 102    
Looper.loop() line: 136 
ActivityThread.main(String[]) line: 5001    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 515  
ZygoteInit$MethodAndArgsCaller.run() line: 785  
ZygoteInit.main(String[]) line: 601 
NativeStart.main(String[]) line: not available [native method]  

2 Answers2

6
blankTile = (ImageView) findViewById(R.drawable.blank); 

you can not use R.drawable to retrieve items. You have to use R.id

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • no @FrankN.Stein. That's correct. He can actually set the same id of the drawable. But set id, generates an entry for `R.id` – Blackbelt Jul 28 '14 at 07:41
  • Yes. I've retrieved drawables by R.drawable before. That's the point of having it. :-) – user3451652 Jul 28 '14 at 07:50
  • @user3451652 I did not get your comment, – Blackbelt Jul 28 '14 at 07:51
  • Thanks. That did stop the premature exit. Unfortunately, clicking still doesn't work. I suspect that's because I'm trying to retrieve the replacement image (zero) using R.drawable. But I can't retrieve "zero" using R.id because it's not in my layout XML file (or displayed on the screen). So it doesn't have an id . The only way to reference it is via its R.drawable number. So clicking doesn't work. Thanks for any further insights you might have! – user3451652 Jul 28 '14 at 07:53
  • @user3451652 use blankTile.setDrawableResource(R.drawable.zero); see below answer. – Giru Bhai Jul 28 '14 at 07:55
  • you issue is related to `setOnClickListener`. If you did not declare an `id` for the view, inside your `xml` you can not retrieve it through `findViewById` – Blackbelt Jul 28 '14 at 07:55
  • What I mean is, the point of having an "R.drawable" reference (or any reference) in the first place is simply to have a value by which to reference something. – user3451652 Jul 28 '14 at 07:55
  • @Giru Bhai: Thanks! You're a little off, but it was close enough to get me there. Because the class is ImageView, the method isn't setDrawableResource, but setImageResource. Thank you!! – user3451652 Jul 28 '14 at 08:05
  • It's kind of strange that they use "Image" for "Drawable," since a drawable is what's being retrieved. :-) – user3451652 Jul 28 '14 at 08:19
  • you mean with `findViewById` ? – Blackbelt Jul 28 '14 at 08:22
6

To get Id of View,use R.id instead of R.drawable,So change

blankTile = (ImageView) findViewById(R.drawable.blank); 

to

blankTile = (ImageView) findViewById(R.id.blank); 

And set drawable using blankTile.setImageResource(R.drawable.zero);

Giru Bhai
  • 14,370
  • 5
  • 46
  • 74