NullPointerException when closing app - android

I have a bubble level app that I am trying to incorporate into another app I wrote. The app works as it should, except when you push the android back button, it force closes the app with the following exception:
Logcat:
04-08 08:10:15.056: I/SensorManager(21015): Set normal delay = true
04-08 08:10:15.287: W/dalvikvm(21015): threadid=11: thread exiting with uncaught exception (group=0x41ca7ac8)
04-08 08:10:15.287: E/AndroidRuntime(21015): FATAL EXCEPTION: Thread-89255
04-08 08:10:15.287: E/AndroidRuntime(21015): java.lang.NullPointerException
04-08 08:10:15.287: E/AndroidRuntime(21015): at com.golfstuff.ecolevel.MoveSkater$DrawSkater.run(MoveSkater.java:121)
04-08 08:10:15.287: E/AndroidRuntime(21015): at java.lang.Thread.run(Thread.java:856)
Movescater:
package com.golfstuff.ecolevel;
// STAND-ALONE
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class MoveSkater extends Activity implements SensorEventListener, OnTouchListener {
Bitmap level, logo, ball, lines,background, exit;
SensorManager sm;
DrawSkater ourView;
MediaPlayer beepl,beepr;
//private Camera cameraObj;
float x,y,sensorX, sensorY,mid,exitx,exity;
boolean isRunning;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Check for Accelerometer
beepl = MediaPlayer.create(this, R.raw.beepleft);
beepr = MediaPlayer.create(this, R.raw.beepright);
ourView = new DrawSkater(this);
ourView.resume();
ourView.setOnTouchListener(this);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() !=0) {
Sensor s=sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}
lines=BitmapFactory.decodeResource(getResources(), R.drawable.lines);
level=BitmapFactory.decodeResource(getResources(), R.drawable.newlevel);
logo=BitmapFactory.decodeResource(getResources(), R.drawable.ecolevel);
ball = BitmapFactory.decodeResource(getResources(), R.drawable.newball);
background = BitmapFactory.decodeResource(getResources(), R.drawable.levelback);
exit = BitmapFactory.decodeResource(getResources(), R.drawable.btn_exit);
x = y = sensorX = sensorY = 0;
setContentView(ourView);
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sensorX=event.values[1];
sensorY=event.values[0];
}
public class DrawSkater extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = true;
public DrawSkater(Context context) {
super(context);
ourHolder = getHolder();
}
public void pause() {
isRunning = false;
while(true) {
try {
ourThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
finish();
}
break;
}
ourThread = null;
}
public void resume() {
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
#Override
public void run() {
// TODO Auto-generated method stub
while(isRunning) {
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.WHITE); *** JAVA ERROR POINT ***
canvas.drawBitmap(background, canvas.getWidth()/2-640,canvas.getHeight()/2-200,null);
canvas.drawBitmap(logo, canvas.getWidth()/2-230,0,null);
canvas.drawBitmap(level, canvas.getWidth()/2-605,canvas.getHeight()-130,null);
canvas.drawBitmap(exit, 20,140,null);
mid = ((sensorX*(-100))+600);
// Right Side Max
if (mid > 757) {
mid = 757;
} //Left Side Max
else if (mid <422) {
mid = 422;
}
canvas.drawBitmap(ball, mid,494,null);
canvas.drawBitmap(lines,575,497,null);
canvas.drawBitmap(lines,653,497,null);
ourHolder.unlockCanvasAndPost(canvas);
// Left Center Tone
if ((mid > 570 && mid<580)) {
//beepl.start();
}
// Right Center Tone
else if (mid >600 && mid<610) {
//beepr.start();
}
}
}
}
I've tried in the onPause method: stop(); interupt(); finish();
Also tried: onDestroy method
Thanks in advance for your help.

Related

Errors occuring when using accelerometer in android

