8 Replies - 886 Views - Last Post: 23 March 2013 - 05:10 PM

#1 jjames967  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 01-January 12

Inheriting Resources From Parent Class Issues

Posted 21 March 2013 - 11:32 PM

I currently have my main class extending Activity. I have another class extending the main class. For some reason, I'm getting a "source not found error" when importing a resource in the subclass. the statement works fine in the main class, but I put the exact statement in the subclass and it causes a runtime error.

getResources().getDrawable(R.drawable.knight);

my main file class is set up as

public main extends Activity{}
public subclass extends main{}

Should I be implementing something in the subclass?

Is This A Good Question/Topic? 0
  • +

Replies To: Inheriting Resources From Parent Class Issues

#2 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Inheriting Resources From Parent Class Issues

Posted 22 March 2013 - 01:09 AM

:code:/>

I might be reading this thread clumsily, which I apologise in advance for, but I really don't understand your question. If you could post more of your code, I could provide a more comprehensive answer. I'll try to help you, though.

What are you trying to do with getResources().getDrawable(R.drawable.knight)?
Remember: The left side of an equation must always be a variable

There are a few issues here that we might be able to help you with, but it essentially comes down to a gap in your fundamental knowledge of classes and polymorphism.

First of all, you haven't declared your classes as classes.

public main extends Activity{}
public subclass extends main{}



should be

public class main extends Activity{}
public class subclass extends main{}



and they should be in different files as any single file can contain only one publicly visible class.

Secondly, if you want to modify a method or field contained in a parent class, you have to use the protected (or public) visibility modifier.

import android.app.Activity;
public class main extends Activity
{
    protected Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.knight);
    //code
}

class subclass extends main
{
    private Bitmap bmp = super.bmp;
}



or

import android.app.Activity;
public class main extends Activity
{
    public static Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.knight);
    //code
}

class subclass extends main
{
    private Bitmap bmp = main.bmp;
}



Does that answer your question? You can use the super. prefix to specify a method or field that belongs to a parent class. However, you shouldn't really need to. You can usually override fields without using any special modifiers. If it's a variable declared in the constructor of the parent class, you should just be able to use the this. prefix. If you're calling the field using main.field without first instantiating an object (which you don't do through polymorphism anyway: you construct the parent class using the super() method), you will need to make the parent field static.

I'm sorry if none of this makes sense. You kind of have to work your way up to it through lessons/tutorials/lectures. It just sounds like a blur of information if you don't have a fundamental understanding of the concepts.

Ultimately, you will need to post more of your code. Even if all we can do it point you towards a suitable tutorial or documentation page, we need more information about your problem.

This post has been edited by Gungnir: 22 March 2013 - 01:24 AM

Was This Post Helpful? 0
  • +
  • -

#3 jjames967  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 01-January 12

Re: Inheriting Resources From Parent Class Issues

Posted 22 March 2013 - 05:18 PM

Okay, this is what works

public class DungeonReality extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

      // this works fine
      Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.knight)), 512, 512));

      // creating to object
      Entity entity = new Entity();
      entity.addTexture();
   }
}



This isn't working (giving a "source not found" error)

public class Entity extends DungeonReality{

   Texture texture;

   public void addTexture()
   {
      // this is where the error occurs
      texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.knight)), 512, 512));
   }
}



Was This Post Helpful? 0
  • +
  • -

#4 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Inheriting Resources From Parent Class Issues

Posted 22 March 2013 - 09:59 PM

That's better. :D/>

getResources() is a method of the View class. You should be importing android.view.View or android.view.SurfaceView.

Then instantiate an object of View and attribute the getResources() method to it.
import android.view.View;

public class Entity extends DungeonReality{

   Texture texture;
   View view = new View();

   public void addTexture()
   {
      // this is where the error occurs
      texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(view.getResources().getDrawable(R.drawable.knight)), 512, 512));
   }
}



Alternatively, just don't declare the field again. If you need to override the texture field, you could just get it directly from the parent class:

public class Entity extends DungeonReality{

   Texture texture;

   public void addTexture()
   {
      // this is where the error occurs
      this.texture = super.texture;
   }
}



This is terribly redundant, though. Protected fields that belong to the parent class also belong to the child class. If you need to add NEW textures, then you'll need a method in your parent class that takes a Texture as a parameter:

public class DungeonReality extends Activity
{
    //code and stuff
    Texture texture;
    int height, width;

    protected void setTexture(Texture texture, height, width)
    {
        this.texture = texture;
        this.height = height;
        this.width = width;
    }
}



public class Entity extends DungeonReality {

   public void addTexture(Texture texture)
   {
      super.addTexture(texture, texture.getHeight(), texture.getWidth());
   }
}



THEN instantiate Entity somewhere else. Please, please, please don't instantiate a child object in a parent class.

View view = new View();
Entity MyEntity = new Entity();
MyEntity.addTexture(view.getResources().getDrawable(R.drawable.knight)));



I'm assuming that 512, 512 are the height and width properties of the texture, in which case, that won't work on all devices. You need to have a dynamic height/width based on the dimensions of the resource that you're using.

If this didn't come even remotely close to answering your question, let me know. If it did, let me know using the Posted Image button.

Here are a few links that may help you more than I can. For I am just a flimsy mortal, and they are websites:
- Android > Displaying Bitmaps Efficiently
- Java > Polymorphism

This post has been edited by Gungnir: 22 March 2013 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Inheriting Resources From Parent Class Issues

Posted 22 March 2013 - 10:16 PM

I just read through your code again and realised what the number 512 is for. You want to shrink the size of the bitmap down. I have a better solution for this.

You will need a View class when it comes to drawing your texture to the screen, obviously. Your canvas should be the screen, but when doing the drawing, you can paste a rect over another rect:

int desiredHeight = 512;
int desiredWidth = 512;

public void draw(Canvas canvas)
{	
	Rect source = new Rect(textureX, textureY, 
			(textureX + texture.getWidth()), (textureY + texture.getWidth()));
	Rect destination = new Rect(x, y, (x + desiredWidth), (y + texture.desiredHeight));		
	canvas.drawBitmap(texture, source, destination, null);
}



As you can see, drawBitmap can be overloaded with two rects using Canvas.drawBitmap(Bitmap, Rect, Rect, Paint). The first rect is your texture file. The second rect is the rect that you want it to take up on the screen.

More links:
- Android > Rect
- Android > Canvas
Was This Post Helpful? 0
  • +
  • -

#6 jjames967  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 01-January 12

Re: Inheriting Resources From Parent Class Issues

Posted 22 March 2013 - 10:46 PM

Thanks. I'll look into it more. I'm not really concerned about the dimensions. I'm just confused why the main parent class is able to access resources, but the child can't.
Was This Post Helpful? 0
  • +
  • -

#7 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Inheriting Resources From Parent Class Issues

Posted 23 March 2013 - 12:23 PM

Because to access drawable resources, you first need a context. In any given workspace there might be several linked projects, each with their own drawable resources.

I would say that the context of your parent class is private, and doesn't extend to the child class.

I'm not saying that this will work, but try passing the context down to the child class in a protected method.

public class DungeonReality extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

      private Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.knight)), 512, 512));
      
      protected Context getContext()
      {
         return getApplicationContext(); // OR return this
      }
   }
}



public class Entity extends DungeonReality
{
   private Context context = super.getContext();
   private Texture texture;

   public void addTexture()
   {
      texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(getResources().getDrawable(R.drawable.knight)), 512, 512));
   }
}



I've provided a few possible solutions now. Try them out and tell me whether or not the error still occurs.

I'm sorry that I can't be of more assistance right now. To tell you the truth, I don't really understand your program structure.

Further reading:
- Android > Context

This post has been edited by Gungnir: 23 March 2013 - 12:36 PM

Was This Post Helpful? 1
  • +
  • -

#8 jjames967  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 01-January 12

Re: Inheriting Resources From Parent Class Issues

Posted 23 March 2013 - 04:47 PM

Thank to your help and a little bit of research, I figured it out.

public class DungeonReality extends Activity{
    private static Context context;

    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        if (context == null)
             context = this;
    }

    public static Context getContext(){
        return context;
    }
}



Then I could access it in subclasses using
texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(
		DungeonReality.getContext().getResources()
                         .getDrawable(R.drawable.knight)), 512, 512));


This post has been edited by jjames967: 23 March 2013 - 04:50 PM

Was This Post Helpful? 0
  • +
  • -

#9 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Inheriting Resources From Parent Class Issues

Posted 23 March 2013 - 05:10 PM

So it was a context issue. I'm glad I could help!

Congratulations on figuring it out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1