0

I have an issue with Orientation changes. I have been trying to fix this issue for sometime and more seriously in last 24 hours. Below is my issue. Appreciate any help.

I am saving my data in onSaveInstanceState

public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putSerializable("testPaperQuestion", testPaperQuestion);
}

And in my onCreate method I am retrieving as follows

    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);
    }

By log shows the below

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.

Please throw some light while I continue to read some documentation. Thanks for your help....

Please note this issue happens randomly.

Here is the full activity....

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) {}
    }

}

Here is the code for 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();
        }

    };

}
Chinta
  • 85
  • 1
  • 8

1 Answers1

0

In your code, I could not find anywhere a place where you initialize your testPaperQuestion. Are you sure your log on newInstance is not outputting "ERROR:PUTTING NULL AS QUESTION PAPER"?

Check your code on testPaperQuestion = (TestPaperQuestion)getArguments().getSerializable(Q_ID) to make sure you are not already retrieving a null value there.

You need to create a new testPaperQuestion somewhere before you can save it to on onSaveInstance(). Debug your code to understand what is happening. Here are some tips: How to Debug Android application line by line using Eclipse?

Also, you may want to consider using Log.d(tag, message) instead of using System.out.println() to output messages.

Hope it helps.

Community
  • 1
  • 1
Ricardo
  • 7,785
  • 8
  • 40
  • 60
  • Added a debug msg after but never saw that message in the log but still got this exception...... 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?"); – Chinta Nov 23 '13 at 12:41
  • I am inclined to believe there is a bug in the support library. Is it too soon to assume that as this can be a problem somewhere else like ViewPager?..... – Chinta Nov 23 '13 at 12:43
  • I don't undestand. Is the `if (testPaperQuestion == null)` in your comment above true or false? Also, in your `newInstance()`, did you check there if before you add it to the `Bundle` if `testPaperQuestion` is null? – Ricardo Nov 23 '13 at 13:09
  • Can you put the full code of your `testPaperQuestion` on your question please? – Ricardo Nov 23 '13 at 13:18
  • Ricardo, The testPaperQuestion == null condition is always false. Also checked if testPaperQuestion is null while adding to bundle. Updated the activity code and added the TestPaperQuestion as well.... – Chinta Nov 23 '13 at 15:40
  • @Chinta, I just built a test project here and on my virtual machine your code posted works just fine. I initialized it as follows: `TestPaperQuestion q = new TestPaperQuestion();` `myFragment = SingleTestQuestionView.newInstance(q);` You can see the source code I used here: https://github.com/riclage/fragmentwithinfragment – Ricardo Nov 23 '13 at 16:13
  • Ricardo, Thanks for taking time work on this issue. – Chinta Nov 24 '13 at 03:37