Reputation: 69
I already tried this code in my activity ..
In my First Activity this is my code to pass data :
Intent intent = new Intent(getApplicationContext(), TeacherSideQuestion2.class);
Bundle bundle = new Bundle();
bundle.putInt("score", finalScore);
intent.putExtras(bundle);
startActivityForResult(intent,0);
and in my Second Activity , here's the code to retrieve my data .
public void onClick() {
Bundle bundle = getIntent().getExtras();
// int temp = extras.getInt("score", 0);
// int temp = i.getExtras().getInt("score", 0);
if (bundle != null) {
int temp = bundle.getInt("score", 0);
if (temp >= 1) {
Intent intent = new Intent(TeacherSideQuestion2.this,
CongratsEnglish.class);
startActivity(intent);
} else {
Intent intent1 = new Intent(TeacherSideQuestion2.this,
FailedEnglish.class);
startActivity(intent1);
}
}
}
but when I try to click the onClick event , my application will crash .
can anyone fix this ?
Thank you for your answers .
Here's the LogCat says :
12-20 03:51:38.973: E/AndroidRuntime(3051): FATAL EXCEPTION: main
12-20 03:51:38.973: E/AndroidRuntime(3051): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
12-20 03:51:38.973: E/AndroidRuntime(3051): at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
12-20 03:51:38.973: E/AndroidRuntime(3051): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
12-20 03:51:38.973: E/AndroidRuntime(3051): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
12-20 03:51:38.973: E/AndroidRuntime(3051): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
12-20 03:51:38.973: E/AndroidRuntime(3051): at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 03:51:38.973: E/AndroidRuntime(3051): at android.os.Looper.loop(Looper.java:137)
12-20 03:51:38.973: E/AndroidRuntime(3051): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-20 03:51:38.973: E/AndroidRuntime(3051): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 03:51:38.973: E/AndroidRuntime(3051): at java.lang.reflect.Method.invoke(Method.java:511)
12-20 03:51:38.973: E/AndroidRuntime(3051): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-20 03:51:38.973: E/AndroidRuntime(3051): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-20 03:51:38.973: E/AndroidRuntime(3051): at dalvik.system.NativeStart.main(Native Method)
12-20 03:51:38.973: E/AndroidRuntime(3051): Caused by: java.lang.NullPointerException
12-20 03:51:38.973: E/AndroidRuntime(3051): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
12-20 03:51:38.973: E/AndroidRuntime(3051): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
12-20 03:51:38.973: E/AndroidRuntime(3051): at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
12-20 03:51:38.973: E/AndroidRuntime(3051): ... 11 more
12-20 03:51:40.223: E/Trace(3085): error opening trace file: No such file or directory (2)
Upvotes: 0
Views: 155
Reputation:
Change startActivityForResult(intent,0);
to startActivity(intent);
because you don't need to start it for a result just to pass a value to another activity.
I'm not sure if this would be a problem, but you may need to change getApplicationContext()
to YourActivityName.this
.
Last thing, you don't need to create a bundle to send a value to another activity. You can just do it like this:
Intent intent = new Intent(YourActivityName.this, TeacherSideQuestion2.class);
intent.putExtra("score",finalscore);
startActivity(intent);
Upvotes: 0
Reputation: 5063
In your First Activity this is my code to pass data :
Intent intent = new Intent(getApplicationContext(), TeacherSideQuestion2.class);
Bundle bundle = new Bundle();
bundle.putInt("score", finalScore);
intent.putExtras(bundle);
startActivity(intent);
In Second Activity:
Declare int i as a global variable.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i = getIntent().getIntExtra("", 0);
}
and on your button.setOnClick {
if (i >= 1) {
Intent intent = new Intent(TeacherSideQuestion2.this,
CongratsEnglish.class);
startActivity(intent);
} else {
Intent intent1 = new Intent(TeacherSideQuestion2.this,
FailedEnglish.class);
startActivity(intent1);
}
}
Upvotes: 0
Reputation: 1316
Check your manifest file and see if you have entries for all the activities. Check your min sdk and target sdk in manifest file and android properties.
Upvotes: 0
Reputation: 2086
Try this :
Intent intent = new Intent(MyActivity.this,YourClass.class);
intent.putInt("score",finalScore);
startActivity(intent);
and in receiving class receive it like this in your onCreate()
int defValue = -999;
Intent intent = getIntent();
int score = intent.getIntExtra("score",defValue); // defalut value can be anything if score not get recieved
Upvotes: 3