MacaronLover MacaronLover - 3 years ago 110
Android Question

GridView gravity won't change

I am trying to change the gravity of a GridView whenever I click a button but the app crashes when deployed. I'm not sure why this occurs when the GridView has been declared in the XML layout.


Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.GridView.getLayoutParams()' on a null object reference


sw600dp/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_moveup"
android:text="move up"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="top"
android:onClick="moveup_click"
/>

<GridView
android:id="@+id/abslistview_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:numColumns="auto_fit"
/>

<Button
android:id="@+id/btn_movedown"
android:text="move down"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:onClick="movedown_click"
/>
</LinearLayout>


MainActivity.java
public class MainActivity extends Activity {

Activity mActivity;
private Button btnUp;
private Button btnDown;
private GridView gridMain;

private AbsListView absListView;
private ArrayAdapter<MainItem> adapter;
private ArrayList<MainItem> mainArrayList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

absListView = findViewById(R.id.abslistview_main);
settingData();

adapter = new AbsListViewAdapter(this, R.layout.item_abslistview, mainArrayList);
absListView.setAdapter(adapter);
}

private void settingData() {
mainArrayList = new ArrayList<>();

mainArrayList.add(new MainItem(R.drawable.ic_circle, getResources().getString(R.string.item_1)));
mainArrayList.add(new MainItem(R.drawable.ic_star, getResources().getString(R.string.item_2)));
mainArrayList.add(new MainItem(R.drawable.ic_square, getResources().getString(R.string.item_3)));
mainArrayList.add(new MainItem(R.drawable.ic_triangle, getResources().getString(R.string.item_4)));
mainArrayList.add(new MainItem(R.drawable.ic_rectangle, getResources().getString(R.string.item_5)));
mainArrayList.add(new MainItem(R.drawable.ic_crescent, getResources().getString(R.string.item_6)));
}

public void moveup_click(View view) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) gridMain.getLayoutParams();
gridMain = findViewById(R.id.abslistview_main);
if (params.gravity == Gravity.BOTTOM) {
gridMain.setGravity(Gravity.CENTER_VERTICAL);
btnDown.setVisibility(View.VISIBLE);
} else if (params.gravity == Gravity.CENTER_VERTICAL) {
gridMain.setGravity(Gravity.TOP);
btnUp.setVisibility(View.GONE);
}
}
}

Answer Source

You have buttons that fire the moveup_click() function. In that function, you first want LayoutParams of GridView and after that you initialise the gridview itself. This is not the right sequence.

To solve it, you can move the gridMain = findViewById(R.id.abslistview_main);the oncreate function or BEFORE LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) gridMain.getLayoutParams();. -

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download