0

I (think) I have configured my code such that when an Image is clicked, then it opens a Dialog with some information. However, when I click the image, instead of the Dialog opening, it says that MY_APP has stopped on the device screen.

I am using Android studio for this and I don't get any compiler errors there when I build my project. However, it doesn't work on the device.

Here's my code: UserDataInputActivity.java

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);
    }
}

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();
    }
}

LOGCAT CRUSH REPORT

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

I expect the dialog to pop-up, but - instead - it says my app has stopped working.

Shounak Ray
  • 163
  • 1
  • 11

3 Answers3

1

Change this

intent = new Intent(this, GaitInformation.class);

into this

FragmentManager manager = getSupportFragmentManager();
GaitInformation dialog = new GaitInformation();
dialog.show(manager, "dialog");
Tahir Ferli
  • 636
  • 4
  • 16
  • That doesn't seem to work. Error output is: `error: variable intent might not have been initialized`. It makes sense because the last `else` condition doesn't initialize `Intent`, as opposed to the other conditional above. – Shounak Ray Jan 04 '19 at 21:54
  • Try this to start the intent Intent intent = null; – Tahir Ferli Jan 04 '19 at 21:55
  • It works; thanks! Can you tell me what's going on in the above code? – Shounak Ray Jan 04 '19 at 22:12
  • 1
    In your if else block default behaviour was not initializing the intent. Just to prevent that error we passed null. So it was initialized after that but just the value is null. – Tahir Ferli Jan 04 '19 at 22:20
1

You can't open a DialogFragment like this. Please try this.

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 I am getting the same error output: `error: variable intent might not have been initialized`. How can I fix this? After the user closes the `Dialog`, it should return to `UserDataInputActivity`'s screen... – Shounak Ray Jan 04 '19 at 21:57
  • It works; thank you. Can you go over what you changed? – Shounak Ray Jan 04 '19 at 22:10
  • Just initialize the intent object with null. – Zoarder Al Muktadir Jan 04 '19 at 22:12
  • Ok gotcha. @Zoarder Al Muktadir Could you also take a look at these questions, I have been having a lot of trouble as well. I believe its a simple solution...https://stackoverflow.com/questions/54036101/how-to-resolve-java-lang-runtimeexception-stub-error-in-java-file and https://stackoverflow.com/questions/53983231/how-to-use-pre-trained-model-file-for-predictions-in-android-studio. Lastly, an upvote would be nice! : ) – Shounak Ray Jan 04 '19 at 22:14
  • Ok, Let me Check... Thank You. – Zoarder Al Muktadir Jan 04 '19 at 22:16
0

In your class GaitInfomation on line new AlertDialog.Builder(getActivity()); You have to pass valid context which I suspect is null looking at the code.

Update: Instead of launching a new activity

      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()
           }
Anand Khinvasara
  • 618
  • 4
  • 17
  • How do I pass the "valid context"? Can you provide a code snippet so I understand what you mean? Thanks. – Shounak Ray Jan 04 '19 at 21:28
  • Knivasara Please see https://imgur.com/7wploeS - it shows what my your code looks like on my IDE. As for when I run it, I get `error: expected`, `error: ';' expected`, and `error: illegal start of expression` in many places. – Shounak Ray Jan 04 '19 at 22:02