Android应用程序因方向更改而崩溃java.lang.IllegalArgumentException:错误的状态类--应为视图状态
我的android应用程序有另一个问题,我找不到答案,基本上我有一个通过编程创建的android视图,然后当我更改设备方向时,应用程序崩溃,原因是: java.lang.IllegalArgumentException:错误的状态类--应为视图状态 我曾尝试使用onConfigurationChange方法再次设置内容视图,但我不知道我是否在这里找到了错误的树。我已经从日志中包含了完整的类和错误,以防它有所帮助Android应用程序因方向更改而崩溃java.lang.IllegalArgumentException:错误的状态类--应为视图状态,android,orientation,screen-orientation,illegalargumentexception,Android,Orientation,Screen Orientation,Illegalargumentexception,我的android应用程序有另一个问题,我找不到答案,基本上我有一个通过编程创建的android视图,然后当我更改设备方向时,应用程序崩溃,原因是: java.lang.IllegalArgumentException:错误的状态类--应为视图状态 我曾尝试使用onConfigurationChange方法再次设置内容视图,但我不知道我是否在这里找到了错误的树。我已经从日志中包含了完整的类和错误,以防它有所帮助 package org.project.accessible; import ja
package org.project.accessible;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.*;
public class RegisterScreen extends Activity implements View.OnClickListener{
private EditText username;
private EditText password;
private EditText password2;
private EditText email;
private EditText surname;
private EditText forename;
private EditText street;
private EditText postcode;
private EditText city;
private EditText county;
private String country;
private int checkboxAmount;
private LinearLayout layoutMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initiate the database to be qureied
DatabaseData db = new DatabaseData(this);
db = new DatabaseData(this);
try {
db.createDataBase();
}
catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
db.openDataBase();
}
catch(SQLException sqle){
throw sqle;
}
SQLiteDatabase rdb = db.getReadableDatabase();
//main layout of the screen
layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.VERTICAL);
layoutMain.setBackgroundColor(Color.WHITE);
//Linear Layout for the Banner
LinearLayout banner = new LinearLayout(this);
banner.setOrientation(LinearLayout.VERTICAL);
banner.setBackgroundColor(Color.rgb(17, 168, 191));
//layout params for height and width
LayoutParams bannerParams = new android.widget.LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.FILL_PARENT, 40);
banner.setLayoutParams(bannerParams);
//Banner text
TextView bannerText = new TextView(this);
bannerText.setText("Register");
bannerText.setTextColor(Color.WHITE);
banner.addView(bannerText);
bannerText.setTextSize(24);
bannerText.setGravity(Gravity.CENTER);
//add banner layout to main layout
layoutMain.addView(banner);
//Scroll view for the rest of the screen
ScrollView sv = new ScrollView(this);
//sv.setFillViewport(true);
//Table layout to align the items register form items
TableLayout tl = new TableLayout(this);
//Table rows to put items on left and right sides of the page
TableRow usernameTR = new TableRow(this);
//Username label
TextView usernameL = new TextView(this);
usernameL.setText("Username:");
usernameL.setTextColor(Color.BLACK);
usernameTR.addView(usernameL);
//Username textbox
EditText usernameTB = new EditText(this);
usernameTB.setId(1001);
usernameTB.setSingleLine(true);
usernameTR.addView(usernameTB);
tl.addView(usernameTR);
TableRow passwordTR = new TableRow(this);
//password label
TextView passwordL = new TextView(this);
passwordL.setText("Password:");
passwordL.setTextColor(Color.BLACK);
passwordTR.addView(passwordL);
//password textbox
EditText passwordTB = new EditText(this);
passwordTB.setId(1009);
passwordTB.setSingleLine(true);
passwordTB.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
passwordTB.setTransformationMethod(PasswordTransformationMethod.getInstance());
passwordTR.addView(passwordTB);
tl.addView(passwordTR);
TableRow password2TR = new TableRow(this);
//password2 label
TextView password2L = new TextView(this);
password2L.setText("Confirm Pass:");
password2L.setTextColor(Color.BLACK);
password2TR.addView(password2L);
//password2 textbox
EditText password2TB = new EditText(this);
password2TB.setId(1010);
password2TB.setSingleLine(true);
password2TB.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
password2TB.setTransformationMethod(PasswordTransformationMethod.getInstance());
password2TR.addView(password2TB);
tl.addView(password2TR);
TableRow emailTR = new TableRow(this);
//email label
TextView emailL = new TextView(this);
emailL.setText("Email:");
emailL.setTextColor(Color.BLACK);
emailTR.addView(emailL);
//email textbox
EditText emailTB = new EditText(this);
emailTB.setId(1002);
emailTB.setSingleLine(true);
emailTR.addView(emailTB);
tl.addView(emailTR);
TableRow forenameTR = new TableRow(this);
//forename label
TextView forenameL = new TextView(this);
forenameL.setText("Forename:");
forenameL.setTextColor(Color.BLACK);
forenameTR.addView(forenameL);
//forename textbox
EditText forenameTB = new EditText(this);
forenameTB.setId(1003);
forenameTB.setSingleLine(true);
forenameTR.addView(forenameTB);
tl.addView(forenameTR);
TableRow surnameTR = new TableRow(this);
//surname label
TextView surnameL = new TextView(this);
surnameL.setText("Surname:");
surnameL.setTextColor(Color.BLACK);
surnameTR.addView(surnameL);
//surname textbox
EditText surnameTB = new EditText(this);
surnameTB.setId(1004);
surnameTB.setSingleLine(true);
surnameTR.addView(surnameTB);
tl.addView(surnameTR);
TableRow streetTR = new TableRow(this);
//street label
TextView streetL = new TextView(this);
streetL.setText("Street:");
streetL.setTextColor(Color.BLACK);
streetTR.addView(streetL);
//street textbox
EditText streetTB = new EditText(this);
streetTB.setId(1005);
streetTB.setSingleLine(true);
streetTR.addView(streetTB);
tl.addView(streetTR);
TableRow postcodeTR = new TableRow(this);
//postcode label
TextView postcodeL = new TextView(this);
postcodeL.setText("Postcode:");
postcodeL.setTextColor(Color.BLACK);
postcodeTR.addView(postcodeL);
//postcode textbox
EditText postcodeTB = new EditText(this);
postcodeTB.setId(1006);
postcodeTB.setSingleLine(true);
postcodeTR.addView(postcodeTB);
tl.addView(postcodeTR);
TableRow cityTR = new TableRow(this);
//city label
TextView cityL = new TextView(this);
cityL.setText("City:");
cityL.setTextColor(Color.BLACK);
cityTR.addView(cityL);
//city textbox
EditText cityTB = new EditText(this);
cityTB.setId(1007);
cityTB.setSingleLine(true);
cityTR.addView(cityTB);
tl.addView(cityTR);
TableRow countyTR = new TableRow(this);
//county label
TextView countyL = new TextView(this);
countyL.setText("County:");
countyL.setTextColor(Color.BLACK);
countyTR.addView(countyL);
//county textbox
EditText countyTB = new EditText(this);
countyTB.setId(1008);
countyTB.setSingleLine(true);
countyTR.addView(countyTB);
tl.addView(countyTR);
TableRow countryTR = new TableRow(this);
//country label
TextView countryL = new TextView(this);
countryL.setText("Country:");
countryL.setTextColor(Color.BLACK);
countryTR.addView(countryL);
//country dropdown
Spinner countrySpinner = new Spinner(this);
countrySpinner.setId(1009);
countrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
country = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
//get array of countries
Countries countries = new Countries();
String countriesList[] = countries.getList();
ArrayAdapter<String> countriesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesList);
countrySpinner.setAdapter(countriesAdapter);
//add the array to the dropdown
countryTR.addView(countrySpinner);
tl.addView(countryTR);
TextView catTitle = new TextView(this);
catTitle.setText("\nPlease select the categories which affect you:\n");
catTitle.setTextColor(Color.BLACK);
tl.addView(catTitle);
//categories
//categories title
String[] cols = {"_id", "cat_name"}; //columns to be searched
Cursor cursor = rdb.query("aa_category", cols, null, null, null, null, null); // save the query to the db
checkboxAmount = cursor.getCount();
while (cursor.moveToNext()) {
CheckBox catCB = new CheckBox(this);
String name = cursor.getString(1);
int id = cursor.getInt(0);
catCB.setId(id);
catCB.setText("\n"+name+"\n");
catCB.setTextColor(Color.BLACK);
tl.addView(catCB);
}
cursor.close();
rdb.close();
//add field for new category with a text field that will become active on clicking the checkbox
Button submitButton = new Button(this);
submitButton.setId(99);
submitButton.setText("Submit");
tl.addView(submitButton);
//Add table layout to the scroll view
sv.addView(tl);
//Add scroll view to the main layout
layoutMain.addView(sv);
this.setContentView(layoutMain);
View submitButtonListener = findViewById(99);
submitButtonListener.setOnClickListener(this);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
this.setContentView(layoutMain);
}
public void onClick(View v) {
String messageString = null;
//String usernameStr = null;
//save the id of the edit text fields to a variable
username = (EditText)findViewById(1001);
password = (EditText)findViewById(1009);
password2 = (EditText)findViewById(1010);
email = (EditText)findViewById(1002);
forename = (EditText)findViewById(1003);
surname = (EditText)findViewById(1004);
street = (EditText)findViewById(1005);
postcode = (EditText)findViewById(1006);
city = (EditText)findViewById(1007);
county = (EditText)findViewById(1008);
//save the edit text values
String usernameStr = username.getText().toString();
String passwordStr = password.getText().toString();
String passwordStr2 = password2.getText().toString();
String emailStr = email.getText().toString();
String forenameStr = forename.getText().toString();
String surnameStr = surname.getText().toString();
String streetStr = street.getText().toString();
String postcodeStr = postcode.getText().toString();
String cityStr = city.getText().toString();
String countyStr = county.getText().toString();
String checkboxStr = "";
int checkBoxCheck = 0;
for(int i = 1; i<=checkboxAmount; i++){
CheckBox cbx = (CheckBox)findViewById(i);
if(cbx.isChecked()){
if(checkBoxCheck == 0){
checkboxStr += i;
checkBoxCheck ++;
}
else{
checkboxStr += ":" + i;
}
}
}
//Check for nulls
if(usernameStr.equals("")){
messageString = "Username can't be blank please check and try again";
}
else if(emailStr.equals("")){
messageString = "Email can't be blank please check and try again";
}
else if(forenameStr.equals("")){
messageString = "Forename can't be blank please check and try again";
}
else if(surnameStr.equals("")){
messageString = "Surname can't be blank please check and try again";
}
else if(streetStr.equals("")){
messageString = "Street can't be blank please check and try again";
}
else if(postcodeStr.equals("")){
messageString = "Postcode can't be blank please check and try again";
}
else if(cityStr.equals("")){
messageString = "City can't be blank please check and try again";
}
else if(countyStr.equals("")){
messageString = "County can't be blank please check and try again";
}
else if(country.equals("") || country.equals("Please select a country")){
messageString = "Please select a country";
}
else if(checkBoxCheck == 0){
messageString = "You must select at least one category";
}
else if(passwordStr.equals("")){
messageString = "Password can't be blank please check and try again";
}
else if(passwordStr2.equals("")){
messageString = "Confirm pass can't be blank please check and try again";
}
else if(!passwordStr.equals(passwordStr2)){
messageString = "Passwords don't match please check and try again";
}
else{
//get checkbox values
messageString = "New user added";
ConnectionToDb connec = new ConnectionToDb();
connec.registerUser(usernameStr, passwordStr, emailStr, forenameStr, surnameStr, streetStr, postcodeStr, cityStr, countyStr, country, checkboxStr);
}
new AlertDialog.Builder(this)
.setTitle("Accessible Application")
.setMessage(messageString)
.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
}
}
该特定异常的源代码似乎已更新,并显示了一条更有用的错误消息:
protected void onRestoreInstanceState(Parcelable state) {
mPrivateFlags |= SAVE_STATE_CALLED;
if (state != BaseSavedState.EMPTY_STATE && state != null) {
throw new IllegalArgumentException("Wrong state class, expecting View State but "
+ "received " + state.getClass().toString() + " instead. This usually happens "
+ "when two views of different type have the same id in the same hierarchy. "
+ "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
+ "other views do not use the same id.");
}
}
我可以看到您在代码中两次使用ID1009,这可能与此有关
理想情况下,您应该在布局中使用XML。这会让你的生活更轻松。啊!谢谢,我换了身份证,可以用了。不过,您提到使用xml会更容易。我希望能够用xml来做,但我认为因为我的视图是动态的,例如,从数据库中创建的复选框的数量是动态的,这意味着我不能用xml来做?据我所知,我无法使用xml在运行中循环并添加复选框,对吗?@Gary yeah,这是真的。就像您可以在XML中围绕视图设置布局一样。当涉及到使用条目的动态长度时,您通常可以使用AdapterView。您可以通过适配器控制动态大小的视图。适配器将允许您创建一种基于视图表示的数据模型定义视图的方法。AdapterView通常会调整视图大小、页面和循环视图,以便更快地制作动画,这样您就可以专注于显示数据。查看文档,您可以将两者混合使用。。。没有艰难的选择。在XML中放入固定的内容,并动态创建这些复选框。您可以在运行时使用findById()来定位它们的容器。正如@Greg所说,在没有看到您的UI的情况下很难确定,但是ListView可能更适合您的要求。感谢各位,我已经像您建议的那样将这两种方法混合在一起,现在更容易理解了
protected void onRestoreInstanceState(Parcelable state) {
mPrivateFlags |= SAVE_STATE_CALLED;
if (state != BaseSavedState.EMPTY_STATE && state != null) {
throw new IllegalArgumentException("Wrong state class, expecting View State but "
+ "received " + state.getClass().toString() + " instead. This usually happens "
+ "when two views of different type have the same id in the same hierarchy. "
+ "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
+ "other views do not use the same id.");
}
}