posted 3 years ago
I am learning ActivityLifeCycle from http://developer.android.com/training/basics/activity-lifecycle/stopping.html and getting message while running the code "Unfortunately ActiviyLifeCycle has stopped" and I want to know what is Notepad.Notes in values.put(Notepad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText()); how to resolve this issue
Below is the code
**ActivityLifeCycle.java**
package com.example.activitylifecycle;
import android.content.ContentValues;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.content.Context;
import android.content.ClipData;
public class MainActivity extends ActionBarActivity {
TextView mTextView; //Member TextView
private Uri mUri;
private int mCurrentScore, mCurrentLevel;
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
/*private static final int COLUMN_NAME_NOTE=0;
private static final int COLUMN_NAME_TITLE=1;*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
super.onCreate(savedInstanceState);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
// Set the user interface layout for this Activity
// The layout file is defined in the project res/layout/main_activity.xml file
setContentView(R.layout.activity_main);
// Initialize member TextView so we can manipulate it later
mTextView = (TextView) findViewById(R.id.text_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// For the main activity, make sure the icon in the action bar
// does not behave as a button
android.app.ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
}
}
public void OnRestoreInstanceState(Bundle savedInstanceState){
//Always call the superclass so it can restore the vie hierarchy
super.onRestoreInstanceState(savedInstanceState);
//Restore state members for state instance
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}
@Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
@Override
protected void onStop(){
super.onStop(); //Always call superclass method 1st
//Save note current draft and we want to be sure current note's progress isn't lost
ContentValues values = new ContentValues();
values.put(Notepad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());
values.put(Notepad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());
getContentResolver().update(
mUri, // Uri for the Note to Update
values, // The map of column name and new values apply to them
null, // No select criteria are used
null // No where column are used
);
}
@Override
protected void onStart() {
super.onStart(); // Always call the superclass method first
// The activity is either being restarted or started for the first time
// so this is where we should make sure that GPS is enabled
LocationManager locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
// Create a dialog here that requests the user to enable GPS, and use an intent
// with the android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS action
// to take the user to the Settings screen to enable GPS when they click "OK"
}
}
@Override
protected void onRestart() {
super.onRestart(); // Always call the superclass method first
// Activity being restarted from stopped state
}
}
**Notepad.java**
package com.example.activitylifecycle;
public class Notepad {
public class Notes {
public static final String COLUMN_NAME_NOTE = "note";
public static final String COLUMN_NAME_TITLE = "title";
}
}
Below is the code
**ActivityLifeCycle.java**
package com.example.activitylifecycle;
import android.content.ContentValues;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.content.Context;
import android.content.ClipData;
public class MainActivity extends ActionBarActivity {
TextView mTextView; //Member TextView
private Uri mUri;
private int mCurrentScore, mCurrentLevel;
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
/*private static final int COLUMN_NAME_NOTE=0;
private static final int COLUMN_NAME_TITLE=1;*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
super.onCreate(savedInstanceState);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
// Set the user interface layout for this Activity
// The layout file is defined in the project res/layout/main_activity.xml file
setContentView(R.layout.activity_main);
// Initialize member TextView so we can manipulate it later
mTextView = (TextView) findViewById(R.id.text_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// For the main activity, make sure the icon in the action bar
// does not behave as a button
android.app.ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
}
}
public void OnRestoreInstanceState(Bundle savedInstanceState){
//Always call the superclass so it can restore the vie hierarchy
super.onRestoreInstanceState(savedInstanceState);
//Restore state members for state instance
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}
@Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
@Override
protected void onStop(){
super.onStop(); //Always call superclass method 1st
//Save note current draft and we want to be sure current note's progress isn't lost
ContentValues values = new ContentValues();
values.put(Notepad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());
values.put(Notepad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());
getContentResolver().update(
mUri, // Uri for the Note to Update
values, // The map of column name and new values apply to them
null, // No select criteria are used
null // No where column are used
);
}
@Override
protected void onStart() {
super.onStart(); // Always call the superclass method first
// The activity is either being restarted or started for the first time
// so this is where we should make sure that GPS is enabled
LocationManager locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
// Create a dialog here that requests the user to enable GPS, and use an intent
// with the android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS action
// to take the user to the Settings screen to enable GPS when they click "OK"
}
}
@Override
protected void onRestart() {
super.onRestart(); // Always call the superclass method first
// Activity being restarted from stopped state
}
}
**Notepad.java**
package com.example.activitylifecycle;
public class Notepad {
public class Notes {
public static final String COLUMN_NAME_NOTE = "note";
public static final String COLUMN_NAME_TITLE = "title";
}
}
Muhammad Ammar Elahi
Greenhorn
Posts: 14
posted 3 years ago
Could you please tell me what is Notepad.Notes is it something class values.put(Notepad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle()); ?
Logcats
12-25 19:20:57.230: E/AndroidRuntime(623): at java.lang.reflect.Method.invokeNative(Native Method)
12-25 19:20:57.230: E/AndroidRuntime(623): at java.lang.reflect.Method.invoke(Method.java:511)
12-25 19:20:57.230: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-25 19:20:57.230: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-25 19:20:57.230: E/AndroidRuntime(623): at dalvik.system.NativeStart.main(Native Method)
12-25 19:20:57.230: E/AndroidRuntime(623): Caused by: java.lang.NullPointerException
12-25 19:20:57.230: E/AndroidRuntime(623): at com.example.activitylifecycle.MainActivity.onCreate(MainActivity.java:25)
12-25 19:20:57.230: E/AndroidRuntime(623): at android.app.Activity.performCreate(Activity.java:5008)
12-25 19:20:57.230: E/AndroidRuntime(623): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-25 19:20:57.230: E/AndroidRuntime(623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
Could you please tell me what is Notepad.Notes is it something class values.put(Notepad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle()); ?
Logcats
12-25 19:20:57.230: E/AndroidRuntime(623): at java.lang.reflect.Method.invokeNative(Native Method)
12-25 19:20:57.230: E/AndroidRuntime(623): at java.lang.reflect.Method.invoke(Method.java:511)
12-25 19:20:57.230: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-25 19:20:57.230: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-25 19:20:57.230: E/AndroidRuntime(623): at dalvik.system.NativeStart.main(Native Method)
12-25 19:20:57.230: E/AndroidRuntime(623): Caused by: java.lang.NullPointerException
12-25 19:20:57.230: E/AndroidRuntime(623): at com.example.activitylifecycle.MainActivity.onCreate(MainActivity.java:25)
12-25 19:20:57.230: E/AndroidRuntime(623): at android.app.Activity.performCreate(Activity.java:5008)
12-25 19:20:57.230: E/AndroidRuntime(623): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-25 19:20:57.230: E/AndroidRuntime(623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
posted 3 years ago
I am guessing you are missing some point.
You are saving something without creating it.
if you look at your code. You are trying to save instance before calling super.onCreate.
I think you should change your code as follows.
Of course this is first thing came up my mind and i am sure someone in here help you better.
You are saving something without creating it.
if you look at your code. You are trying to save instance before calling super.onCreate.
I think you should change your code as follows.
Of course this is first thing came up my mind and i am sure someone in here help you better.
Muhammad Ammar Elahi
Greenhorn
Posts: 14
posted 3 years ago
Brother it is still showing me this error kindly check this i am follow this tutorial developer.android.com
Muhammad Ammar Elahi
Greenhorn
Posts: 14
posted 3 years ago
It seems that savedInstanceState is null. You should notice that you are checking it against null value on line 25 and you have removed
super.onSaveInstanceState(savedInstanceState)
from your new code. Add that line in start of method.
You can check your variable values by printing them in logcat.
super.onSaveInstanceState(savedInstanceState)
from your new code. Add that line in start of method.
You can check your variable values by printing them in logcat.
**OCP, Java SE 6 Programmer**OCM, Java SE 6 Developer**
