简体   繁体   中英

Trying to pass parameter in android

So I have two files ( MainActivity.java and HomeFragment.java ) and I am trying to pass a public static void called displayPromptForEnablingGPS from HomeFragment to MainActivity . Here is the code

HomeFragment.java (the code I'm trying to pass)

public static void displayPromptForEnablingGPS(
        final Activity activity)
{
    final AlertDialog.Builder builder =
            new AlertDialog.Builder(activity);
    final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
    final String message = "Enable either GPS or any other location"
            + " service to find current location.  Click OK to go to"
            + " location services settings to let you do so.";


    builder.setMessage(message)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface d, int id) {
                            activity.startActivity(new Intent(action));
                            d.dismiss();
                        }
                    })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface d, int id) {
                            d.cancel();
                        }
                    });
    builder.create().show();
}

MainActivity.java (how I'm trying to call it)

public void showMainView() {
   HomeFragment.displayPromptForEnablingGPS();
}

But I get an error saying

"HomeFragment.displayPromptForEnablingGPS();" is invalid

The method you want to call requires a parameter but you don't pass it.

public static void displayPromptForEnablingGPS(
    final Activity activity)

Your method in MainActivity should look like this:

public void showMainView() {
   HomeFragment.displayPromptForEnablingGPS(this);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM