假设我有3个活动,第一个是MainActivity,第二个是PopUp,第三个是SearchCategory。
我想要做的是我将开始这个活动,PopUp有一个按钮。现在,当我单击此按钮时,我希望它将其存储为字符串并将其作为Intent传递,以便我可以将它用于MainActivity来比较它。
这适用于我的SearchCategory类。
我遇到的问题是当我点击PopUp类中的按钮时它会崩溃。
我的MainActivity的代码是onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle searchCategory = data.getExtras();
if (start.equals("1")){
route = searchCategory.getString("route");
}else {
start = searchCategory.getString("start");
route = searchCategory.getString("route");
}
...
}
我的SearchCategory类按钮的代码:
public void search(View view) {
category = (MaterialBetterSpinner)findViewById(R.id.spinner_category);
category_string = category.getText().toString();
route = (MaterialBetterSpinner)findViewById(R.id.spinner_routes);
route_string = route.getText().toString();
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("start", category_string);
bundle.putString("route", route_string);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
我的PopUp类的代码:
package rjj.manilapuvtravelcompanion;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.TextView;
import org.w3c.dom.Text;
/**
* Created by Julian on 8/5/2017.
*/
public class PopUp extends AppCompatActivity {
String route, start = "1";
TextView textView2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_layout);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.5));
route = getIntent().getExtras().getString("route");
//Checks if I retrieved the intent correctly
if (route.equals("Sample Route")){
textView2 = (TextView)findViewById(R.id.textView2);
textView2.setText("Nice");
}
}
public void getRoute(View view) {
if (route.equals("Sample Route")){
Intent intent = new Intent();
Bundle bundle = new Bundle();
intent.putExtra("start", start);
intent.putExtra("route", route);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
}
}
这是我点击按钮后出现的错误:
08-18 00:08:47.221 2807-2813/? E/jdwp: Failed writing handshake bytes: Broken pipe (-1 of 14)
08-18 00:08:50.341 2807-2807/rjj.manilapuvtravelcompanion E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
08-18 00:08:50.821 2807-2807/rjj.manilapuvtravelcompanion E/dalvikvm: Could not find class 'com.google.android.chimera.Activity', referenced from method jx.b
08-18 00:08:58.441 2807-2807/rjj.manilapuvtravelcompanion E/AndroidRuntime: FATAL EXCEPTION: main
Process: rjj.manilapuvtravelcompanion, PID: 2807
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {rjj.manilapuvtravelcompanion/rjj.manilapuvtravelcompanion.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3365)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at rjj.manilapuvtravelcompanion.MainActivity.onActivityResult(MainActivity.java:131)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
编辑:现在两人都崩溃了。 SearchCategory之前工作正常! :(
答案 0 :(得分:0)
看起来我得到了问题的答案,但我真的不知道为什么。好的事情我总是在尝试新的东西之前复制粘贴我的整个代码。
我认为在调用ActivityforResult时我的代码出了问题,因为那是我改变了很多的地方。
很抱歉,我无法发布这两个关键代码。我出于某种原因甚至无法重做。
但我觉得它是这样的:
PopUp活动的代码:
Intent intent = new Intent(MainActivity.this, PopUp.class);
intent.putExtra("route", tag);
startActivityForResult(intent, 1);
以及SearchCategoryActivity的代码:
Intent i = new Intent(MainActivity.this, SearchCategory.class);
startActivityForResult(i, 0);
我所做的解决方案是将onClick更改为调用PopUp Activity为:
Intent i = new Intent(MainActivity.this, PopUp.class);
i.putExtra("route", tag);
startActivityForResult(i, 0);
基本上是在调用SearchCategory时复制了一个并添加了这一行:
i.putExtra("route", tag);