lundi 11 mai 2015

Eclipse Android Game - Crashes when I hit Play

I've been following some tutorials online to try to get a grasp of basic android development, and I seem to be stuck. There are no errors in my code, but a few warnings and whenever I press play from the main menu, it crashes. If anyone could help out that'd be greatly appreciated!!

This is my MainActivity.java

package com.example.shadowassassinstu3;

import com.example.shadowassassinstu3.Game;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void play(View v)
    {
        Intent i=new Intent(this,Game.class);
        startActivity(i);
    }

    public void exit(View v)
    {
        System.exit(0);
    }
}

This is my Game.java

package com.example.shadowassassinstu3;

import com.example.shadowassassinstu3.gameloop;
import com.example.shadowassassinstu3.Game.GameView;
import com.example.shadowassassinstu3.Game.TeleListener;
import com.example.shadowassassinstu3.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class Game extends Activity {
    gameloop gameLoopThread;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //phone state
        TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        TelephonyMgr.listen(new TeleListener(),PhoneStateListener.LISTEN_CALL_STATE);
        //for no title
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new GameView(this));     
    }

    public class GameView extends SurfaceView {
        Bitmap bmp,pause;
        Bitmap background;
        Bitmap run1;
        Bitmap run2;
        Bitmap run3;
        Bitmap exit;

        private SurfaceHolder holder;
        private int x = 0,y=0,z=0,delay=0,getx,gety;
        int show=0,sx,sy;
        int cspeed=0,kspeed=0,gameover=0;
        int score=0,health=100,reset=0;
        int pausecount=0,volume,power=0,powerrun=0,shieldrun=0;

        @SuppressWarnings("deprecation")
        @SuppressLint("NewApi")
        public GameView(Context context) 
          {
              super(context);

              gameLoopThread = new gameloop(this);
              holder = getHolder();

                 holder.addCallback(new SurfaceHolder.Callback() {
                @SuppressWarnings("deprecation")
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) 
                {
                    gameLoopThread.setRunning(false);
                    gameLoopThread.getThreadGroup().interrupt();
                }

                @SuppressLint("WrongCall")
                @Override
                public void surfaceCreated(SurfaceHolder holder) 
                {
                      gameLoopThread.setRunning(true);
                      gameLoopThread.start();

                }
                @Override
                public void surfaceChanged(SurfaceHolder holder, int format,int width, int height) 
                        {

                        }
                });



                Display display = getWindowManager().getDefaultDisplay();

                sx = display.getWidth();
                sy = display.getHeight();;
                background = BitmapFactory.decodeResource(getResources(), R.drawable.back);
                run1=BitmapFactory.decodeResource(getResources(), R.drawable.run1);
                run2=BitmapFactory.decodeResource(getResources(), R.drawable.run2);
                run3=BitmapFactory.decodeResource(getResources(), R.drawable.run3);
                exit=BitmapFactory.decodeResource(getResources(), R.drawable.exit);
                pause=BitmapFactory.decodeResource(getResources(), R.drawable.pause);

                exit=Bitmap.createScaledBitmap(exit, 25,25, true);
                pause=Bitmap.createScaledBitmap(pause, 25,25, true);
                run1=Bitmap.createScaledBitmap(run1, sx/9,sy/7, true);
                run2=Bitmap.createScaledBitmap(run2, sx/9,sy/7, true);
                run3=Bitmap.createScaledBitmap(run3, sx/9,sy/7, true);
                background=Bitmap.createScaledBitmap(background, 2*sx,sy, true);

    }


     // on touch method

          @Override
            public boolean onTouchEvent(MotionEvent event) {

                if(event.getAction()==MotionEvent.ACTION_DOWN)
                {
                    show=1;

                    getx=(int) event.getX();
                    gety=(int) event.getY();

                // restart game
                if(getx>91&&gety<25)
                {
                    if(health<=0)
                    {
                        gameLoopThread.setPause(0);
                        health=100;
                        score=0;

                    }
                }
                //pause game
                if((getx>(sx-25)&&gety<25&&pausecount==0))
                {

                    gameLoopThread.setPause(1);
                    pausecount=1;
                }
                else if(getx>(sx-25)&&gety<25&&pausecount==1)
                {
                    gameLoopThread.setPause(0);
                    pausecount=0;
                }
            }

            return true;
        }


        @SuppressLint("WrongCall")
        @Override
        protected void onDraw(Canvas canvas) 
        {
            //background moving
            z=z-10;
            if(z==-sx)
            {
                z=0;
                canvas.drawBitmap(background, z, 0, null);

            }
            else
            {
                canvas.drawBitmap(background, z, 0, null);  
            }

            //running player 

         x+=5;
         if(x==20)
         {
             x=5;
         }

          if(show==0)
          {
              if(x%2==0)
              {
                  canvas.drawBitmap(run3, sx/16, 15*sy/18, null);

              }
              else 
              {
                  canvas.drawBitmap(run1, sx/16, 15*sy/18, null);

              }
          }

          canvas.drawBitmap(exit, 0, 0, null);
          canvas.drawBitmap(pause, (sx-25), 0, null);
        }
    }

    //phone state
    public class TeleListener extends PhoneStateListener 
    {
        public void onCallStateChanged(int state,String incomingNumber)
        {
            if(state==TelephonyManager.CALL_STATE_RINGING)
                {
                    System.exit(0);  
                }
        } 

    }
}

