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