Java 如何保存动态创建的按钮实例?
每次单击floatactionbutton时,我的应用程序都会动态创建(最多)8个按钮,但当屏幕旋转时,所有按钮都会消失。我知道这背后的原因,那是因为我没有在生成的视图上设置onSaveInstanceState。我尝试用outState.putInt设置onSaveInstanceState(因为我觉得这是合适的,因为按钮ID的类型是int),但它不起作用 这是我的JavaJava 如何保存动态创建的按钮实例?,java,android,onsaveinstancestate,Java,Android,Onsaveinstancestate,每次单击floatactionbutton时,我的应用程序都会动态创建(最多)8个按钮,但当屏幕旋转时,所有按钮都会消失。我知道这背后的原因,那是因为我没有在生成的视图上设置onSaveInstanceState。我尝试用outState.putInt设置onSaveInstanceState(因为我觉得这是合适的,因为按钮ID的类型是int),但它不起作用 这是我的Java public class MainActivity extends AppCompatActivity { i
public class MainActivity extends AppCompatActivity {
int counter = 0;
FloatingActionButton addingSemester;
Button semesterButton;
LinearLayout semesterLayout;
GridLayout semesterGridLayout;
LinearLayout.LayoutParams portraitLayoutParams = new LinearLayout.LayoutParams(
AppBarLayout.LayoutParams.MATCH_PARENT,
AppBarLayout.LayoutParams.WRAP_CONTENT);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addingSemester = (FloatingActionButton) findViewById(R.id.addActionButton);
semesterLayout = (LinearLayout) findViewById(R.id.main_layout);
semesterGridLayout = (GridLayout)findViewById(R.id.semester_grid_layout);
semesterButton = new Button(MainActivity.this);
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
outState.putInt("semesterButton", semesterButton.getId());
super.onSaveInstanceState(outState, outPersistentState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
savedInstanceState.getInt("semesterButton", semesterButton.getId());
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.delete) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete everything?")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterGridLayout.removeAllViews();
} else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterLayout.removeAllViews();
}
counter = 0;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.show();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onFloatActionButtonClick(View view) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
double width = (size.x)/3;
semesterButton = new Button(MainActivity.this);
if (counter < 8) {
semesterButton.setId(counter + 1);
semesterButton.setText("Semester " + (counter + 1));
semesterButton.setBackgroundColor(getColor(R.color.colorPrimary));
semesterButton.setTextColor(Color.WHITE);
portraitLayoutParams.setMargins(24, 24, 24, 24);
if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.setMargins(24, 24, 24, 24);
params.width = (int) width;
params.height = GridLayout.LayoutParams.WRAP_CONTENT;
semesterButton.setLayoutParams(params);
semesterGridLayout.addView(semesterButton);
} else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterLayout.addView(semesterButton);
semesterButton.setLayoutParams(portraitLayoutParams);
}
// these lines moved outside the if statement blocks
counter++;
setOnLongClickListenerForSemesterButton();
} else if (counter == 8) {
Toast.makeText(MainActivity.this, "You cannot add more than 8 semesters", Toast.LENGTH_SHORT).show();
}
}
private void setOnLongClickListenerForSemesterButton() {
semesterButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final Button b = (Button) v;
b.setTag(b.getText().toString());
b.setBackgroundColor(Color.RED);
b.setText("Delete");
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Delete entry");
builder.setMessage("Are you sure you want to delete this entry?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterGridLayout.removeView(b);
for (int i = 0; i < semesterGridLayout.getChildCount(); i++) {
((Button) semesterGridLayout.getChildAt(i)).setText("Semester " + (i + 1));
}
} else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
semesterLayout.removeView(b);
for (int i = 0; i < semesterLayout.getChildCount(); i++) {
((Button) semesterLayout.getChildAt(i)).setText("Semester " + (i + 1));
}
}
counter--;
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
b.cancelLongPress();
b.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
b.setText(b.getTag().toString());
dialog.cancel();
}
});
builder.show();
return true;
}
});
}
}
对于我的onCreate,我就是这么做的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addingSemester = (FloatingActionButton) findViewById(R.id.addActionButton);
semesterLayout = (LinearLayout) findViewById(R.id.main_layout);
semesterGridLayout = (GridLayout)findViewById(R.id.semester_grid_layout);
semesterButton = new Button(MainActivity.this);
if(savedInstanceState!=null) {
int numberofbuttons = savedInstanceState.getInt("NUMBEROFBUTTONS");
if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
for(int i = 0; i < numberofbuttons; i++){
semesterLayout.addView(semesterButton);
}
} else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
for(int i = 0; i < numberofbuttons; i++){
semesterGridLayout.addView(semesterButton);
}
}
}
}
基本上,问题是我无法为GridLayout对象获取getChildCount()。在配置更改之间无法保存与UI相关的变量(这是在旋转屏幕时发生的情况) 但是,您有多个选项可以执行您想要的操作: 您可以使用片段。如果在按钮片段上使用“setRetainInstance”,则在配置更改之间应该保持不变,但这种方法不允许您处理backbackback(用于使用back按钮还原更改)。赞成:最简单的交易方式。缺点:嗯,setRetainInstance用于保存没有UI元素的片段 您可以在编写时在onSaveInstanceState中保存按钮的数量,但随后需要在“onCreate”上检查捆绑包,如果不为空,则获取数量,并相应地添加按钮。赞成者:简单。缺点:如果您从按钮切换到任何东西,或者如果按钮依赖于某些外部数据,那么这样做可能会改变按钮的外观 您可以自己管理屏幕旋转。我不知道如何做到这一点,但我不建议这样做,因为如果用户交换语言、区域或任何导致配置更改的内容,问题仍然存在。然而,即使这是最糟糕的情况,也需要提及这个选项(IMO) 编辑:根据要求,更多详细信息请参见第二页-> 首先,覆盖: 然后,在onCreate期间:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!=null) {
int number_of_button = savedInstanceState.getInt("NUMBEROFBUTTONS");
display_X_buttons(number_of_button); /*sum up : display number_of_button buttons as you wish display them.*/
}
}
第二个选项怎么办?请看:这根本不是同一个问题。当没有子项时,当它请求childcount时为null。我应该在代码中实现if(x!=null)吗?即使在那之后,这将如何解决整个问题?即使布局中有视图,旋转也会导致应用程序崩溃。是的,您应该这样做,或者在这种情况下将0另存为按钮编号。这并不能保证它能完全解决您的问题,我只是提供一些方法来处理您的问题,您的实现邮件失败(如NPE),或插入按钮失败,或其他任何不同的问题,但我在这里的工作是帮助您回答第一个问题,而不是为您编程:)我不是要求您为我编程应用程序。必须有一种方法来存储动态生成视图的实例。play store上有大量的应用程序可以做到这一点。
FATAL EXCEPTION: main
Process: myapp.onur.journeygpacalculator, PID: 10463
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.GridLayout.getChildCount()' on a null object reference
at myapp.onur.journeygpacalculator.MainActivity.onSaveInstanceState(MainActivity.java:68)
public void onSaveInstanceState(Bundle outsave) {
super.onSaveInstanceState(outsave);
outsave.putInt("NUMBEROFBUTTONS", number_of_buttons);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!=null) {
int number_of_button = savedInstanceState.getInt("NUMBEROFBUTTONS");
display_X_buttons(number_of_button); /*sum up : display number_of_button buttons as you wish display them.*/
}
}