0

So here is my problem, I keep on getting red underline in my loginFragment.show(getFragmentManager(), "login"); why does it have an underline under the parameters. When I use getSupportFragmentManager and not use AppCompatActivity and use Activity instead, yes it does not have an error in the program itself but as soon as I click the button to show the dialog box, it crashes.Here is my entire code.

import android.app.Dialog;    
import android.content.Context;    
import android.content.DialogInterface;    
import android.support.v4.app.DialogFragment;    
import android.support.v7.app.AlertDialog;    
import android.support.v7.app.AppCompatActivity;    
import android.os.Bundle;    
import android.view.LayoutInflater;    
import android.view.View;    
import android.widget.EditText;    
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {        
static Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
    }
    public void showLogin(View v){
        DialogFragment loginFragment = new LoginDialogFragment();
        loginFragment.show(getFragmentManager(),"login");
    }

    public static class LoginDialogFragment extends DialogFragment{
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState){
            AlertDialog.Builder builder = new
                    AlertDialog.Builder(getActivity());
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View v = inflater.inflate(R.layout.layout_login,null);
            final EditText etUsername = (EditText)
                    v.findViewById(R.id.username);
            final EditText etPassword = (EditText)
                    v.findViewById(R.id.password);
            builder.setView(v).setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    String username, password;
                    username = etUsername.getText().toString();
                    password = etPassword.getText().toString();
                    String msg;
                    if(username.equalsIgnoreCase("abcd")&&password.equalsIgnoreCase("1234")){
                        msg = "Access Granted";
                    }
                    else {
                        msg = "Access Denied";
                    }
                    Toast.makeText(context, msg,Toast.LENGTH_SHORT).show();
                }
            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog,int id){

                }
            });
                    return builder.create();
        }
    }
}
Radhey
  • 2,139
  • 2
  • 24
  • 42
apd
  • 1
  • 2
  • Post your exception/error here also – Ravindra Kushwaha May 22 '17 at 12:50
  • Error:(26, 22) error: no suitable method found for show(android.app.FragmentManager,String) method DialogFragment.show(android.support.v4.app.FragmentManager,String) is not applicable (argument mismatch; android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager) method DialogFragment.show(FragmentTransaction,String) is not applicable (argument mismatch; android.app.FragmentManager cannot be converted to FragmentTransaction) – apd May 22 '17 at 12:50
  • Possible duplicate of [cannot convert from android.app.FragmentManager to android.support.v4.app.FragmentManager](https://stackoverflow.com/questions/16690364/cannot-convert-from-android-app-fragmentmanager-to-android-support-v4-app-fragme) – Ravindra Kushwaha May 22 '17 at 12:52
  • refer this https://stackoverflow.com/a/25306259/1848157 – Radhey May 22 '17 at 12:55
  • You also need to extend your class from android.support.v4.app.FragmentActivity, not just from standard android.AppCompatActivity to get this method. – Radhey May 22 '17 at 12:58
  • Extending it to FragmentActivity fixed it for me but now it doesn't have the application header and icon. – apd May 22 '17 at 13:06

1 Answers1

1

So here is my problem, I keep on getting red underline in my loginFragment.show(getFragmentManager(), "login");

it is because your DialogFragment is from the support library. In this case you have to use getSupportFragmentManager and not getFragmentManager.

Change

loginFragment.show(getFragmentManager(),"login");

with

loginFragment.show(getSupportFragmentManager(),"login");
Blackbelt
  • 156,034
  • 29
  • 297
  • 305