Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have been trying to add a new view to my view group for now I only have one view but I do want to add another one for buttons and things of that nature Here is the code for my Game View Class(View Group) :

    import android.content.Context;
    import android.view.ViewGroup;
    import java.io.FileNotFoundException;

    public class GameView extends ViewGroup {

        private MainActivity activity;

        public GameView(Context context,MainActivity activity) throws FileNotFoundException
        {
            super(context);
            this.activity = activity;
            Graphics v = new Graphics(this,activity,context);
            addView(v);


        } 
        @Override
        protected void onLayout(boolean b, int i, int i2, int i3, int i4) {

        }
    }

This is the Code for my Graphics class :

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;


import java.io.IOException;

/**
 * Created by Anonymous on 4/29/14.
 */

public class Graphics extends View implements Runnable,Button.OnTouchListener {
    private Thread thread;
    private boolean running = false;
    private Player p;
    private MainActivity activity;
    private Button b;
    GameView view;
    public Graphics(GameView view,MainActivity activity,Context context){
        super(context);
        this.view = view;
        this.activity = activity;
        p = new Player(400,600,activity);

    }

    public synchronized void start()
    {
        if(running)
            return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }
    public synchronized void stop()
    {
        if(!running)
            return;

        running = false;
        System.exit(0);

    }

    @Override
    public void run()
    {
        Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        while(running)
        {
            onDraw(c);
            tick();

        }

    }

    public void tick()
    {
        p.tick(this);

    }


    @Override
    protected void onDraw(Canvas canvas)
    {
        try
        {
            p.render(canvas);

        }catch (IOException e)
        {

        }


    }

    public boolean onTouch(View view,MotionEvent e){
        int action = e.getAction();
        switch(action)
        {
            case MotionEvent.ACTION_DOWN:

                p.setY(p.getY()+1);
                break;
        }


        return false;
    }
}

Here is my main activity class :

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

import java.io.FileNotFoundException;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        try{
        setContentView(new GameView(this,this));

        }catch(FileNotFoundException e){

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

Without the logcat it's kind of risky to diagnose.

However

 private Player p;

is never initialized and used all over the place. So I'll venture you're getting NPE exceptions everywhere.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...