我正在尝试使用DialogFragment创建一个简单的登录提示。我得到了旧的requestFeature() - 异常。我花了好一半的时间在谷歌上搜索并尝试不同的解决方案而没有成功。所以我转向你。这是堆栈跟踪:
12-16 22:15:59.986 24137-24137/se.elsgard.zombiehorror E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{se.elsgard.zombiehorror/se.elsgard.zombiehorror.MapsActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:320)
at com.android.internal.app.AlertController.installContent(AlertController.java:253)
at android.app.AlertDialog.onCreate(AlertDialog.java:337)
at android.app.Dialog.dispatchOnCreate(Dialog.java:362)
at android.app.Dialog.show(Dialog.java:266)
at android.app.DialogFragment.onStart(DialogFragment.java:492)
at android.app.Fragment.performStart(Fragment.java:1723)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1865)
at android.app.Activity.performStart(Activity.java:5388)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
以下是显示对话框的活动:
public class MapsActivity extends FragmentActivity implements LoginFragment.NoticeDialogListener{
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LocationManager locationManager;
private LocationListener locationListener;
private Marker you;
private ServerListener serverListener;
private LatLng position;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
position = new LatLng(59.2542, 15.2467);
setUpMapIfNeeded();
serverListener = new ServerListener(this);
serverListener.start();
showLoginDialog();
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if(you != null)
you.remove();
position = new LatLng(location.getLatitude(), location.getLongitude());
you = mMap.addMarker(new MarkerOptions().position(position).title("You"));
you.showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
}
private void showLoginDialog(){
DialogFragment newFragment = new LoginFragment();
newFragment.show(getFragmentManager(), "Login");
}
@Override
protected void onResume() {
super.onResume();
try{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}catch (Exception e){
e.printStackTrace();
}
setUpMapIfNeeded();
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(locationListener);
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
//mMap.addMarker(new MarkerOptions().position(position).title("You"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
@Override
public void onDialogLoginClick(Dialog dialog) {
EditText usernameEditText = (EditText) dialog.findViewById(R.id.nameEditText);
EditText passwordEditText = (EditText) dialog.findViewById(R.id.passwordEditText);
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
serverListener.send("LOGIN " + username + " " + password);
}
@Override
public void onDialogRegisterClick(Dialog dialog) {
EditText usernameEditText = (EditText) dialog.findViewById(R.id.nameEditText);
EditText passwordEditText = (EditText) dialog.findViewById(R.id.passwordEditText);
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
serverListener.send("REGISTER " + username + " " + password);
}
}
DialogFragment:
public class LoginFragment extends DialogFragment {
private EditText usernameEditText, passwordEditText;
private View view;
public interface NoticeDialogListener {
public void onDialogLoginClick(Dialog dialog);
public void onDialogRegisterClick(Dialog dialog);
}
private NoticeDialogListener mListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(view);
builder.setPositiveButton("Login", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogLoginClick(LoginFragment.this.getDialog());
}
})
.setNegativeButton("Register", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogRegisterClick(LoginFragment.this.getDialog());
//LoginFragment.this.getDialog().cancel();
}
});
Dialog dialog = builder.create();
//dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
public LoginFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_login, container, false);
usernameEditText = (EditText) view.findViewById(R.id.nameEditText);
passwordEditText = (EditText) view.findViewById(R.id.passwordEditText);
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
答案 0 :(得分:0)
如果你这样做:
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
到那个:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
应该有效