我正在设置一个要添加卡片布局的应用。单击添加按钮后,将添加带有用户名和详细信息的卡。单击卡片后,将打开一个新活动。一切正常,除非我关闭活动,所有添加的卡片都消失了,并且应用程序完全空白。 同样在添加卡片后更改应用方向时,应用崩溃会导致泄漏的窗口异常。请帮助我解决问题
我已经重写了onSaveInstanceState()和onRestoreInstanceState()方法,这些方法在代码末尾可见。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
mcontext=this;
buttonScan = findViewById(R.id.fab);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
dialog1 = new Dialog(Activity.this);
context = getApplicationContext();
/* SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
boolean shouldDisplayCard = sharedPref.getBoolean("should_display_card", false);
if(shouldDisplayCard)
cardview();*/
buttonScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addhome();
/* scanWifi();
dialog1.setContentView(R.layout.activity_scan);
dialog1.setTitle("Scanning");
listView = dialog1.findViewById(R.id.wifiList);
listView.setAdapter(adapter);
dialog1.show();*/
}
});
homeLayout = findViewById(R.id.homelayout);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv = new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("Currently no device is added, Tap '+' to add a new device.");
tv.setTextSize(21);
tv.setGravity(Gravity.CENTER);
tv.setPadding(70, 470, 70, 50);
homeLayout.addView(tv);
}
public void addhome() {
dialog2 = new Dialog(Activity.this);
dialog2.setContentView(R.layout.add_home);
v = View.inflate(this, R.layout.add_home, null);
dialog2.setContentView(v);
dialog2.show();
editText1 = v.findViewById(R.id.editText);
passtext1 = v.findViewById(R.id.passText1);
passtext2 = v.findViewById(R.id.passText2);
homebut = v.findViewById(R.id.homeBut);
homebut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText1.getText().toString().trim().length() <= 0)
Toast.makeText(getApplicationContext(), "Please input Home name", Toast.LENGTH_SHORT).show();
else if (passtext1.getText().toString().trim().length() <= 0 || passtext2.getText().toString().trim().length() <= 0)
Toast.makeText(getApplicationContext(), "Please enter Password", Toast.LENGTH_SHORT).show();
else if (!passtext1.getText().toString().trim().equals(passtext2.getText().toString().trim()))
Toast.makeText(getApplicationContext(), "Passwords do not match", Toast.LENGTH_SHORT).show();
else {
pass1 = passtext1.getText().toString().trim();
pass2 = passtext2.getText().toString().trim();
cardview();
/*SharedPreferences sharedPref = mcontext.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("should_display_card", true);
editor.apply();*/
dialog2.hide();
homeLayout.removeView(tv);
}
}
});
}
public void cardview() {
cardview = new CardView(context);
layoutparams1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutparams1.setMargins(18, 30, 18, 0);
cardview.setLayoutParams(layoutparams1);
cardview.setCardBackgroundColor(Color.WHITE);
cardview.setMinimumHeight(400);
cardview.setRadius(30);
textview1 = new TextView(context);
//textview1.setLayoutParams(layoutparams1);
textview1.setText(editText1.getText().toString().trim());
textview1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 28);
textview1.setTypeface(null, Typeface.BOLD);
textview1.setTextColor(Color.GRAY);
textview1.setPadding(30, 35, 25, 65);
textview1.setGravity(Gravity.TOP);
textview1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Activity.this, MainActivity.class);
Activity.this.startActivity(intent);
}
});
textview2 = new TextView(context);
// textview2.setLayoutParams(layoutparams1);
textview2.setText("Connection: ");
textview2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
textview2.setTextColor(Color.GRAY);
textview2.setPadding(30, 165, 25, 45);
textview2.setGravity(Gravity.TOP);
textview3 = new TextView(context);
// textview3.setLayoutParams(layoutparams1);
textview3.setText("Security: ");
textview3.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
textview3.setTextColor(Color.GRAY);
textview3.setPadding(30, 255, 25, 45);
textview3.setGravity(Gravity.TOP);
textview4 = new TextView(context);
//textview4.setLayoutParams(layoutparams1);
textview4.setText("Active");
textview4.setTextColor(Color.GREEN);
textview4.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
textview4.setPadding(450, 165, 25, 45);
textview4.setGravity(Gravity.TOP);
textview5 = new TextView(context);
textview5.setLayoutParams(layoutparams1);
textview5.setText("Inactive");
textview5.setTextColor(Color.RED);
textview5.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
textview5.setPadding(450, 215, 25, 45);
textview5.setGravity(Gravity.TOP);
cardview.addView(textview1);
cardview.addView(textview2);
cardview.addView(textview3);
cardview.addView(textview4);
cardview.addView(textview5);
homeLayout.addView(cardview);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState");
final EditText textBox = v.findViewById(R.id.editText);
CharSequence userText = textBox.getText();
outState.putCharSequence("savedText", userText);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
final EditText textBox = v.findViewById(R.id.editText);
CharSequence userText = savedInstanceState.getCharSequence("savedText");
textBox.setText(userText);
}