'Get bundle values in Android

I'm still really confused why I'm having error getting values from the bundle of my main activity.

So, here's my code snippet:

Level1.class

Chronometer chrono;
chrono = (Chronometer)findViewById(R.id.chronometer1);
chrono.start();

long timeElapsed = SystemClock.elapsedRealtime() - chrono.getBase();
int hours = (int) (timeElapsed / 3600000);
int minutes = (int) (timeElapsed - hours * 3600000) / 60000;
int seconds = (int) (timeElapsed - hours * 3600000 - minutes * 60000) / 1000;

Intent lvl1 = new Intent(getApplicationContext(), Finish.class);    
Bundle time1 = new Bundle();
time1.putInt("hour1", hours);
time1.putInt("minutes1", minutes);
time1.putInt("seconds1", seconds);

lvl1.putExtras(time1);

and my Finish.class: (inside the onCreate method)

TextView Set1;

Set1 = (TextView) findViewById (R.id.time1);

Bundle time1 = getIntent().getExtras();
int hrs = time1.getInt("hour1");
int min = time1.getInt("minutes1"); 
int sec = time1.getInt("seconds1");


Set1.setText(hrs + ":" + min + ":" + sec);

Could anyone tell me why I'm having errors?

Note: ERROR in line 30, which is the int hrs = time1.getInt("hour1");.

LOGCAT:

08-20 01:54:10.461: E/AndroidRuntime(950): FATAL EXCEPTION: main
08-20 01:54:10.461: E/AndroidRuntime(950): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathattack/com.example.mathattack.Finish}: java.lang.NullPointerException
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.os.Looper.loop(Looper.java:123)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-20 01:54:10.461: E/AndroidRuntime(950):  at java.lang.reflect.Method.invokeNative(Native Method)
08-20 01:54:10.461: E/AndroidRuntime(950):  at java.lang.reflect.Method.invoke(Method.java:521)
08-20 01:54:10.461: E/AndroidRuntime(950):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-20 01:54:10.461: E/AndroidRuntime(950):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-20 01:54:10.461: E/AndroidRuntime(950):  at dalvik.system.NativeStart.main(Native Method)
08-20 01:54:10.461: E/AndroidRuntime(950): Caused by: java.lang.NullPointerException
08-20 01:54:10.461: E/AndroidRuntime(950):  at com.example.mathattack.Finish.onCreate(Finish.java:30)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-20 01:54:10.461: E/AndroidRuntime(950):  ... 11 more


Solution 1:[1]

You need to debug your code and put break points where you get your bundle from your intent to check the value in the bundle. Without doing that your guess is as good as ours.

Solution 2:[2]

From the docs of putExtras (emphasis mine):

Add a set of extended data to the intent. The keys must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

Use Intent.putExtra("hour1", hours); instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James andresakis
Solution 2 Delyan