我已在我的活动中试过这段代码..
在我的第一个活动中,这是我传递数据的代码:
Intent intent = new Intent(getApplicationContext(), TeacherSideQuestion2.class);
Bundle bundle = new Bundle();
bundle.putInt("score", finalScore);
intent.putExtras(bundle);
startActivityForResult(intent,0);
在我的第二个活动中,这是检索我的数据的代码。
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);
}
}
}
但是当我尝试单击onClick事件时,我的应用程序将崩溃。
任何人都可以解决这个问题吗?
感谢您的回答。
这是LogCat说的:
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)
答案 0 :(得分:3)
试试这个:
Intent intent = new Intent(MyActivity.this,YourClass.class);
intent.putInt("score",finalScore);
startActivity(intent);
并且在接收类中在onCreate()
中接收它int defValue = -999;
Intent intent = getIntent();
int score = intent.getIntExtra("score",defValue); // defalut value can be anything if score not get recieved
答案 1 :(得分:0)
在第一个活动中,这是我传递数据的代码:
Intent intent = new Intent(getApplicationContext(), TeacherSideQuestion2.class);
Bundle bundle = new Bundle();
bundle.putInt("score", finalScore);
intent.putExtras(bundle);
startActivity(intent);
在第二项活动:
中将int i声明为全局变量。
@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);
}
}
答案 2 :(得分:0)
检查您的清单文件,看看您是否有所有活动的条目。检查你的min sdk和目标sdk清单文件和android属性。
答案 3 :(得分:0)
将startActivityForResult(intent,0);
更改为startActivity(intent);
,因为您无需为了将结果传递给其他活动而启动结果。
我不确定这是否有问题,但您可能需要将getApplicationContext()
更改为YourActivityName.this
。
最后,您不需要创建一个包来将值发送到另一个活动。你可以这样做:
Intent intent = new Intent(YourActivityName.this, TeacherSideQuestion2.class);
intent.putExtra("score",finalscore);
startActivity(intent);