Sunday, April 1, 2018

java - Attempt to invoke virtual method RecyclerView$LayoutManager.onRestoreInstanceState(android.os.Parcelable)' on a null object reference

 I am getting error when i rotate screen 



java.lang.RuntimeException: Unable to resume activity {com.example.naveen.bakingapp/com.example.naveen.bakingapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onRestoreInstanceState(android.os.Parcelable)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3121)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4098)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)
at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onRestoreInstanceState(android.os.Parcelable)' on a null object reference



public class MainActivity extends AppCompatActivity {
// @BindView(R.id.recyclerview) RecyclerView recyclerView;
ArrayListreceipeArrayList;

RecyclerView recyclerView;`enter code here`
Parcelable mListInstanceState;`enter code here`
LinearLayoutManager layoutManager;
private static Bundle mBundleRecyclerViewState;
private final String KEY_RECYCLER_STATE = "recycler_state";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// ButterKnife.bind(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
receipeArrayList=new ArrayList<>();
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);

new GetAllReceipe().execute();
}
public class GetAllReceipe extends AsyncTask
{ String response;

ProgressDialog dialog;
ContentValues receipeValuesArr;
int progressStatus = 0;
boolean running;
@Override
protected void onPreExecute() {
super.onPreExecute();
running = true;
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");

dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
dialog.setProgress(progressStatus);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
running = false;
}
});

}
@Override
protected String doInBackground(String... params) {
URL url ;
HttpURLConnection connection ;
try {
url = new URL("https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json");
connection = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder buffer = new StringBuilder();

String temp;
while ((temp=br.readLine())!=null)
{
buffer.append(temp);
}
response=buffer.toString();

int i = 5;
while (running & progressStatus < 5) {
try {

progressStatus++;
publishProgress(progressStatus);
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}

if (i-- == 0) {
running = false;
}

publishProgress(i);

}
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@Override
protected void onProgressUpdate(Integer... values) {

super.onProgressUpdate(values);
dialog.setProgress(progressStatus);
}
@Override
protected void onPostExecute(String response) {
//Log.i("result",response.toString());
if (dialog!=null&&dialog.isShowing()){dialog.dismiss();}
try {
// JSONObject jsonObject=new JSONObject(response);
JSONArray jsonArray=new JSONArray(response);

for (int i=0;i {
JSONObject object=jsonArray.getJSONObject(i);
receipeValuesArr = new ContentValues();
Receipe receipe=new Receipe();
receipe.setReceipe_name(object.getString("name"));
receipe.setId(object.getInt("id"));
receipeArrayList.add(receipe);
receipeValuesArr.put(ReceipeContract.ReceipeEntry.COLUMN_RECEIPE_ID,object.getInt("id"));
receipeValuesArr.put(ReceipeContract.ReceipeEntry.COLUMN_RECEIPE_NAME, object.getString("name"));

getContentResolver().insert(ReceipeContract.ReceipeEntry.CONTENT_URI,receipeValuesArr);
}
//final RecyclerView.LayoutManager lmanager=new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,true);
layoutManager = new LinearLayoutManager(MainActivity.this);

layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new ReceipeAdapter(receipeArrayList,MainActivity.this));
recyclerView.setItemAnimator(new DefaultItemAnimator());


} catch (JSONException e) {
e.printStackTrace();
}
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);


outState.putParcelable("list_state",layoutManager.onSaveInstanceState());

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);

if (savedInstanceState!=null)

{
mListInstanceState = savedInstanceState.getParcelable("list_state");

}

/*
assert mListInstanceState != null;
//System.out.println(recyclerView.getLayoutManager().toString());
if (recyclerView.getLayoutManager()!=null){
recyclerView.getLayoutManager().onRestoreInstanceState(mListInstanceState);


}
else {
System.out.println("layoutmanager is null");
}*/
}
/* @Override
protected void onResume() {
super.onResume();


if (mListInstanceState != null) {
layoutManager.onRestoreInstanceState(mListInstanceState);
}
}*/


@Override
protected void onPause()
{
super.onPause();


// save RecyclerView state
mBundleRecyclerViewState = new Bundle();
Parcelable listState = recyclerView.getLayoutManager().onSaveInstanceState();
mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, listState);
}

@Override
protected void onResume()
{

super.onResume();

// restore RecyclerView state
if (mBundleRecyclerViewState != null) {
Parcelable listState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE);
recyclerView.getLayoutManager().onRestoreInstanceState(listState);
}
}
}

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...