Je (pense) avoir configuré mon code de telle sorte que lorsqu'un clic sur une image, il ouvre une boîte de dialogue contenant des informations. Cependant, lorsque je clique sur l'image, au lieu de l'ouverture de la boîte de dialogue, cela indique que MY_APP s'est arrêté
sur l'écran de l'appareil.
J'utilise Android Studio pour cela et je ne reçois aucun compilateur erreurs là-bas lorsque je construit
mon projet. Cependant, cela ne fonctionne pas sur l'appareil.
Voici mon code:
UserDataInputActivity.java
2019-01-04 14:24:34.741 8385-8385/com.example.owner.introductoryapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.owner.introductoryapplication, PID: 8385 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:6312) at android.view.View$PerformClick.run(View.java:24802) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:169) at android.app.ActivityThread.main(ActivityThread.java:6521) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:6312)Â at android.view.View$PerformClick.run(View.java:24802)Â at android.os.Handler.handleCallback(Handler.java:790)Â at android.os.Handler.dispatchMessage(Handler.java:99)Â at android.os.Looper.loop(Looper.java:169)Â at android.app.ActivityThread.main(ActivityThread.java:6521)Â at java.lang.reflect.Method.invoke(Native Method)Â at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)Â at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)Â Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.owner.introductoryapplication/com.example.owner.introductoryapplication.GaitInformation}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1940) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1618) at android.app.Activity.startActivityForResult(Activity.java:4529) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767) at android.app.Activity.startActivityForResult(Activity.java:4487) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754) at android.app.Activity.startActivity(Activity.java:4848) at android.app.Activity.startActivity(Activity.java:4816) at com.example.owner.introductoryapplication.UserDataInputActivity.onGenericMenuClick(UserDataInputActivity.java:52) at java.lang.reflect.Method.invoke(Native Method)Â at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)Â at android.view.View.performClick(View.java:6312)Â at android.view.View$PerformClick.run(View.java:24802)Â at android.os.Handler.handleCallback(Handler.java:790)Â at android.os.Handler.dispatchMessage(Handler.java:99)Â at android.os.Looper.loop(Looper.java:169)Â at android.app.ActivityThread.main(ActivityThread.java:6521)Â at java.lang.reflect.Method.invoke(Native Method)Â at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)Â at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)Â 2019-01-04 14:24:35.040 2367-2389/? E/libnav: CablComputeBacklightLevel(): UpdateType = DifferentSceneUpdate 2019-01-04 14:24:35.251 2638-2670/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.example.owner.introductoryapplication/com.example.owner.introductoryapplication.PastDiagnosticResult/1 2019-01-04 14:24:40.474 2367-2389/? E/libnav: CablComputeBacklightLevel(): UpdateType = DifferentSceneUpdate
GaitInformation.java
package com.example.owner.introductoryapplication; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.DialogFragment; public class GaitInformation extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.GaitInformationContent) .setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //GO BACK TO THE USER DATA INPUT PAGE - HOW? } }); // Create the AlertDialog object and return it return builder.create(); } }
RAPPORT D'ÉCRASEMENT LOGCAT
package com.example.owner.introductoryapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.view.View; public class UserDataInputActivity extends AppCompatActivity { String[] genderOptions = {"Male", "Female", "Other"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user_data_input); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, genderOptions); //Find TextView control AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.GenderPromptValue); //Set the number of characters the user must type before the drop down list is shown acTextView.setThreshold(1); //Set the adapter acTextView.setAdapter(adapter); } public void onGenericMenuClick(View v) { Intent intent; if(v.getId() == R.id.pasttests) { intent = new Intent(this, PastDiagnosticResult.class); } else if (v.getId() == R.id.currenttest) { intent = new Intent(this, CurrentDiagnosticResultActivity.class); } else if (v.getId() == R.id.myinfo) { intent = new Intent(this, UserDataInputActivity.class); } else //if(v.getId() == R.id.gaitInfoButton) { intent = new Intent(this, GaitInformation.class); } startActivity(intent); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); } }
Je m'attends à ce que la boîte de dialogue apparaisse, mais - à la place - cela indique que mon application a cessé de fonctionner.
3 Réponses :
Dans votre classe GaitInfomation en ligne nouveau AlertDialog.Builder (getActivity ()); Vous devez passer un contexte valide que je soupçonne est nul en regardant le code.
Mise à jour: au lieu de lancer une nouvelle activité
else //if(v.getId() == R.id.gaitInfoButton) { AlertDialog.Builder(this) setMessage(R.string.GaitInformationContent) .setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //GO BACK TO THE USER DATA INPUT PAGE - HOW? return; } }); .create() .show() }
Comment passer le "contexte valide"? Pouvez-vous fournir un extrait de code pour que je comprenne ce que vous voulez dire? Merci.
Knivasara Veuillez consulter imgur.com/7wploeS - il montre à quoi ressemble votre code sur mon IDE. Quant au moment où je l'exécute, j'obtiens error:
, error: ';' attendu
, et erreur: début d'expression illégal
à de nombreux endroits.
Remplacez ce
FragmentManager manager = getSupportFragmentManager(); GaitInformation dialog = new GaitInformation(); dialog.show(manager, "dialog");
par ce
intent = new Intent(this, GaitInformation.class);
Cela ne semble pas fonctionner. Le résultat de l'erreur est: error: l'intention de la variable n'a peut-être pas été initialisée
. Cela a du sens car la dernière condition else
n'initialise pas Intent
, contrairement à l'autre condition ci-dessus.
Essayez ceci pour démarrer l'intent Intent intent = null;
Ça marche; Merci! Pouvez-vous me dire ce qui se passe dans le code ci-dessus?
Dans votre bloc if else, le comportement par défaut n'était pas d'initialiser l'intention. Juste pour éviter cette erreur, nous avons passé null. Il a donc été initialisé après cela, mais seule la valeur est nulle.
Vous ne pouvez pas ouvrir un DialogFragment comme celui-ci. Veuillez essayer ceci.
package com.example.owner.introductoryapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.view.View; public class UserDataInputActivity extends AppCompatActivity { String[] genderOptions = {"Male", "Female", "Other"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user_data_input); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, genderOptions); //Find TextView control AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.GenderPromptValue); //Set the number of characters the user must type before the drop down list is shown acTextView.setThreshold(1); //Set the adapter acTextView.setAdapter(adapter); } public void onGenericMenuClick(View v) { Intent intent = null; if(v.getId() == R.id.pasttests) { intent = new Intent(this, PastDiagnosticResult.class); } else if (v.getId() == R.id.currenttest) { intent = new Intent(this, CurrentDiagnosticResultActivity.class); } else if (v.getId() == R.id.myinfo) { intent = new Intent(this, UserDataInputActivity.class); } else //if(v.getId() == R.id.gaitInfoButton) { AlertDialog.Builder builder = new AlertDialog.Builder(UserDataInputActivity.this); builder.setMessage(R.string.GaitInformationContent) .setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //GO BACK TO THE USER DATA INPUT PAGE - HOW? } }); // Create the AlertDialog object and return it builder.create().show(); } if (intent != null) { startActivity(intent); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); } } }
Al Maktadir J'obtiens la même sortie d'erreur: error: la variable intent n'a peut-être pas été initialisée
. Comment puis-je réparer cela? Une fois que l'utilisateur ferme la Dialog
, il doit revenir à l'écran de UserDataInputActivity
...
Ça marche; Merci. Pouvez-vous revenir sur ce que vous avez changé?
Initialisez simplement l'objet d'intention avec null.
Ok gotcha. @Zoarder Al Muktadir Pourriez-vous aussi jeter un oeil à ces questions, j'ai eu beaucoup de mal aussi. Je crois que c'est une solution simple ... stackoverflow.com/questions/54036101/... et stackoverflow.com/questions/53983231/… . Enfin, un vote positif serait bien! :)
Ok, laissez-moi vérifier ... Merci.
Un coup de cœur se produit au moment de l'exécution, lorsque vous cliquez sur l'image. pouvez-vous s'il vous plaît poster le rapport d'écrasement? vous le trouverez dans logcat.
@ZoarderAlMuktadir J'ai spécifié le rapport d'écrasement!