Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在函数中作为参数传递片段_Android_Android Fragments_Android Alertdialog - Fatal编程技术网

Android 在函数中作为参数传递片段

Android 在函数中作为参数传递片段,android,android-fragments,android-alertdialog,Android,Android Fragments,Android Alertdialog,我在MainActivity中有一个“先询问密码”函数,当用户从一个片段切换到另一个片段时,我想调用该函数。我想知道我的函数是否可以接受一个片段作为参数,然后将其传递到该函数中 我已经尝试过向函数传递字符串并使用findFragmentByTag,但它返回null 这就是我的代码的样子 private returnType?? logIn(fragmentparameterhere???????){ //show alert dialog editText and ask for

我在MainActivity中有一个“先询问密码”函数,当用户从一个片段切换到另一个片段时,我想调用该函数。我想知道我的函数是否可以接受一个片段作为参数,然后将其传递到该函数中

我已经尝试过向函数传递字符串并使用findFragmentByTag,但它返回null

这就是我的代码的样子

    private returnType?? logIn(fragmentparameterhere???????){

   //show alert dialog editText and ask for password

    alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String value = String.valueOf(input.getText());
            if (value.equals("123456")){
                dialog.dismiss();
                Toast.makeText(getApplicationContext(), "Welcome!", Toast.LENGTH_SHORT).show();

                Fragment fragment = fragmentparameterhere??;
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container,fragment)
                        .commit();
            }
            else {
                x[0]++;

                 if(x[0] == 5){

                      //password entered too many times
                   }
                else {


                    //toast wrong password
                }

            }

        }


    });

    alert.show();

}
我想知道这是否可能

登录名(某些片段的名称)

顺便说一句,我使用了android Nagiviation抽屉的碎片

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

         if (id == R.id.nav_fragment1) {
         login(fragmentNameOfThis);

    }
   else if (id == R.id.nav_fragment2) {

           login(fragmentNameOfThis);

         }
    }
试试这个:


    // Pass the string tag of the target fragment
    private void changeFragment(final String fragTag) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragTag);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment)
                .commit();

    }

    // Pass the instance of the targeted fragment
    private void changeFragment(final Fragment targetFragment) {
//        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragTag);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, targetFragment)
                .commit();
    }

    // Pass the instance of the target fragment
    private void logIn(final Fragment targetFragment) {

        //show alert dialog editText and ask for password
        alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String value = String.valueOf(input.getText());
                if (value.equals("123456")) {
                    dialog.dismiss();
                    Toast.makeText(getApplicationContext(), "Welcome!", Toast.LENGTH_SHORT).show();

                    changeFragment(targetFragment);

                } else {
                    x[0]++;

                    if (x[0] == 5) {

                        //password entered too many times
                    } else {


                        //toast wrong password
                    }

                }

            }


        });

        alert.show();
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        // Use switch for better performance
        switch (id) {
            case R.id.nav_fragment1:
                // Passing the instance of FragmentName_1
                logIn(FragmentName_1());
                break;

            case R.id.nav_fragment2:
                // Passing the instance of FragmentName_2
                logIn(FragmentName_2());
                break;

            default:
                break;
        }
    }

    // Pass the string tag of the target fragment
    private void changeFragment(final String fragTag) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragTag);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment)
                .commit();

    }

    // Pass the instance of the targeted fragment
    private void changeFragment(final Fragment targetFragment) {
//        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragTag);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, targetFragment)
                .commit();
    }

    // Pass the instance of the target fragment
    private void logIn(final Fragment targetFragment) {

        //show alert dialog editText and ask for password
        alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String value = String.valueOf(input.getText());
                if (value.equals("123456")) {
                    dialog.dismiss();
                    Toast.makeText(getApplicationContext(), "Welcome!", Toast.LENGTH_SHORT).show();

                    changeFragment(targetFragment);

                } else {
                    x[0]++;

                    if (x[0] == 5) {

                        //password entered too many times
                    } else {


                        //toast wrong password
                    }

                }

            }


        });

        alert.show();
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        // Use switch for better performance
        switch (id) {
            case R.id.nav_fragment1:
                // Passing the instance of FragmentName_1
                logIn(FragmentName_1());
                break;

            case R.id.nav_fragment2:
                // Passing the instance of FragmentName_2
                logIn(FragmentName_2());
                break;

            default:
                break;
        }
    }