0

我正在尝试获取一个对话框来警告用户应该以横向模式查看屏幕。我有一个对话框类和我的活动调用对话框,但它抱怨 show(FragmentManager, String) 不适用于 show(FragmentManager, String)。

import my.application.dialogs.OrientationAlertDialog;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;

public class StationMapActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        OrientationAlertDialog dialog = new OrientationAlertDialog();
        dialog.show(getSupportFragmentManager(), "String");
    } else {
        setContentView(R.layout.activity_station_map);
    }
}
}

我的对话类是:

import my.application.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class OrientationAlertDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.landscape_dialog_alert)
               .setPositiveButton(R.string.landscape_dialog_ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       System.out.println("OK");
                   }
               })
               .setNegativeButton(R.string.landscape_dialog_cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       System.out.println("CANCEL");
                   }
               });
        return builder.create();
    }
}

我从 android 对话框文档中调整了代码,所以我不确定在哪里可以找到解决方案。

我尝试过的每一种方式,当我按下按钮进入这个活动时,应用程序就会崩溃。

Log cat 不打印任何有用的东西:

[2013-10-09 00:24:17 - my.application] ------------------------------
[2013-10-09 00:24:17 - my.application] Android Launch!
[2013-10-09 00:24:17 - my.application] adb is running normally.
[2013-10-09 00:24:17 - my.application] Performing my.application.MainActivity activity launch
[2013-10-09 00:24:24 - my.application] Application already deployed. No need to reinstall.
[2013-10-09 00:24:24 - my.application] Starting activity my.application.MainActivity on device 00194db94fb33f
[2013-10-09 00:24:25 - my.application] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=my.application/.MainActivity }
[2013-10-09 00:24:26 - my.application] Attempting to connect debugger to 'my.application' on port 8600
4

1 回答 1

2

您正在使用android.support.v4.app.DialogFragment,表示您正在尝试使用 Android 支持包的片段的反向移植。如果是这种情况,您需要使用getSupportFragmentManager(),而不是getFragmentManager()

于 2013-10-08T23:46:12.967 回答