简体   繁体   English

尝试在android中传递参数

[英]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 . 所以我有两个文件( MainActivity.javaHomeFragment.java ),我试图将一个名为displayPromptForEnablingGPSpublic static voidHomeFragmentMainActivity Here is the code 这是代码

HomeFragment.java (the code I'm trying to pass) HomeFragment.java (我尝试传递的代码)

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) MainActivity.java (我怎么称呼它)

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: 您在MainActivity中的方法应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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