我对方向更改有疑问。在过去的24小时内,我一直在努力解决这个问题。以下是我的问题。感谢任何帮助。
我正在onSaveInstanceState
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-23 12:37:26.914: I/System.out(20809): Getting EXISTING testPaperQuestion
11-23 12:37:26.914: I/System.out(20809): From EXISTING instance but NULL
11-23 12:37:26.914: E/InputEventReceiver(20809): Exception dispatching input event.
在我继续阅读一些文档的同时请注意一下。谢谢你的帮助......
请注意,此问题是随机发生的。
这是完整的活动......
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) {}
}
}
以下是TestPaperQuestion的代码
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();
}
};
}
答案 0 :(得分:0)
在您的代码中,我找不到您初始化testPaperQuestion
的地方。您确定自己的登录newInstance
未输出"ERROR:PUTTING NULL AS QUESTION PAPER"
吗?
检查testPaperQuestion = (TestPaperQuestion)getArguments().getSerializable(Q_ID)
上的代码,确保您尚未在那里检索空值。
您需要先创建一个新的testPaperQuestion
,然后才能将其保存到onSaveInstance()
。调试代码以了解发生的情况。以下是一些提示:
How to Debug Android application line by line using Eclipse?
此外,您可能需要考虑使用Log.d(tag, message)
而不是使用System.out.println()
来输出消息。
希望它有所帮助。