Android 方向上的savedInstanceState中缺少数据
我对方向的改变有意见。在过去的24小时里,我一直在努力解决这个问题,而且更加认真。下面是我的问题。谢谢你的帮助 我正在将数据保存在SaveInstanceState中Android 方向上的savedInstanceState中缺少数据,android,android-viewpager,Android,Android Viewpager,我对方向的改变有意见。在过去的24小时里,我一直在努力解决这个问题,而且更加认真。下面是我的问题。谢谢你的帮助 我正在将数据保存在SaveInstanceState中 public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); savedInstanceState.putSerializable("testPaperQuest
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putSerializable("testPaperQuestion", testPaperQuestion);
}
在我的onCreate
方法中,我检索如下
if (savedInstanceState != null) {
System.out.println("Getting EXISTING testPaperQuestion");
testPaperQuestion = (TestPaperQuestion) savedInstanceState.getSerializable("testPaperQuestion");
if (testPaperQuestion == null)
System.out.println("From EXISTING instance but NULL");
} else {
System.out.println("Getting NEW testPaperQuestion");
testPaperQuestion = (TestPaperQuestion) getArguments().getSerializable(Q_ID);
}
按日志显示如下内容
11-2312:37:26.914:I/System.out(20809):获取现有测试文件问题
11-23 12:37:26.914:I/System.out(20809):来自现有实例,但为空
11-23 12:37:26.914:E/InputEventReceiver(20809):异常调度输入事件。
在我继续阅读一些文档时,请给我一些提示。谢谢你的帮助
请注意,此问题是随机发生的
以下是完整的活动
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Chronometer;
import com.actionbarsherlock.app.SherlockFragment;
import com.scan.jee.R;
import com.scan.model.Question;
import com.scan.model.TestPaperQuestion;
import com.scan.util.GatherData;
public class SingleTestQuestionView extends SherlockFragment implements
View.OnClickListener {
private static String Q_ID = "Q_ID";
public static final SingleTestQuestionView newInstance(TestPaperQuestion testPaperQuestion) {
SingleTestQuestionView fragment = new SingleTestQuestionView();
Bundle bdl = new Bundle(1);
bdl.putSerializable(Q_ID, testPaperQuestion);
if (testPaperQuestion == null){
Log.d("SingleTestQuestionView", "ERROR:PUTTING NULL AS QUESTION PAPER");
}
fragment.setArguments(bdl);
return fragment;
}
private static final String pre_1 = "<!DOCTYPE html>"
+ "<html lang='en' xmlns:m='http://www.w3.org/1998/Math/MathML'>"
+ "<head>"
+ " <link rel='stylesheet' href='http://fonts.googleapis.com/css?family=UnifrakturMaguntia'>"
+ " <link rel='stylesheet' href='file:///android_asset/jqmath-0.4.0.css'>"
+ " <script src='file:///android_asset/jquery-1.4.3.min.js'></script>"
+ " <script src='file:///android_asset/jqmath-etc-0.4.0.min.js'></script>"
+ "</head>"
+ "<body bgcolor='#d3ffce'>"
+ "<div style='border: 1px solid black;'><p><font face='SERIF'size='4'>";
private static final String pre_2 = "</font></p></div><br><div style='border: 1px solid black;'><TABLE width='100%'><TR><TD ALIGN=CENTER width='10'><font style='border: 1px solid black;'face='SERIF'size='4'>A</font></TD><TD ALIGN=LEFT><font face='SERIF'size='4'>";
private static final String pre_3 = "</font></TD></TR><TR><TD ALIGN=CENTER><font style='border: 1px solid black;' face='SERIF'size='4'>B</font></TD><TD ALIGN=LEFT><font face='SERIF'size='4'>";
private static final String pre_4 = "</font></TD></TR><TR><TD ALIGN=CENTER><font style='border: 1px solid black;'face='SERIF'size='4'>C</font></TD><TD ALIGN=LEFT><font face='SERIF'size='4'>";
private static final String pre_5 = "</font></TD></TR><TR><TD ALIGN=CENTER><font style='border: 1px solid black;'face='SERIF'size='4'>D</font></TD><TD ALIGN=LEFT><font face='SERIF'size='4'>";
private static final String pre_6 = "</font></TD></TR></TABLE></div></body></html>";
// Declare Variables
private WebView webQuestion;
private Button option1;
private Button option2;
private Button option3;
private Button option4;
private Chronometer chronometer = null;
private Question qustion = null;
private TestPaperQuestion testPaperQuestion = null;
public Question getQustion() {
return qustion;
}
public void setQustion(Question qus) {
this.qustion = qus;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.question_item, container,
false);
chronometer = (Chronometer) rootView.findViewById(R.id.chronometer);
if (savedInstanceState != null) {
Log.d("SingleTestQuestionView", "Getting EXISTING testPaperQuestion");
testPaperQuestion = (TestPaperQuestion) savedInstanceState.getSerializable("testPaperQuestion");
if (testPaperQuestion == null)
Log.d("SingleTestQuestionView", "From EXISTING instance but NULL");
} else {
Log.d("SingleTestQuestionView", "Getting NEW testPaperQuestion");
testPaperQuestion = (TestPaperQuestion) getArguments().getSerializable(Q_ID);
if (testPaperQuestion == null)
Log.d("SingleTestQuestionView", "DID I Get the NULL for the new Instance?");
}
Log.d("SingleTestQuestionView", "Finished Creating new Item with Id <" + testPaperQuestion.getId() + ">");
chronometer.setBase(SystemClock.elapsedRealtime() + testPaperQuestion.getSeconds_to_answer());
qustion = testPaperQuestion.getQuestion();
webQuestion = (WebView) rootView.findViewById(R.id.question);
String htmlMsg = "";
htmlMsg = pre_1 + qustion.getQuestion() + pre_2 + qustion.getOption1() + pre_3
+ qustion.getOption2() + pre_4 + qustion.getOption3() + pre_5
+ qustion.getOption4() + pre_6;
webQuestion.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webQuestion.getSettings().setJavaScriptEnabled(true);
webQuestion.getSettings().setAppCacheEnabled(true);
webQuestion.loadData(htmlMsg, "text/html", "UTF-8");
option1 = (Button) rootView.findViewById(R.id.option1);
option2 = (Button) rootView.findViewById(R.id.option2);
option3 = (Button) rootView.findViewById(R.id.option3);
option4 = (Button) rootView.findViewById(R.id.option4);
option1.setText("A");
option1.setOnClickListener(this);
option2.setText("B");
option2.setOnClickListener(this);
option3.setText("C");
option3.setOnClickListener(this);
option4.setText("D");
option4.setOnClickListener(this);
if (testPaperQuestion.getUser_answer() != -1) {
switch (testPaperQuestion.getUser_answer()) {
case 1:
changeState(R.id.option1);
break;
case 2:
changeState(R.id.option2);
break;
case 3:
changeState(R.id.option3);
break;
case 4:
changeState(R.id.option4);
break;
default:
break;
}
}
return rootView;
}
public void stopTimer() {
testPaperQuestion.setSeconds_to_answer(chronometer.getBase() - SystemClock.elapsedRealtime());
chronometer.stop();
}
public void startTimer() {
chronometer.setBase(SystemClock.elapsedRealtime() + testPaperQuestion.getSeconds_to_answer());
chronometer.start();
}
public void onClick(View v) {
changeState(v.getId());
//Update Chapter details.....
FragmentTestCenter.updateAdapter(testPaperQuestion);
}
private void changeState(int userSelectionId) {
int userAnswer = -1;
Button userOoption = null;
switch (userSelectionId) {
case R.id.option1:
userAnswer = 1;
userOoption = option1;
break;
case R.id.option2:
userAnswer = 2;
userOoption = option2;
break;
case R.id.option3:
userAnswer = 3;
userOoption = option3;
break;
case R.id.option4:
userAnswer = 4;
userOoption = option4;
break;
default:
break;
}
testPaperQuestion.setUser_answer(userAnswer);
//testPaperQuestion.setSeconds_to_answer(chronometer.getBase() - SystemClock.elapsedRealtime());
//chronometer.stop();
option1.setBackgroundColor(Color.LTGRAY);
option2.setBackgroundColor(Color.LTGRAY);
option3.setBackgroundColor(Color.LTGRAY);
option4.setBackgroundColor(Color.LTGRAY);
userOoption.setBackgroundColor(Color.YELLOW);
}
public void onSaveInstanceState(Bundle savedInstanceState) {
Log.d("SingleTestQuestionView", "Calling onSaveInstanceState");
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putSerializable("testPaperQuestion", testPaperQuestion);
if (testPaperQuestion == null){
Log.d("SingleTestQuestionView", "ERROR: SAVING testPaperQuestion AS NULL");
}
}
public void onDestroy () {
super.onDestroy();
//new updateTestQuestion().execute(new String[] { "" });
}
public void onPause () {
super.onPause();
new updateTestQuestion().execute(new String[] { "" });
}
public class updateTestQuestion extends AsyncTask<String, Void, List<TestPaperQuestion>> {
protected List<TestPaperQuestion> doInBackground(String... strs) {
GatherData.updateTestQuestion (testPaperQuestion);
return new ArrayList();
}
protected void onPostExecute(List<TestPaperQuestion> chapterNames) {}
}
}
import java.util.ArrayList;
导入java.util.List;
导入android.graphics.Color;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.os.SystemClock;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.webkit.WebView;
导入android.widget.Button;
导入android.widget.Chronometer;
导入com.actionbarsherlock.app.SherlockFragment;
导入com.scan.jee.R;
导入com.scan.model.Question;
导入com.scan.model.TestPaperQuestion;
导入com.scan.util.GatherData;
公共类SingleTestQuestionView扩展了SherlockFragment实现
View.OnClickListener{
私有静态字符串Q_ID=“Q_ID”;
公共静态最终SingleTestQuestionView新实例(TestPaperQuestion TestPaperQuestion){
SingleTestQuestionView片段=新的SingleTestQuestionView();
Bundle bdl=新Bundle(1);
bdl.putSerializable(Q_ID,testPaperQuestion);
如果(testPaperQuestion==null){
Log.d(“SingleTestQuestionView”,“错误:将NULL作为问题纸放置”);
}
fragment.setArguments(bdl);
返回片段;
}
私有静态最终字符串pre_1=“”
+ ""
+ ""
+ " "
+ " "
+ " "
+ " "
+ ""
+ ""
+“”;
私有静态最终字符串pre_2=“
A”;
私有静态最终字符串pre_3=“B”;
私有静态最终字符串pre_4=“C”;
私有静态最终字符串pre_5=“D”;
私有静态最终字符串pre_6=“”;
//声明变量
私有网络视图网络问题;
私人按钮选项1;
私人按钮选项2;
私人按钮选项3;
私人按钮选项4;
私人天文钟=零;
私有问题qustion=null;
私有TestPaperQuestion TestPaperQuestion=null;
公共问题getQustion(){
返回量;
}
公共无效设置问题(问题){
this.qustion=qus;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(右布局。问题\u项目,容器,
假);
天文钟=(天文钟)rootView.findviewbyd(R.id.天文钟);
如果(savedInstanceState!=null){
Log.d(“SingleTestQuestionView”,“获取现有testPaperQuestion”);
testPaperQuestion=(testPaperQuestion)savedInstanceState.getSerializable(“testPaperQuestion”);
如果(testPaperQuestion==null)
d(“SingleTestQuestionView”,“来自现有实例,但为空”);
}否则{
Log.d(“SingleTestQuestionView”,“获取新的testPaperQuestion”);
testPaperQuestion=(testPaperQuestion)getArguments().getSerializable(Q_ID);
如果(testPaperQuestion==null)
Log.d(“SingleTestQuestionView”,“我是否得到了新实例的空值?”);
}
Log.d(“SingleTestQuestionView”,“已完成创建Id为的新项”);
计时器.setBase(SystemClock.elapsedRealtime()+testPaperQuestion.getSeconds\u to\u answer());
qustion=testPaperQuestion.getQuestion();
webQuestion=(WebView)rootView.findviewbyd(R.id.question);
字符串htmlMsg=“”;
htmlMsg=pre_1+qustion.getQuestion()+pre_2+qustion.getOption1()+pre_3
+qustion.getOption2()+pre_4+qustion.getOption3()+pre_5
+qustion.getOption4()+pre_6;
webQuestion.setLayerType(View.LAYER\u TYPE\u软件,null);
webQuestion.getSettings().setJavaScriptEnabled(true);
webQuestion.getSettings().setAppCacheEnabled(true);
加载数据(htmlMsg,“文本/html”,“UTF-8”);
option1=(按钮)rootView.findViewById(R.id.option1);
option2=(按钮)rootView.findViewById(R.id.option2);
option3=(按钮)rootView.findViewById(R.id.option3);
option4=(按钮)rootView.findViewById(R.id.option4);
选项1.设置文本(“A”);
选项1.setOnClickListener(此选项);
选择2.设置文本(“B”);
选项2.setOnClickListener(此选项);
选项3.设置文本(“C”);
选项3.setOnClickListener(此选项);
选项4.设置文本(“D”);
选项4.setOnClickListener(此选项);
if(testPaperQuestion.getUser\u answer()!=-1){
开关(testPaperQuestion.getUser\u answer()){
案例1:
变更状态(R.id.option1);
打破
案例2:
变更状态(R.id.option2);
打破
案例3:
变更状态(R.id.option3);
打破
案例4:
变更状态(R.id.option4);
打破
违约:
打破
}
}
返回rootView;
}
公共无效停止计时器(){
testPaperQuestion.setSeconds到答案(chronometer.getBase()-SystemClock.elapsedRealtime());
计时器。停止();
}
公共无效startTimer(){
chr
import java.io.Serializable;
import java.util.Comparator;
public class TestPaperQuestion implements Serializable,
Comparable<TestPaperQuestion> {
private int id;
private int test_paper_id;
private int subject_id;
private int question_id;
private int user_answer;
private long seconds_to_answer;
private Question question;
public Question getQuestion() {
return question;
}
public void setQuestion(Question question) {
this.question = question;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getTest_paper_id() {
return test_paper_id;
}
public void setTest_paper_id(int test_paper_id) {
this.test_paper_id = test_paper_id;
}
public int getSubject_id() {
return subject_id;
}
public void setSubject_id(int subject_id) {
this.subject_id = subject_id;
}
public int getQuestion_id() {
return question_id;
}
public void setQuestion_id(int question_id) {
this.question_id = question_id;
}
public int getUser_answer() {
return user_answer;
}
public void setUser_answer(int user_answer) {
this.user_answer = user_answer;
}
public long getSeconds_to_answer() {
return seconds_to_answer;
}
public void setSeconds_to_answer(long seconds_to_answer) {
this.seconds_to_answer = seconds_to_answer;
}
public int compareTo(TestPaperQuestion testPaperQuestion) {
int compare = -1;
if (testPaperQuestion != null
&& testPaperQuestion.getQuestion() != null) {
compare = user_answer - testPaperQuestion.getUser_answer();
}
return compare;
}
public static Comparator<TestPaperQuestion> Comparator = new Comparator<TestPaperQuestion>() {
public int compare(TestPaperQuestion testPaperQuestion1,
TestPaperQuestion testPaperQuestion2) {
// ascending order
return testPaperQuestion2.getUser_answer() - testPaperQuestion1.getUser_answer();
}
};
}