2016-06-07 22 views
1

Ich verwende einen Dialog, um eine Benutzereingabe für einen Dateinamen zu erhalten. Eine Schaltfläche in der Hauptaktivität startet den Dialog, an dem ich möchte, dass die positive Schaltfläche die Eingabe speichert und dann die nächste Aktivität startet und die Eingabe an die nächste Aktivität weitergibt. Immer wenn ich den positiven Knopf im Dialog drücke, stürzt die App ab. Ich habe versucht, die Eingabe innerhalb und außerhalb des onClick() innerhalb des Dialogs zu speichern sowie zu versuchen, die Aktivität separat außerhalb des Dialogs zu starten. Ich bin neu in Android, so viel Code wird aus den API-Handbüchern entnommen. Warum stürzt die App weiter ab und wie starte ich die nächste Aktivität?Aktivität starten und Eingabe über DialogFragment übergeben

public class initializeTestFragment extends DialogFragment { 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     // Get the layout inflater 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     // Inflate and set the layout for the dialog 
     // Pass null as the parent view because its going in the dialog layout 
     builder.setView(inflater.inflate(R.layout.activity_initialize_test, null)); 
     builder.setTitle(R.string.start_test); 
       // Add action buttons 
     builder.setPositiveButton(R.string.start_test, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         EditText editText = (EditText) findViewById(R.id.filename); 
         String filename = editText.getText().toString(); 
         Context context = getContext(); 
         Intent intent = new Intent(context, InitializeTest.class); 
         intent.putExtra(FILENAME, filename); 
         context.startActivity(intent); 
        } 
       }); 
     builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.dismiss(); 
        } 
       }); 
     return builder.create(); 
    } 
} 

public void startTest(View view) { 
    DialogFragment newFragment = new initializeTestFragment(); 
    android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
    newFragment.show(fragmentTransaction, "initialize"); 
} 

EDIT: Wie gewünscht, hier wird die volle logcat von läuft die App (Ich habe einen OnePlus One auf Android 6.0.1, Cyanogen OS 13.0-ZNH0EAS2JK)

06-07 13:05:23.705 11193-11193/com.tiem.rstemme.layoutpractice I/art: Late-enabling -Xcheck:jni 
06-07 13:05:23.764 11193-11193/com.tiem.rstemme.layoutpractice W/System: ClassLoader referenced unknown path: /data/app/com.tiem.rstemme.layoutpractice-2/lib/arm 
06-07 13:05:26.165 11193-11193/com.tiem.rstemme.layoutpractice W/System: ClassLoader referenced unknown path: /data/app/com.tiem.rstemme.layoutpractice-2/lib/arm 
06-07 13:05:26.181 11193-11193/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package common 
06-07 13:05:26.183 11193-11193/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package com.cyngn.hexo 
06-07 13:05:26.186 11193-11193/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package com.cyngn.hexo 
06-07 13:05:26.223 11193-11193/com.tiem.rstemme.layoutpractice I/FirebaseInitProvider: FirebaseApp initialization unsuccessful 
06-07 13:05:26.410 11193-11203/com.tiem.rstemme.layoutpractice W/art: Suspending all threads took: 6.204ms 
06-07 13:05:26.539 11193-11193/com.tiem.rstemme.layoutpractice W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
06-07 13:05:26.631 11193-11193/com.tiem.rstemme.layoutpractice W/Activity: Can reqeust only one set of permissions at a time 
06-07 13:05:26.658 11193-11484/com.tiem.rstemme.layoutpractice D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
06-07 13:05:26.721 11193-11484/com.tiem.rstemme.layoutpractice I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU (Ia6c73e7530) 
                     OpenGL ES Shader Compiler Version: E031.29.00.00 
                     Build Date: 12/04/15 Fri 
                     Local Branch: mybranch17080070 
                     Remote Branch: quic/LA.BF.1.1.3_rb1.5 
                     Local Patches: NONE 
                     Reconstruct Branch: NOTHING 
