1

I am a new experience of Android. Could anyone help me out with my problem? I want to create a game Tiger fighting with Cow (it is board game). I have a class Piece can be setType as Tiger or Cow and I want to create an array of this class like below.

*Note: Piece is extends 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);
}

Here is my Piece class:

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;
    }
}

Here is my 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);
    }
}
}

Here is my 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;
}

}

The problem is can not add new objects to that array. What is wrong with this and how should I do?

Error:

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
6
  • plz also post stackTrace with question Commented Mar 12, 2013 at 2:23
  • Where is line 10 of Piece.java? Commented Mar 12, 2013 at 2:44
  • Double check that each loop is using the correct array length. Commented Mar 12, 2013 at 2:46
  • So now it is the new upload. I'm sorry, very messing up for my first time. Commented Mar 12, 2013 at 3:20
  • @Fiemhong show the MainActivity code as well. Did you remember to call super.onCreate()? Commented Mar 12, 2013 at 3:24

2 Answers 2

1

You forgot to give context a non-null reference, and since the Context finally gets put to use in the superclass of your Piece class, the NPE is thrown all the way over there. Since you're working in an Activity change your code so it is uses the this keyword instead:

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);
    }

Or if you're really keen on using context, give it a value after calling super.onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this; //add this line
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much A--C and all of you try to help for my problem. I got it now. :) :)
1

Instead of Piece.length in your loops it should be tigers.length() and cows.length()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.