- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的 android 应用程序有另一个问题,我找不到答案,基本上我有一个以编程方式创建的 android View ,然后当我更改设备的方向时,应用程序崩溃,原因是:
java.lang.IllegalArgumentException: Wrong state class -- expecting View State
我曾尝试使用 onConfigurationChange 方法再次设置内容 View ,但我不知道我是否在这里找错了树。我已经包含了日志中的完整类和错误,以防有帮助。
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();
}
}
堆栈跟踪:
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
最佳答案
该特定异常的源代码似乎已使用更有用的错误消息进行了更新:
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 进行布局。它会让您的生活变得更加轻松。
关于Android 应用程序在方向更改时崩溃 java.lang.IllegalArgumentException : Wrong state class -- expecting View State,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5059439/
我只想知道它们之间的区别: .class .class{ font-size:14px; } 对比: .class > .class{ font-size:14px; } 是一样的东西吗? 最佳答案
PrimeFaces 文档的以下摘录使标题中描述的两个选择器之间似乎存在差异: .ui-widget, .ui-widget .ui-widget { font-size: 90% !imp
我正在尝试选择特定值。但我遇到了一个问题。 我有两个元素,一个有 X Y,另一个有 X Y Z。 当选择 X Y Z 时,我也收到 X Y 的值...有没有办法让它寻找 X AND Y AND Z 而
.class.class 和 .class .class 有什么区别? 最佳答案 .class .class 匹配类 .class 的任何元素,这些元素是类 .class< 的另一个元素的后代/. .
我正在研究 Classname.class 和 Classname.class.toString() 并发现了一些不寻常的东西。 .class 在同一个类上运行时似乎等同于 .class。尽管 .cl
我试图在Dart中扩展列表并在此列表中使用另一个类。 这是我的示例,其中注释出了问题: import "Radio.dart"; // extends ListBase { List ra
我有一个很大的“经理”类,我认为它做得太多了,但我不确定如何将它划分为更多逻辑单元。 一般来说类主要由以下方法组成: class FooBarManager{ GetFooEntities();
我在一个文件中定义了一个抽象父类(super class),在另一个文件中定义了一个子类。我需要父类(super class)文件和堆栈跟踪报告来找到一个包含它。 但是,当它到达“extends”行时
我在 A. Alexenderscu 的现代 C++ 设计中找到了一些模板示例 作者使用以下行的地方 template class CheckingPolicy // class SmartPt
看一下这段代码: public static class A { public void doA() { } } public static class B extends A {
我有两个具有 .body 类的 div,但是,一个位于另一个具有 .box 类的 div 中 - 如下所示: 我只想为 .box 内部的 .body 设置样式...但我在下面所
我一定是遗漏了 C++ 规范中的某些内容,因为我无法解释为什么以下代码可以成功编译: class MyClass { static void fun(); }; int main() { MyClas
我正在尝试在 python 中“模拟”命名空间。我使用内部和外部类层次结构来创建我的命名空间。例如,您希望将文件(如资源)的路径保存在一个位置。我试过这样的事情: src = #path to sou
在试验 online crystal compiler 时(这太棒了),我遇到了一个我似乎无法找到解释的错误: class Person class Current < self end
在查看我的一段代码时,我陷入了如下的一条语句。 TMyObjectClass = TMyObject 类; 我有点困惑,不知道这句话是什么意思。由于 TMyObjectClass 在该语句上方没有声明
我正在编写一个简单的应用程序,以学习一些基本的Dart编程,但无法弄清楚其结构和包含的内容-我得到了一个重复的类Point 首先,我有一个叫做MouseTrack的主类。它将初始化列表并产生循环。 #
在 org.springframework.core.SerializableTypeWrapper (版本 5.2.3),第 112 行有以下代码: if (GraalDetector.in
我希望将鼠标悬停在子导航中的列表项上,以激活页面上该类别中所有项的类(不仅仅是父元素或同级元素)。有任何想法吗?这是我的意思的一个例子: img.BLUE {border:1px solid #FF
我正在通过 ClassLoader 加载类: Class clazz = urlClassLoader.loadClass(name.substring(0, name.length() - 6).r
以下简化的类在从 get() 返回值时执行不同的操作,具体取决于该类是被赋予 double 值还是数组作为模板参数: #include "array" #include "type_traits" t
我是一名优秀的程序员,十分优秀!