我的Android应用程序有另一个问题我无法找到答案基本上我有一个以编程方式创建的android视图,然后当我更改设备的方向时,应用程序因以下原因崩溃:
java.lang.IllegalArgumentException:错误的州级 - 期待视图状态
我曾尝试使用onConfigurationChange方法然后再次设置内容视图,但我不知道我是否在这里咆哮错误的树。我已经在日志中包含了完整的类和错误,以防万一。
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();
}
}
Stacktrace:
I/WindowManager( 84): Setting rotation to 1, animFlags=1
I/WindowManager( 84): Config changed: { scale=1.0 imsi=0/0 loc=en_GB touch=3 keys=2/1/1 nav=3 orien=2 layout=18}
D/StatusBar( 84): updateResources
D/AndroidRuntime( 1357): Shutting down VM
W/dalvikvm( 1357): threadid=3: thread exiting with uncaught exception (group=0x4001da28)
E/AndroidRuntime( 1357): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 1357): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.project.accessible/org.project.accessible.RegisterScreen}: java.lang.IllegalArgumentException: Wrong state class -- expecting View State
E/AndroidRuntime( 1357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
E/AndroidRuntime( 1357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime( 1357): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3502)
E/AndroidRuntime( 1357): at android.app.ActivityThread.access$2200(ActivityThread.java:116)
E/AndroidRuntime( 1357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1798)
E/AndroidRuntime( 1357): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1357): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1357): at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime( 1357): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1357): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 1357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime( 1357): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1357): Caused by: java.lang.IllegalArgumentException: Wrong state class -- expecting View State
E/AndroidRuntime( 1357): at android.view.View.onRestoreInstanceState(View.java:5713)
E/AndroidRuntime( 1357): at android.widget.TextView.onRestoreInstanceState(TextView.java:2408)
E/AndroidRuntime( 1357): at android.view.View.dispatchRestoreInstanceState(View.java:5689)
E/AndroidRuntime( 1357): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1125)
E/AndroidRuntime( 1357): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1125)
E/AndroidRuntime( 1357): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1125)
E/AndroidRuntime( 1357): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1125)
E/AndroidRuntime( 1357): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1125)
E/AndroidRuntime( 1357): at android.view.View.restoreHierarchyState(View.java:5668)
E/AndroidRuntime( 1357): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1506)
E/AndroidRuntime( 1357): at android.app.Activity.onRestoreInstanceState(Activity.java:833)
E/AndroidRuntime( 1357): at android.app.Activity.performRestoreInstanceState(Activity.java:805)
E/AndroidRuntime( 1357): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1172)
E/AndroidRuntime( 1357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2378)
E/AndroidRuntime( 1357): ... 12 more
答案 0 :(得分:4)
该特定异常的源代码似乎已使用更有用的错误消息进行了更新:
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.");
}
}
我可以看到您在代码中使用ID 1009两次,这可能与它有关。
理想情况下,您将使用XML进行布局。它会让你的生活变得更加轻松。