06-07 13:05:26.725 11193-11484/com.tiem.rstemme.layoutpractice I/OpenGLRenderer: Initialized EGL, version 1.4 
06-07 13:05:31.422 11193-11193/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
06-07 13:05:31.422 11193-11193/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
06-07 13:05:32.726 11193-11193/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
06-07 13:05:32.726 11193-11193/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
06-07 13:06:09.663 12461-12461/com.tiem.rstemme.layoutpractice W/System: ClassLoader referenced unknown path: /data/app/com.tiem.rstemme.layoutpractice-2/lib/arm 
06-07 13:06:10.314 12461-12461/com.tiem.rstemme.layoutpractice W/System: ClassLoader referenced unknown path: /data/app/com.tiem.rstemme.layoutpractice-2/lib/arm 
06-07 13:06:10.319 12461-12461/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package common 
06-07 13:06:10.321 12461-12461/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package com.cyngn.hexo 
06-07 13:06:10.323 12461-12461/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package com.cyngn.hexo 
06-07 13:06:10.353 12461-12461/com.tiem.rstemme.layoutpractice I/FirebaseInitProvider: FirebaseApp initialization unsuccessful 
06-07 13:06:10.602 12461-12461/com.tiem.rstemme.layoutpractice W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
06-07 13:06:10.687 12461-12461/com.tiem.rstemme.layoutpractice W/Activity: Can reqeust only one set of permissions at a time 
06-07 13:06:10.710 12461-12512/com.tiem.rstemme.layoutpractice D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
06-07 13:06:10.774 12461-12512/com.tiem.rstemme.layoutpractice I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU (Ia6c73e7530) 
                     OpenGL ES Shader Compiler Version: E031.29.00.00 
                     Build Date: 12/04/15 Fri 
                     Local Branch: mybranch17080070 
                     Remote Branch: quic/LA.BF.1.1.3_rb1.5 
                     Local Patches: NONE 
                     Reconstruct Branch: NOTHING 
06-07 13:06:10.775 12461-12512/com.tiem.rstemme.layoutpractice I/OpenGLRenderer: Initialized EGL, version 1.4 
06-07 13:06:12.644 12461-12461/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
06-07 13:06:12.644 12461-12461/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
06-07 13:06:13.308 12461-12461/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
06-07 13:06:13.308 12461-12461/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
06-07 13:06:15.655 12461-12461/com.tiem.rstemme.layoutpractice D/AndroidRuntime: Shutting down VM 


                      --------- beginning of crash 
06-07 13:06:15.655 12461-12461/com.tiem.rstemme.layoutpractice E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.tiem.rstemme.layoutpractice, PID: 12461 
                      java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference 
                       at com.tiem.rstemme.layoutpractice.MainActivity$initializeTestFragment$1.onClick(MainActivity.java:93) 
                       at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5466) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
06-07 13:06:18.119 12461-12461/com.tiem.rstemme.layoutpractice I/Process: Sending signal. PID: 12461 SIG: 9 
+0

Welche Ausnahme erhalten Sie? – Codes

+0

Ich denke, die Zeile 'context.startActivity (intent);' stürzt ab, bitte drucken Sie StackTrace – Ninja

+1

Achten Sie darauf, die Frage mit der ** gesamten ** Logcat der Ausnahme zu bearbeiten, die Sie erhalten. – Vucko

Antwort

2

I‘ Ich nehme an, dass der EditText filename in Ihrem aufgeblasenen Layout ist. Sie müssen es über dieses überhöhte Layout referenzieren.

View view = inflater.inflate(R.layout.activity_initialize_test, null); 
final EditText editText = (EditText) view.findViewById(R.id.filename); 

builder.setPositiveButton(R.string.start_test, new DialogInterface.OnClickListener() {    
    @Override 
    public void onClick(DialogInterface dialog, int id) { 
     String filename = editText.getText().toString(); 
     Context context = getContext(); 
     Intent intent = new Intent(context, InitializeTest.class); 
     intent.putExtra(FILENAME, filename); 
     context.startActivity(intent); 
    } 
}); 

Sie sind wahrscheinlich ein NullPointerException aufgrund findViewById Suche durch Ihre Tätigkeit Layout statt Ihrem Dialog-Layout bekommen und nicht eine entsprechende Ansicht mit dieser ID zu finden.

+0

Ich war in der Lage, es zur nächsten Aktivität mit diesem Code zu machen. Danke für Ihre Hilfe! – rstemme

Verwandte Themen