Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd object to an array object is not work in android
    primarykey
    data
    text
    <p>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.</p> <p>*Note: Piece is extends ImageView</p> <pre><code>Piece[] tigers = new Piece[4]; for(int i = 0; i &lt; tigers.length; i++) { tigers[i] = new Piece(context); tigers[i].setType(R.drawable.tiger); } </code></pre> <p>Here is my Piece class:</p> <pre><code>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; } } </code></pre> <p>Here is my MainActivity.java</p> <pre><code>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 &lt; tigers.length; i++) { tigers[i] = new Piece(context); tigers[i].setType(R.drawable.tiger); } } } </code></pre> <p>Here is my ImageAdapter.java</p> <pre><code>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; } } </code></pre> <p>The problem is can not add new objects to that array. What is wrong with this and how should I do?</p> <p>Error:</p> <pre><code>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.&lt;init&gt;(View.java:2693) 03-12 10:36:17.376: E/AndroidRuntime(1331): at android.widget.ImageView.&lt;init&gt;(ImageView.java:104) 03-12 10:36:17.376: E/AndroidRuntime(1331): at com.camitss.klasikou.Piece.&lt;init&gt;(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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload