2017-08-17 7 views
-2

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! :(

+0

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

Antwort

0

Sieht aus wie ich die Antwort auf mein Problem habe, aber ich weiß wirklich nicht, warum. Gut, dass ich meinen ganzen Code und füge ihn immer kopieren, bevor etwas Neues zu versuchen.

Ich denke, etwas falsch war mit meinem Codes die ActivityforResult in Berufung, weil das ist, wo ich viel verändert.

Sorry, dass ich nicht in der Lage war, diese beiden kritischen Codes zu schreiben. ich kann aus irgendeinem Grund nicht einmal wiederholen.

Aber ich glaube, es war so etwas wie dies:

Die Codes für die PopUp Aktivität:

Intent intent = new Intent(MainActivity.this, PopUp.class); 
     intent.putExtra("route", tag); 
     startActivityForResult(intent, 1); 

und die Codes für die SearchCategoryActivity:

Intent i = new Intent(MainActivity.this, SearchCategory.class); 
      startActivityForResult(i, 0); 

Und die Lösung, die ich gemacht wurde änderte ich den onClick für die PopUp Aktivität Aufruf an:

Intent i = new Intent(MainActivity.this, PopUp.class); 
      i.putExtra("route", tag); 
      startActivityForResult(i, 0); 

Im Grunde kopierte man den Aufruf von SearchCategory und fügte diese Zeile hinzu:

i.putExtra("route", tag);