我正試圖恢復當我離開應用程序和旋轉應用程序時滾動視圖的位置。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]);
}
});
}
}
它工作正常了最後2種方法,類是在清單和錯誤是從第12行(scrollview對象被實例化的地方)拋出的。id也是一個有效的id。 謝謝你這麼多
可能的複製[什麼是空指針異常,以及如何解決?(https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-以及如何解決我的問題) –