我正在尝试将值K从活动A传递到B.当单击活动B中的保存button
时,它会再次将值k传递给活动A.但如果我想返回A,应用程序崩溃了。
活动A中的一些代码段
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
intent.putExtra("k",k);
startActivityForResult(intent, 0);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
intent.putExtra("k", k);
startActivityForResult(intent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
int button = data.getIntExtra("k1", 0);
if (button == 1) {
switch (requestCode) {
case 0:
String result = data.getStringExtra("text");
String b = data.getStringExtra("a");
as=Long.parseLong(result);
c.setText(" " + b + "------" + "RM " + result);
break;
case 1:
String result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("a");
as1=Long.parseLong(result1);
c.setText(" " + b1 + "------" + "RM " + result1);
break;
}
}
else if(button==2)
{
switch (requestCode) {
case 0:
String result = data.getStringExtra("text");
String b = data.getStringExtra("a");
bs=Long.parseLong(result);
d.setText(" " + b + "------" + "RM " + result);
break;
case 1:
String result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("a");
bs1=Long.parseLong(result1);
d.setText(" " + b1 + "------" + "RM " + result1);
break;
}
}
活动B
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
txt= (EditText)findViewById(R.id.editText36);
Button b=(Button)findViewById(R.id.button17);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent returnIntent = new Intent();
a="Project";
text = txt.getText().toString();
returnIntent.putExtra("text", text);
returnIntent.putExtra("a", a);
final int k1 = getIntent().getExtras().getInt("k");
returnIntent.putExtra("k1", k1);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
}
@Override
public void onBackPressed()
{
Intent intent = new Intent(Project1.this, Claims.class);
startActivity(intent);
finish();
}
LogCat错误
10-28 15:55:26.612 3641-3641/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 3641
java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=2, result=0, data=null} to activity {com.example.project.project/com.example.project.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
最新错误
10-28 16:54:56.150 6381-6381/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 6381
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.project.project/com.example.project.project.Claims}: java.lang.ClassCastException: com.example.project.project.Claims cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
我确实喜欢@we是borg建议的并在mainfest.xml中声明活动A(claims.java),但是得到上面的错误
谢谢大家......我确实喜欢这个并解决了问题:)
活动A
声明public static final int CAMERA_REQUEST_CODE = 6;
添加else if(requestCode==CAMERA_REQUEST_CODE) {}after else if(button==5){}
活动B
使用
@Override
public void onBackPressed()
{
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
而不是@Override
public void onBackPressed()
{
Intent intent = new Intent(Project1.this, Claims.class);
startActivity(intent);
finish();
}
答案 0 :(得分:5)
按后退按钮时,默认结果代码为RESULT_CANCEL,返回意图为空。
您需要在 onActivityResult 中检查它,或在活动B中覆盖方法 onBackPressed 并设置必要的结果值。
编辑:
以下列方式(活动B)
覆盖 onBackPressed@Override
public void onBackPressed() {
Intent returnIntent = new Intent();
a="Project";
text = txt.getText().toString();
returnIntent.putExtra("text", text);
returnIntent.putExtra("a", a);
final int k1 = getIntent().getExtras().getInt("k");
returnIntent.putExtra("k1", k1);
setResult(Activity.RESULT_OK, returnIntent);
super.onBackPressed();
}
在这种情况下,您将发回必要的结果值。
如果您不想按下后退按钮发送任何值,您可以在 onActivityResult
中查看结果代码public void onActivityResult(int requestCode, int resultCode, Intent data) {
// int button = data.getIntExtra("k1", 0); // data could be null. and here you are getting NullPointerException
// if (button == 1) {
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("text");
String b = data.getStringExtra("a");
as=Long.parseLong(result);
c.setText(" " + b + "------" + "RM " + result);
} else if (resultCode == RESULT_CANCEL) {
// TODO other functionality - when you do not override onBackPressed in activity B
}
break;
...
}
}
答案 1 :(得分:2)
好的,从http://developer.android.com/training/basics/intents/result.html的官方文档中无耻地复制粘贴:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
确保代码中包含if (resultCode == RESULT_OK)
行。如果(resultCode == RESULT_CANCELED)
,则无法获得任何结果且Intent
为null
。
当您按下"返回"您设备上的按钮正常结果是取消当前活动,因此在这种情况下,调用活动获得resultCode
RESULT_CANCELED
而不是您通过finish()
明确返回时设置的data = np.arange(9).reshape(3,3)
IDs = np.array([
["A", "A", "B"],
["A", "B", "C"],
["B", "C", "C"]
])
答案 2 :(得分:1)
在班级中添加此功能:
@Override
public void onBackPressed() {
Intent intent = new Intent(CurrentClass.this, DestinationActivity.class);
startActivity(intent);
finish();
}
我希望这就是您所寻找的,如果没有,我会删除答案,只需发表评论。
答案 3 :(得分:-1)
我认为'空对象引用'是因为getActivity()
而来的。
如果它不是使用上下文,请检查getActivity()
是否未返回null。
或者创建一个单例对象。