1

我是Android的新体验。谁能帮我解决我的问题?我想创建一个老虎与牛打架的游戏(它是棋盘游戏)。我有一个类 Piece 可以设置为 Tiger 或 Cow,我想创建一个此类的数组,如下所示。

*注意:Piece 是 ImageView 的扩展

Piece[] tigers = new Piece[4];
for(int i = 0; i < tigers.length; i++) {
    tigers[i] = new Piece(context);
    tigers[i].setType(R.drawable.tiger);
}

这是我的 Piece 课程:

package com.camitss.klasikou;

import android.content.Context;
import android.util.Log;
import android.widget.ImageView;

public class Piece extends ImageView{
    private int type;

    public Piece(Context context){
        super(context);
        super.setScaleType(ImageView.ScaleType.CENTER_CROP);
        super.setPadding(4, 4, 4, 4);
    }

    public int getType() {
        return this.type;
    }

    public void setType(int type) {
        this.type = type;
    }
}

这是我的 MainActivity.java

package com.camitss.klasikou;
import com.camitss.klasikou.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnClickListener{

private LinearLayout llNewGame;
private LinearLayout llInstruction;
private LinearLayout llOption;
private LinearLayout llAbout;
private LinearLayout llCow;
private LinearLayout llTiger;


private RelativeLayout p_new_game;
private RelativeLayout p_instruction;
private GridView p_fighting;
public static ImageAdapter imgAdt;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    llNewGame = (LinearLayout) findViewById(R.id.l_new_game);
    llNewGame.setOnClickListener(this);

    llInstruction = (LinearLayout) findViewById(R.id.l_instruction);
    llInstruction.setOnClickListener(this);

    llOption = (LinearLayout) findViewById(R.id.l_option);
    llOption.setOnClickListener(this);

    llAbout = (LinearLayout) findViewById(R.id.l_about);
    llAbout.setOnClickListener(this);

    llCow = (LinearLayout) findViewById(R.id.l_cow);
    llCow.setOnClickListener(this);

    llTiger = (LinearLayout) findViewById(R.id.l_tiger);
    llTiger.setOnClickListener(this);


    p_new_game = (RelativeLayout) findViewById(R.id.page_new_game);
    p_instruction = (RelativeLayout) findViewById(R.id.page_instruction);
    p_fighting = (GridView) findViewById(R.id.page_fighting);

    imgAdt = new ImageAdapter(this);

    boardInit();

    p_fighting.setAdapter(imgAdt);
    p_fighting.setGravity(Gravity.CENTER);
}

private void llOff(LinearLayout llNewGame, LinearLayout llInstruction,
        LinearLayout llOption, LinearLayout llAbout) {
    llNewGame.setBackgroundResource(R.drawable.menu_newgame);
    llInstruction.setBackgroundResource(R.drawable.menu_instructions);
    llOption.setBackgroundResource(R.drawable.menu_options);
    llAbout.setBackgroundResource(R.drawable.menu_about);
}

private void wrapInvisible(RelativeLayout p_new_game,
        RelativeLayout p_instruction) {
    p_new_game.setVisibility(View.GONE);
    p_instruction.setVisibility(View.GONE);
}

@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 void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId() == R.id.page_fighting) {

    }
    if (v.getId() == R.id.l_cow) {
        p_new_game.setVisibility(View.GONE);
        p_fighting.setVisibility(View.VISIBLE);

    }
    if (v.getId() == R.id.l_tiger) {
        p_new_game.setVisibility(View.GONE);
        p_fighting.setVisibility(View.VISIBLE);
    }
    if (v.getId() == R.id.l_new_game) {
        llOff(llNewGame, llInstruction, llOption, llAbout);
        wrapInvisible(p_new_game, p_instruction);
        llNewGame.setBackgroundResource(R.drawable.menu_newgame_on);
        p_new_game.setVisibility(View.VISIBLE);
    }
    if (v.getId() == R.id.l_instruction) {
        llOff(llNewGame, llInstruction, llOption, llAbout);
        wrapInvisible(p_new_game, p_instruction);

        llInstruction.setBackgroundResource(R.drawable.menu_instructions_on);
        p_instruction.setVisibility(View.VISIBLE);
    }
    if (v.getId() == R.id.l_option) {
        llOff(llNewGame, llInstruction, llOption, llAbout);
        llOption.setBackgroundResource(R.drawable.menu_options_on);
    }
    if (v.getId() == R.id.l_about) {
        llOff(llNewGame, llInstruction, llOption, llAbout);
        llAbout.setBackgroundResource(R.drawable.menu_about_on);
    }
}

public void boardInit(){
    Piece[] tigers = new Piece[4];
    for(int i = 0; i < tigers.length; i++) {
        tigers[i] = new Piece(context);
        tigers[i].setType(R.drawable.tiger);
    }
}
}

这是我的 ImageAdapter.java

package com.camitss.klasikou;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;

public class ImageAdapter implements ListAdapter {
private Context mContext;
public Integer[] board = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};

public ImageAdapter(Context c) {
    mContext = c;
}

public void setObjectToBoard(int type, int position) {
    board[position] = type;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return this.board.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return this.board[position];
//      return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;

    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(57, 58));
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(board[position]);
    return imageView;
}

@Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return 1;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isEmpty() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    // TODO Auto-generated method stub

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
    // TODO Auto-generated method stub

}

@Override
public boolean areAllItemsEnabled() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isEnabled(int position) {
    // TODO Auto-generated method stub
    return false;
}

}

问题是无法向该数组添加新对象。这有什么问题,我该怎么办?

错误:

03-12 10:36:17.376: E/AndroidRuntime(1331): FATAL EXCEPTION: main
03-12 10:36:17.376: E/AndroidRuntime(1331): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.camitss.klasikou/com.camitss.klasikou.MainActivity}: java.lang.NullPointerException
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.os.Looper.loop(Looper.java:137)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.app.ActivityThread.main(ActivityThread.java:4340)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at java.lang.reflect.Method.invoke(Method.java:511)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at dalvik.system.NativeStart.main(Native Method)
03-12 10:36:17.376: E/AndroidRuntime(1331): Caused by: java.lang.NullPointerException
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.view.ViewConfiguration.get(ViewConfiguration.java:314)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.view.View.<init>(View.java:2693)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.widget.ImageView.<init>(ImageView.java:104)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at com.camitss.klasikou.Piece.<init>(Piece.java:9)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at com.camitss.klasikou.MainActivity.boardInit(MainActivity.java:134)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at com.camitss.klasikou.MainActivity.onCreate(MainActivity.java:66)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.app.Activity.performCreate(Activity.java:4465)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-12 10:36:17.376: E/AndroidRuntime(1331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
03-12 10:36:17.376: E/AndroidRuntime(1331):     ... 11 more
4

2 回答 2

1

而不是Piece.length在你的循环中,它应该是tigers.length()cows.length()

于 2013-03-12T02:23:58.297 回答
1

您忘记提供contextnull引用,并且由于Contextfinally 在您的类的超类中使用Piece,因此 NPE 一直被扔到那里。由于您正在使用 Activity 更改代码,因此它使用this关键字:

public void boardInit(){
    Piece[] tigers = new Piece[4];
    for(int i = 0; i < tigers.length; i++) {
        tigers[i] = new Piece(this);
        tigers[i].setType(R.drawable.tiger);
    }

或者,如果您真的热衷于使用context,请在调用后给它一个值super.onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this; //add this line
于 2013-03-12T03:39:26.430 回答