I am getting some errors when using the accelerometer sensor.
These errors don't affect the functioning of the class, but when I press the back button , my application is "force closed."
09-22 21:33:17.119: E/SensorManager(1397): registerListener :: handle = 0
name= BMA220 delay= 20000 Listener= com.ic.edata.GameActivity#4053b4e8
09-22 21:33:17.119: E/SensorManager(1397): =======>>>Sensor Thread RUNNING <<<========
09-22 21:33:17.129: E/SensorManager(1397): reg :: handle = 0
09-22 21:33:17.139: E/SensorManager(1397): registerListener :: handle = 0
name= BMA220 delay= 20000 Listener= com.ic.edata.GameActivity#4053b4e8
09-22 21:33:17.149: E/SensorManager(1397): reg :: handle = 0
09-22 21:33:36.029: E/SensorManager(1397): unregisterListener:: handle = 0
Listener= com.ic.edata.GameActivity#4053b4e8 name = BMA220
09-22 21:33:36.029: E/SensorManager(1397): unregisterListener:: handle = 0
Listener= com.ic.edata.GameActivity#4053b4e8 name = BMA220
09-22 21:33:36.179: E/SensorManager(1397): unregisterListener:: handle = 0
Listener= com.ic.edata.GameActivity#4053b4e8 name = BMA220
`
My code for this activity is
package com.ic.edata;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameActivity extends Activity implements SensorEventListener {
float x,y,sensorX,sensorY,a,b;
Bitmap ball,end1;
SensorManager sm;
MySurfaceView ourSurfaceView;
Thread ourThread = null;
int count=0;
Sensor s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0)
{
s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
}
ball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball2);
end1 = BitmapFactory.decodeResource(getResources(), R.drawable.end1);
x=y=sensorX=sensorY=0;
a=150;
b=170;
ourSurfaceView = new MySurfaceView(this);
ourSurfaceView.resume();
setContentView(ourSurfaceView);
}
#Override
public void onDestroy() //main thread stopped
{
sm.unregisterListener(this,s);
super.onDestroy();
}
#Override
protected void onPause() {
sm.unregisterListener(this, s);
super.onPause();
}
#Override
protected void onResume() {
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
super.onResume();
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sensorX=-event.values[0];
sensorY=event.values[1];
x=x+sensorX;
y=y+sensorY;
}
public class MySurfaceView extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
boolean isRunning = false;
public MySurfaceView(Context context) {
super(context);
ourHolder = getHolder();
}
public void pause()
{
isRunning=false;
while(true)
{
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread=null;
}
public void resume()
{
isRunning=true;
ourThread = new Thread(this);
ourThread.start();
}
#Override
public void run() {
// TODO Auto-generated method stub
while(isRunning)
{
if(!ourHolder.getSurface().isValid())
continue;
Random r = new Random();
Canvas canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.rgb(230, 223, 223));
Paint p1 = new Paint(Color.BLACK);
p1.setTextSize(25);
float endx=a;
float endy=b;
if(x>canvas.getWidth())
x=0-ball.getWidth();
if(y>canvas.getHeight())
y=0-ball.getHeight();
if(x<0-ball.getWidth())
x=canvas.getWidth();
if(y<0-ball.getHeight())
y=canvas.getHeight();
canvas.drawBitmap(end1, endx, endy, null);
if((x>endx-16)&&(x<endx+24)&&(y>endy)&&(y<endy+24))
{
a=r.nextInt(canvas.getWidth()-40);
b=r.nextInt(canvas.getHeight()-40);
x=r.nextInt(canvas.getWidth());
y=r.nextInt(canvas.getHeight());
count++;
}
canvas.drawBitmap(ball, x, y, null);
canvas.drawText("Score : "+count,
canvas.getWidth()/2-45, 20, p1);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
#Override
public void onBackPressed() {
onDestroy();
super.onBackPressed();
}
}
put unregistering in a try catch block, its unregistering in onPause and then getting exception in ondestroy because theres nothing to unregister
First stop calling ondestroy() in onbackpress. ondestroy calls when it has to be call ucant forcfully call it
second for ur error... remove the code "sm.unregisterListener(this,s);"from destroy

accelerometer in android

The following example is taken from internet. In all the example the code is explained using a seprate thread for sensor manager. Could you please let me know why all the accelerometer is explained with a seperate thread for SensorManager
package com.example.myapp1;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import android.widget.Toast;
public class Accelerate extends Activity implements SensorEventListener {
float x, y, sensorX, sensorY;
Bitmap ball;
SensorManager sm;
Sensor s;
MyBringBackSurface ourSurfaceView;
public class MyBringBackSurface extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = false;
public MyBringBackSurface(Context context) {
super(context);
Log.i("Notice","In constructor of mybringbacksurface");
ourHolder = getHolder();
}
public void pause() {
isRunning = false;
Log.i("Notice","In pause of mybringbacksurface");
while (true) {
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void resume() {
Log.i("Notice","In resume of mybringbacksurface");
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
public void run() {
// TODO Auto-generated method stub
Log.i("Notice","In run of mybringbacksurface");
while (isRunning) {
if (!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
canvas.drawRGB(02, 02, 150);
float centerX = canvas.getWidth() / 2;
float centerY = canvas.getHeight() / 2;
canvas.drawBitmap(ball, centerX - sensorX * 18, centerY
+ sensorY * 18, null);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("Notice","In oncreate of of Accelerator");
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}
ball = BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
x = y = sensorX = sensorY = 0;
ourSurfaceView = new MyBringBackSurface(this);
ourSurfaceView.resume();
setContentView(ourSurfaceView);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
Log.i("Notice","In pause of Accelerator");
sm.unregisterListener(this, s);
super.onPause();
ourSurfaceView.pause();
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent e) {
// TODO Auto-generated method stub
Log.i("Notice","In sensorchanged of of Accelerator");
try {
Thread.sleep(32);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sensorX = e.values[0];
sensorY = e.values[1];
}
}
Because the continuous while loop in the run() method would block the main thread and cause your app to be shut down with an "Application Not Responding" or ANR.
Anytime you have a long running loop like that, it must happen in a separate thread.

Scroll Background Like Lunar Lander or Ninja Jump Game - Android

I'm developing a 2d Game using Canvas/Surfaceview and have a problem with scrolling my background image.
Check out the game - http://youtu.be/4Gi5rRqzZ3M
In the NinJump game, the character Ninja is just jumping in X coordinates and Background image is scrolling at a very high speed, making Ninja look like it is actually running.
I have created the basic setup, created the Ninja, added jump functionality, added background. Now I want to repeat the same background over and over again. How can I accomplish that?
Below are my source files - Main Activity Class
package com.abc.apps;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class LadderActivity extends Activity {
private static final String TAG = LadderActivity.class.getSimpleName();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requesting to turn the title OFF
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set our MainGamePanel as the View
setContentView(new MainGameBoard(this));
Log.d(TAG, "View added");
}
#Override
protected void onDestroy() {
Log.d(TAG, "Destroying...");
super.onDestroy();
}
#Override
protected void onStop() {
Log.d(TAG, "Stopping...");
super.onStop();
}
}
Game Board extends SurfaceView
package com.abc.apps;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainGameBoard extends SurfaceView implements SurfaceHolder.Callback{
private MainGameLoop thread;
private Monkey monkey;
private static final String TAG = MainGameLoop.class.getSimpleName();
int currentX, currentY;
public MainGameBoard(Context context) {
super(context);
// TODO Auto-generated constructor stub
// adding the callback (this) to the surface holder to intercept events
//This line sets the current class (MainGamePanel) as the handler for the events happening on the actual surface
getHolder().addCallback(this);
// create monkey and load bitmap INITIALIZE AT LEFT
monkey = new Monkey(BitmapFactory.decodeResource(getResources(), R.drawable.actor),60, 340);
// create the game loop thread
thread = new MainGameLoop(getHolder(), this);
// make the GamePanel focusable so it can handle events.
setFocusable(true);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
thread.setRunning(true);
thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
// tell the thread to shut down and wait for it to finish
// this is a clean shutdown
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
}
catch (InterruptedException e) {
// try again shutting down the thread
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//For jumping Left
if (event.getX() < (getWidth()/2 - 32)) {
// Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
//Log.d(TAG, "Jump Left");
// Sleep so that the main thread doesn't get flooded with UI events.
try {
Thread.sleep(32);
monkey.setX((getWidth()/2 - 60));
monkey.setY(monkey.getY()-70);
} catch (InterruptedException e) {
// No big deal if this sleep is interrupted.
}
}
//For Jumping Right
if (event.getX() > (getWidth()/2 + 32)) {
//Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
//Log.d(TAG, "Jump Right");
// Sleep so that the main thread doesn't get flooded with UI events.
try {
Thread.sleep(32);
monkey.setX((getWidth()/2 + 60));
monkey.setY(monkey.getY()-70);
} catch (InterruptedException e) {
// No big deal if this sleep is interrupted.
}
}
/* //Middle Portion
if (event.getX() > (getWidth()/2 - 32) && event.getX() < (getWidth()/2 +32)) {
//thread.setRunning(false);
//((Activity)getContext()).finish();
}*/
}
return true;
}
public void render(Canvas canvas) {
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_sc), 0, 0,null);
monkey.draw(canvas);
}
/* #Override
protected void onDraw(Canvas canvas){
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_scene), 0, 0,null);
monkey.draw(canvas);
}*/
}
Main Game Loop
package com.abc.apps;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
public class MainGameLoop extends Thread {
private SurfaceHolder surfaceHolder;
private MainGameBoard gameBoard;
private Monkey monkey;
private static final String TAG = MainGameLoop.class.getSimpleName();
// flag to hold game state
private boolean running = true;
public void setRunning(boolean running) {
this.running = running;
}
#Override
public void run() {
Canvas canvas;
Log.d(TAG, "Starting game loop");
while (running) {
canvas = null;
// try locking the canvas for exclusive pixel editing on the surface
try {
canvas = surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
// update game state
// render state to the screen
// draws the canvas on the panel
gameBoard.render(canvas);
}
}
finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}
public MainGameLoop(SurfaceHolder surfaceHolder, MainGameBoard gameBoard) {
super();
this.surfaceHolder = surfaceHolder;
this.gameBoard = gameBoard;
}
}//MainThread
Monkey Class
package com.abc.apps;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
public class Monkey {
private Bitmap bitmap; // the actual bitmap
private int x; // the X coordinate
private int y; // the Y coordinate
private boolean touched; // if monkey is touched
public Monkey(Bitmap bitmap, int x, int y) {
this.bitmap = bitmap;
this.x = x;
this.y = y;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isTouched() {
return touched;
}
public void setTouched(boolean touched) {
this.touched = touched;
}
public void draw(Canvas canvas) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y, paint);
}
}
It looks like you are drawing your background in your MainGameBoard class in the render method.
public void render(Canvas canvas) {
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_sc), 0, 0,null);
monkey.draw(canvas);
}
You should just need 2 drawBitmap calls instead of 1 there.
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_sc), 0, y_offset1,null);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.monkey_sc), 0, y_offset2,null);
I'm making an assumption that each background image has the same height or greater than the screen height; if it is less than the screen height you would need more than 2 instances.
Then you start 1 image at y_offset1 = 0 and the other at y_offset2 = -image_height.
Each draw you would increase y_offset1 and y_offset2 by the same amount. You would then need to do a check for both offsets to see if either has an amount greater than the screen height. If it does then the y_offset that is now "below screen" should be reset to the other y_offset minus the image_height. This will create a scroll image that loops indefinitely.
When using this type of technique it is important to think about your image edges; the image should be designed such that they tile seamlessly, otherwise at the looping point there is a noticeable visual artifact along the edge.

Canvas layout with SurfaceView

I was trying to make the image move. The image does not move and sometimes it flickers.
I wanted to make it Runnable. It uses a Surface Holder and s is the bitmap. The canvas sometimes remain still at i=0 or it flickers. How to make it runnable
package com.smiley;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SmileyView extends SurfaceView implements SurfaceHolder.Callback {
public SmileyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
holder = getHolder();
s= BitmapFactory.decodeResource(context.getResources(),R.drawable.smiley);
holder.addCallback(this);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent msg) {
super.onKeyUp(keyCode, msg);
rv.update();
return true;
}
private int i=0;
private Bitmap s;private SurfaceHolder holder;private RenderView rv ;
boolean grun=true;long t=0;
public void resume()
{
rv=new RenderView();
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
grun=true;
if(rv!=null)
rv.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
try {
grun =false;
rv.join();
rv=null;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class RenderView extends Thread {
RenderView(){
super();
}
#Override
public void run()
{
SurfaceHolder h=holder;
while(grun)
{
Canvas ca=null;
try{ca = h.lockCanvas(null);
synchronized (h) {
update(); dodraw(ca);
}
}
finally
{
if(ca!=null)
h.unlockCanvasAndPost(ca);
}
}
}
public void dodraw(Canvas canvas)
{ canvas.drawBitmap(s, i, i , null);
canvas.restore();
}
public void update()
{
if(i==0)
i=100;
}
}
}
I made a tutorial for what it seems like you want to do here. You can check it out here. If I had to guess then it might be an issue with not clearing out the canvas. try changing it to (assuming your background is Bitmap backBG)
public void dodraw(Canvas canvas)
{
canvas.drawBitmap(backBG, 0, 0, null);
canvas.drawBitmap(s, i, i , null);
canvas.restore();
}

Accelerometer code

I am working with accelerometer for android, I do not understand why the three axis for them which I have assigned to mSensorX, mSensorY and mSensorZ are labelled as unused when I have used them in the onSensorChange, could someone please help me understand this.
package com.example.imageaccel;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class ImageAccelActivity extends Activity implements SensorEventListener {
/** Called when the activity is first created. */
TextView x, y, z;
private float mSensorX;
private float mSensorY;
private float mSensorZ;
private Bitmap car;
private SensorManager sm = null;
// Bitmap car1;
// float x1, y1, z1, sensorX, sensorY, sensorZ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
x = (TextView)findViewById(R.id.x_axis);
y = (TextView)findViewById(R.id.y_axis);
z = (TextView)findViewById(R.id.z_axis);
SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() !=0){
Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sm.unregisterListener(this);
super.onPause();
}
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent ev) {
// TODO Auto-generated method stub
if(ev.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
mSensorX = ev.values[0];
mSensorY = ev.values[1];
mSensorZ = ev.values[2];
}
}
protected void onDraw(Canvas canvas) {
/*
* draw the background
*/
canvas.drawBitmap(car, 0, 0, null);
}
}
You're setting them to a value but that's only half the story. The complaint is most likely that you're never using them after that. It's similar to the C code:
int main (void) {
int a = 1;
return 0;
}
While that compiles and runs fine, you do get a warning (using gcc -Wall) that:
warning: unused variable 'a'
Quick way to check this theory, put a:
System.out.println (mSensorX + "," + mSensorY + "," + mSensorZ);
(or some other sort of use) following the setting and see if the warning disappears.
I think there is a warning because of the if statement there..In some cases they won't be used and hence the warning..That's my opinion:)

Categories

Resources