Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

Android game crashes when adding new view to view group programatically

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...