Android ScrollView错误

时间:2017-05-30 04:35:47

标签: android nullpointerexception android-scrollview

我正在尝试恢复滚动视图离开应用时的位置以及旋转应用时的位置。

由于某种原因,它总是抛出这个错误:

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

以下是代码:

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




}

在最后两个方法中它工作正常,该类在清单中,并且从第12行抛出错误(其中实例化scrollview对象)id也是有效的id。 非常感谢你

2 个答案:

答案 0 :(得分:1)

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

findViewbyId 必须位于实例方法中。 例如,我把它放在onCreate

答案 1 :(得分:0)

您应该在 setContentView(R.layout.activity_benching)之后在 onCreate()中写ScrollView mScrollView = (ScrollView) findViewById(R.id.benchingScroll);; 更改您的代码,如下所示

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