I am trying to restore the position of where my scroll view was when I leave the app and when I rotate the app as well.
For some reason it always throws this error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.edonfreiner.siddur/com.example.edonfreiner.siddur.Benching}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
Here is the code:
package com.example.edonfreiner.siddur;
import android.support.v4.view.ScrollingView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import java.util.logging.Logger;
public class Benching extends AppCompatActivity {
ScrollView mScrollView = (ScrollView) findViewById(R.id.benchingScroll);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_benching);
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d("Rotated", "rotated");
outState.putIntArray("ARTICLE_SCROLL_POSITION",
new int[]{ mScrollView.getScrollX(), mScrollView.getScrollY()});
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d("Rotated1", "rotated1");
final int[] position = savedInstanceState.getIntArray("ARTICLE_SCROLL_POSITION");
if(position != null)
mScrollView.post(new Runnable() {
public void run() {
mScrollView.scrollTo(position[0], position[1]);
}
});
}
}
It works fine with out the last 2 methods, the class is in the manifest and the error is thrown from line 12 (where the scrollview object is instantiated) The id is also a valid id. Thank you so much