Sagen wir, dass ich 3 Aktivitäten habe, First ist MainActivity, Second ist PopUp, Third ist SearchCategory.StartActivityforResult stürzt ab
Was ich versuche zu tun ist, dass ich diese Aktivität starten werde, PopUp, die eine Schaltfläche hat. Wenn ich jetzt auf diese Schaltfläche klicke, möchte ich, dass sie als String gespeichert und als Intent übergeben wird, damit ich sie für die MainActivity verwenden kann, um sie für etwas zu vergleichen.
Dies funktionierte für meine SearchCategory-Klasse.
Das Problem, das ich bekomme, ist, dass es abstürzt, wenn ich auf die Schaltfläche in der PopUp-Klasse klicke.
Die Codes für onActivityResult meine MainActivity suchen:
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");
}
...
}
Die Codes der Taste meiner SearchCategory Klasse:
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();
}
Die Codes meiner PopUp Klasse:
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();
}
}
}
Diese ist ein Fehler, den ich bekomme, nachdem ich auf den Knopf geklickt habe:
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)
EDIT: Jetzt stürzt beide ab. Die SearchCategory hat vorher gut funktioniert! :(
Was ist Zeile 131 in MainActivity? Ist es die 'if (start.equals (" 1 ")) Zeile? Wenn das der Fall ist, wo wird der Start ursprünglich in MainActivity zugewiesen? Ich sehe es nur in Ihrer PopUp-Aktivität zugeordnet – shiv