-1

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Edon Freiner
  • 338
  • 2
  • 17
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Phantômaxx May 30 '17 at 06:14

2 Answers2

2
ScrollView mScrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_benching);
    mScrollView = (ScrollView) findViewById(R.id.benchingScroll);
}

The findViewbyId is has to be inside a instance method. see for example i have put it in onCreate

peeyush pathak
  • 3,663
  • 3
  • 19
  • 25
  • Thank you! Is this true for all instantiations of an object or just the findViewbyId method? – Edon Freiner May 30 '17 at 04:59
  • See you define objects default value. but findviewbyId is a function of activity AppCompatActivity so it can be only be accessed by its object, but it is better that create an initializer method so that everything is in a single place. – peeyush pathak May 30 '17 at 11:58
0

You should write ScrollView mScrollView = (ScrollView) findViewById(R.id.benchingScroll); inside onCreate() after setContentView(R.layout.activity_benching); Change your code like below

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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_benching);
       mScrollView = (ScrollView) findViewById(R.id.benchingScroll);    
     }
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]);
                }
            });
    }
}
T B
  • 24
  • 4