我有一个名为createGrid
的函数,该函数可在Gridlayout
(下面的代码)中创建一个按钮网格,设置其侦听器并为每个按钮添加一个名为no_padding_button的自定义布局。
创建网格后,根据用户单击了什么按钮,我还有另一个名为saveDesign
的函数,该函数可循环浏览gridlayout
并根据其标签返回被单击按钮的数量。我从菜单项调用saveDesign
。
问题是,只要我调用saveDesign
方法,应用程序就会崩溃。
网格工作正常,您单击按钮,它们会更改颜色并且其标签也会更改,但是某些尝试读取gridlayout
的原因会使应用崩溃。
你们可以帮助我了解代码中的错误是什么吗?
谢谢
//method to create a new grid, the number of rows and columns come from the dialogue inside the activity and then passed to this function
void createGrid(final Context context, GridLayout gridLayout) {
gridLayout.setColumnCount(totalColumns); //set the number of rows of the gridlayout
gridLayout.setRowCount(totalRows); //set the number of columns of the grid layout
//add the buttons and implement their listeners
for (int i = 0; i < totalRows; i++) {
for (int j = 0; j < totalColumns; j++) {
Button button = new Button(context);
//no padding
button.setBackground(context.getResources().getDrawable(R.drawable.no_padding_button));
//set the name of the button according to its position
button.setText(Integer.toString(i) + "," + Integer.toString(j)+",");
//hide the name, the user does not need to see this information at this moment
button.setTextColor(View.INVISIBLE);
//setting up the layout parameters for each button
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.setMargins(0, 0, 0, 0);
button.setLayoutParams(param);
//button listener
button.setOnClickListener(new View.OnClickListener() {
boolean already_clicked = false;
@Override
public void onClick(View v) {
//on click, hide the button
Button button = (Button) v;
if(!already_clicked){
//change the color of the selected buttons as an indication
button.setBackgroundColor(context.getResources().getColor(R.color.selected_button));
button.setTag(1);
already_clicked =true;
}
else{
button.setBackgroundColor(context.getResources().getColor(R.color.unselected_button));
button.setTag(0);
already_clicked =false;
}
}
});
gridLayout.addView(button);
}
}
Toast.makeText(context, "Grid Created", Toast.LENGTH_SHORT).show();
}
saveDesing方法:
public void saveDesign(){
int temp=0;
for (int i=0;i<gridLayout.getChildCount(); i++){
Button childButton = (Button)gridLayout.getChildAt(i);
if(childButton.getTag().toString() == "1"){
temp++;
}
}
Toast.makeText(this, Integer.toString(temp), Toast.LENGTH_SHORT).show();
}
持有gridlayout的活动的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<HorizontalScrollView android:id="@+id/HorizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</GridLayout>
</HorizontalScrollView>
</ScrollView>
UPDATE 1错误日志,我也尝试过string.equals(“ 1”),但是我得到了同样的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test.gridtest, PID: 3247
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
at com.example.abtin.gridtest.MainActivity.saveDesign(MainActivity.java:85)
at com.example.abtin.gridtest.MainActivity.onOptionsItemSelected(MainActivity.java:109)
at android.app.Activity.onMenuItemSelected(Activity.java:3204)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:406)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:667)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:947)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:616)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:153)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
最佳答案
如果您不单击按钮,则它没有标记。因此,childButton.getTag()返回null,并且toString使您的应用程序崩溃。
因此,请检查saveDesign()
if(childButton.getTag() != null) {
if(childButton.getTag().toString() == "1"){
temp++;
}
}
或在创建按钮时设置按钮标签。