所以这是我的问题,我继续在loginFragment.show(getFragmentManager(), "login");
中显示红色下划线
为什么它在参数下有下划线。当我使用getSupportFragmentManager
而不使用AppCompatActivity
并使用Activity时,是的,它在程序本身没有错误,但是当我单击按钮显示对话框时,它会崩溃。这里是我的全部代码。
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();
}
}
}
答案 0 :(得分:1)
所以这是我的问题,我继续在我的下划线 loginFragment.show(getFragmentManager(),“login”);
这是因为您的DialogFragment来自支持库。在这种情况下,您必须使用getSupportFragmentManager
而不是getFragmentManager
。
更改
loginFragment.show(getFragmentManager(),"login");
与
loginFragment.show(getSupportFragmentManager(),"login");