and this is my gameloop.java

package com.example.shadowassassinstu3;

import com.example.shadowassassinstu3.Game.GameView;
import android.annotation.SuppressLint;
import android.graphics.Canvas;

public class gameloop extends Thread {

    private GameView view;
    static final long FPS = 10;
    private boolean running = false;
       boolean isPaused;

    public gameloop(GameView view) {
          this.view = view;
    }

    public void setRunning(boolean run) {

          running = run;
    }

    public void setPause(int i)
    {
        synchronized (view.getHolder()) 
        {


            if(i==0)
            {
                isPaused=false;
            }
            if(i==1)
            {
                isPaused = true;
            }
         }
    }

    public void run() {
           long ticksPS = 100;
           long startTime = 0;
           long sleepTime;
              while (running) {
                  //pause and resume

                if (isPaused) 
                {
                      try 
                      {
                          this.sleep(50);
                      } 
                      catch (InterruptedException e) 
                      {
                        e.printStackTrace();
                      }
                }
                else
                {
                     Canvas c = null;
                     startTime = System.currentTimeMillis();
                     try {

                            c = view.getHolder().lockCanvas();

                            synchronized (view.getHolder()) 
                            {
                                view.onDraw(c);
                            }

                          } 
                     finally 
                     {
                         if (c != null) 
                            {
                                view.getHolder().unlockCanvasAndPost(c);
                            }
                     }
                   }
                     sleepTime = ticksPS-(System.currentTimeMillis() - startTime); 

                     try {

                            if (sleepTime > 0)
                               sleep(sleepTime);
                            else
                               sleep(10);
                        } 
                catch (Exception e) {}

              }

        }
}

A lot of the code I've gotten through watching tutorials and I really can't figure out what's wrong, if anyone could help I'd really appreciate it!

I think this is what you guys asked for:

05-11 10:23:51.966: D/AndroidRuntime(1894): Shutting down VM 05-11 10:23:51.966: D/AndroidRuntime(1894): --------- beginning of crash 05-11 10:23:51.966: E/AndroidRuntime(1894): FATAL EXCEPTION: main 05-11 10:23:51.966: E/AndroidRuntime(1894): Process: com.example.shadowassassinstu3, PID: 1894 05-11 10:23:51.966: E/AndroidRuntime(1894): java.lang.IllegalStateException: Could not execute method of the activity 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.view.View$1.onClick(View.java:4007) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.view.View.performClick(View.java:4756) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.view.View$PerformClick.run(View.java:19749) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.os.Handler.handleCallback(Handler.java:739) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.os.Handler.dispatchMessage(Handler.java:95) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.os.Looper.loop(Looper.java:135) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.ActivityThread.main(ActivityThread.java:5221) 05-11 10:23:51.966: E/AndroidRuntime(1894): at java.lang.reflect.Method.invoke(Native Method) 05-11 10:23:51.966: E/AndroidRuntime(1894): at java.lang.reflect.Method.invoke(Method.java:372) 05-11 10:23:51.966: E/AndroidRuntime(1894): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 05-11 10:23:51.966: E/AndroidRuntime(1894): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 05-11 10:23:51.966: E/AndroidRuntime(1894): Caused by: java.lang.reflect.InvocationTargetException 05-11 10:23:51.966: E/AndroidRuntime(1894): at java.lang.reflect.Method.invoke(Native Method) 05-11 10:23:51.966: E/AndroidRuntime(1894): at java.lang.reflect.Method.invoke(Method.java:372) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.view.View$1.onClick(View.java:4002) 05-11 10:23:51.966: E/AndroidRuntime(1894): ... 10 more 05-11 10:23:51.966: E/AndroidRuntime(1894): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.shadowassassinstu3/com.example.shadowassassinstu3.Game}; have you declared this activity in your AndroidManifest.xml? 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Activity.startActivityForResult(Activity.java:3736) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Activity.startActivityForResult(Activity.java:3697) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Activity.startActivity(Activity.java:4007) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Activity.startActivity(Activity.java:3975) 05-11 10:23:51.966: E/AndroidRuntime(1894): at com.example.shadowassassinstu3.MainActivity.play(MainActivity.java:25) 05-11 10:23:51.966: E/AndroidRuntime(1894): ... 13 more 05-11 10:23:55.283: I/Process(1894): Sending signal. PID: 1894 SIG: 9

Aucun commentaire:

Enregistrer un commentaire