Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have noticed one thing in you code:</p> <pre><code>gridView.setLayoutParams(new GridView.LayoutParams(width,LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL)); </code></pre> <p>Even if <code>GridView</code> is a <code>ViewGroup</code>, you can't access it's <code>LayoutParams</code>. Just think it trough, if this would be possible that means you could put a <code>GridView</code> inside another <code>GridView</code>.</p> <p>Fix this before going further because is messing with you.</p> <p>If you want your GridView to be inside a LinearLayout, for example, try this:</p> <pre><code>gridView.setLayoutParams(new LinearLayout.LayoutParams(width,LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL)); </code></pre> <p>Here is the correct implementation of <a href="https://stackoverflow.com/a/13689856/1392423">this answer</a>:</p> <pre><code>public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*You have to implement what things from the gridView sould be "saved"*/ GridView gridView = new GridView(this) { @Override public Parcelable onSaveInstanceState() { // Create the Parceable object with the things you want to save Parceable stateOfGrid = ....etc return stateOfGrid; } @Override public void onRestoreInstanceState(Parcelable state) { // Restore your grid's parameters that you previously implemented in onSaveInstanceState super.onRestoreInstanceState(state); ... } }; } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onRestoreInstanceState(savedInstanceState); if (savedInstanceState != null) { Parcelable state = savedInstanceState.getParcelable("state"); if (state != null) { gridView.onRestoreInstanceState(state); Log.d(this.getClass().getName(), "state restored!"); } } @Override protected void onSaveInstanceState(Bundle outState) { // Put your grid's Parceable into the bundle super.onSaveInstanceState(outState); Parcelable state = gridView.onSaveInstanceState(); outState.putParcelable("state", state); } } </code></pre>
 

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