Problem with drawing bitmap (trying something new now)

Problems with Canvas, OpenGL, etc...

Problem with drawing bitmap (trying something new now)

Postby SolidSora » Sat Nov 06, 2010 4:59 pm

I have an object class and I want it to have its own draw method, then I will call its draw method in my main class. The way I have it now causes the program to crash. Here is my class code:

Code: Select all
package game.testScroller;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;


public class GroundPiece {   
   private Context context;
        Resources res = context.getResources();
   private int x;
   private int y;
   private Paint groundPiecePaint;
   private Bitmap groundPiecePic = BitmapFactory.decodeResource(res, R.drawable.groundpiece);
   
   public GroundPiece(int x, int y){
      this.x = x;
      this.y = y;
   }
   
   public void setX(int x){
      this.x = x;
   }
   
   public int getX(){
      return x;
   }
   
   public void setY(int y){
      this.y = y;
   }
   
   public int getY(){
      return y;
   }
   
   public void moveLeft(){
      this.x -= 5;
   }
   
   public void moveRight(){
      this.x += 5;
   }
   
   public void moveUp(){
      this.y -= 5;
   }
   
   public void moveDown(){
      this.y += 5;
   }
   
   public void draw(Canvas c){
      c.drawBitmap(groundPiecePic, getX(), getY(), groundPiecePaint);
   }

}
Last edited by SolidSora on Mon Nov 08, 2010 3:52 am, edited 1 time in total.
SolidSora
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue Oct 12, 2010 12:56 am

Top

Re: Problem with drawing bitmap

Postby Schermvlieger » Sat Nov 06, 2010 5:48 pm

Check your logcat; are you getting a nullpointerexception?
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Re: Problem with drawing bitmap

Postby SolidSora » Sat Nov 06, 2010 6:05 pm

Code: Select all
11-06 13:04:18.409: ERROR/MTP-LAUNCHER(2316): media scanning not yet finished.
11-06 13:04:19.429: ERROR/Preferences(12723):   recovered user & security token
11-06 13:04:19.469: ERROR/ConnectivityService(2251): connected default network is not mActiveDefaultNetwork!
11-06 13:04:27.779: ERROR/AndroidRuntime(12775): Uncaught handler: thread main exiting due to uncaught exception
11-06 13:04:27.789: ERROR/AndroidRuntime(12775): java.lang.RuntimeException: Unable to start activity ComponentInfo{game.testScroller/game.testScroller.TestScrollerActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class game.testScroller.TestScrollerGame
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.os.Looper.loop(Looper.java:123)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.app.ActivityThread.main(ActivityThread.java:4363)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at java.lang.reflect.Method.invokeNative(Native Method)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at java.lang.reflect.Method.invoke(Method.java:521)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at dalvik.system.NativeStart.main(Native Method)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class game.testScroller.TestScrollerGame
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at game.testScroller.TestScrollerActivity.onCreate(TestScrollerActivity.java:19)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     ... 11 more
11-06 13:04:27.789: ERROR/AndroidRuntime(12775): Caused by: java.lang.reflect.InvocationTargetException
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at game.testScroller.TestScrollerGame.<init>(TestScrollerGame.java:45)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at java.lang.reflect.Constructor.constructNative(Native Method)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     ... 18 more
11-06 13:04:27.789: ERROR/AndroidRuntime(12775): Caused by: java.lang.NullPointerException
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     at game.testScroller.GroundPiece.<init>(GroundPiece.java:13)
11-06 13:04:27.789: ERROR/AndroidRuntime(12775):     ... 22 more
11-06 13:04:48.449: ERROR/SensorManager(2251): unregisterListener: com.android.server.PowerManagerService$9@4777ad90
11-06 13:04:48.509: ERROR/SensorManager(2251): unregisterListener: android.view.WindowOrientationListener$SensorEventListenerImpl@47b791c0


That's my logcat output.
SolidSora
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue Oct 12, 2010 12:56 am

Re: Problem with drawing bitmap

Postby Schermvlieger » Sat Nov 06, 2010 6:51 pm

This is your problem right here:

11-06 13:04:27.789: ERROR/AndroidRuntime(12775): Caused by: java.lang.NullPointerException
11-06 13:04:27.789: ERROR/AndroidRuntime(12775): at game.testScroller.GroundPiece.<init>(GroundPiece.java:13)

Line number 13 of your code
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Re: Problem with drawing bitmap

Postby SolidSora » Sat Nov 06, 2010 7:50 pm

This is my line 13:
Resources res = context.getResources();
SolidSora
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue Oct 12, 2010 12:56 am

Re: Problem with drawing bitmap

Postby Schermvlieger » Sat Nov 06, 2010 9:07 pm

SolidSora wrote:This is my line 13:
Resources res = context.getResources();


I think you need to initialize context, as follows:

Context context = getApplicationContext();

But i always refer to getRecources() directly, that works as well:

private Bitmap groundPiecePic = BitmapFactory.decodeResource(getResources(), R.drawable.groundpiece);
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Top

Re: Problem with drawing bitmap

Postby SolidSora » Sat Nov 06, 2010 9:52 pm

none of those methods worked. It says they are undefined. Is there a certain place I'm supposed to do that or do I need to import something? My code is still the same as above except adding those 2 lines to replace the old ones, which now causes it to not compile.

The class above is just a class I want to have draw an object, it's not my activity class.

Edit: I just tried drawing a plain rectangle with the drawRect method and it works, the problem is definitely with bitmaps and context. Is there a way to use the bitmap without using a context?
SolidSora
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue Oct 12, 2010 12:56 am

Re: Problem with drawing bitmap (trying something new now)

Postby SolidSora » Mon Nov 08, 2010 3:53 am

Now I'm trying to read the image from file instead of resources.

Here is my new code, the program crashes now.

Code: Select all
package game.testScroller;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import game.testScroller.TestScrollerActivity;


public class GroundPiece {   
   TestScrollerActivity mainActivity;
   //Context context = mainActivity.getApplicationContext();
    //Resources res = context.getResources();
   private int x;
   private int y;
   private Paint groundPiecePaint;
   private Paint rectPaint = new Paint();
   private final String path = "C:\\Users\\Public\\Documents\\TestScroller.gif";
   private Bitmap groundPiecePic = BitmapFactory.decodeFile(path);
   
   public GroundPiece(int x, int y){
      this.x = x;
      this.y = y;
      rectPaint.setARGB(255, 255, 255, 0);
   }
   
   public void setX(int x){
      this.x = x;
   }
   
   public int getX(){
      return x;
   }
   
   public void setY(int y){
      this.y = y;
   }
   
   public int getY(){
      return y;
   }
   
   public void moveLeft(){
      this.x -= 5;
   }
   
   public void moveRight(){
      this.x += 5;
   }
   
   public void moveUp(){
      this.y -= 5;
   }
   
   public void moveDown(){
      this.y += 5;
   }
   
   public void draw(Canvas c){
      c.drawBitmap(groundPiecePic, getX(), getY(), groundPiecePaint);
      c.drawRect(getX(), getY(), 200, 300, rectPaint);
      Log.d("GroundPiece", "drawn");
   }

}
SolidSora
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue Oct 12, 2010 12:56 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 4 guests