Java 无法将对话框片段强制转换为android.app.activity

Java 无法将对话框片段强制转换为android.app.activity,java,android,dialog,android-manifest,android-dialogfragment,Java,Android,Dialog,Android Manifest,Android Dialogfragment,我正在上重新创建自定义对话框布局,只做了一些小调整。我收到一个问题,告诉我dialogfragment无法转换到android.app.activity。我很难理解为什么我会在logcat中出现这个错误 日志: 01-20 22:03:10.317: E/AndroidRuntime(9949): FATAL EXCEPTION: main 01-20 22:03:10.317: E/AndroidRuntime(9949): java.lang.RuntimeException: Unable

我正在上重新创建自定义对话框布局,只做了一些小调整。我收到一个问题,告诉我dialogfragment无法转换到android.app.activity。我很难理解为什么我会在logcat中出现这个错误

日志:

01-20 22:03:10.317: E/AndroidRuntime(9949): FATAL EXCEPTION: main
01-20 22:03:10.317: E/AndroidRuntime(9949): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.customdialogfragment/com.customdialogfragment.CustomDialogFragment}: java.lang.ClassCastException: com.customdialogfragment.CustomDialogFragment cannot be cast to android.app.Activity
01-20 22:03:10.317: E/AndroidRuntime(9949):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2004)
活动

public class CustomDialogFragment extends DialogFragment {

    static CustomDialogFragment newInstance() {
        CustomDialogFragment newFragment = new CustomDialogFragment();
        return newFragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(
                inflater.inflate(R.layout.activity_custom_dialog_fragment, null))
                // Add action buttons
                .setPositiveButton("Sign In",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                // sign in the user ...
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                CustomDialogFragment.this.getDialog().cancel();
                            }
                        });
        return builder.create();
    }

    public void showMyDialog() {
        CustomDialogFragment newFragment = new CustomDialogFragment();
        newFragment.show(getFragmentManager(), "custom");
    }
}
舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.customdialogfragment"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.customdialogfragment.launcher"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

XML


如果要显示自定义对话框片段,不需要在清单中定义它,而是需要在活动中创建一个实例并调用其show方法。这就是为什么你会收到这个错误。您在清单中定义了一个活动:com.customdialogfragment.launcher,您需要创建该活动,然后在活动中创建片段

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class launcher extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        showMyDialog();
    }

    public void showMyDialog() {    
      CustomDialogFragment newFragment = new CustomDialogFragment();
      newFragment.show(getSupportFragmentManager(), "custom");
    }
}

*注意:您可能应该重命名活动MainActivity或类似的东西
DialogFragment
Fragment
的子级,因此不能将其强制转换为
activity

创建对话框的3种方法

  • 使用扩展
    对话框的
    警报对话框
    。实际上它是一个
    窗口
  • 使用
    对话框Fragment
    ,它扩展了
    Fragment
  • 如文档所述,使用对话框样式
    活动
  • 在大屏幕上将活动显示为对话框

    在小屏幕上,您可以通过在大屏幕上显示活动作为对话框来实现相同的结果,而不是将对话框显示为全屏UI。您选择哪种方法取决于您的应用程序设计,但当您的应用程序已经设计为小屏幕,并且您希望通过将短期活动显示为对话框来改善平板电脑的体验时,将活动显示为对话框通常很有用

    要仅在大屏幕上将活动显示为对话框,请将Theme.Holo.DialogWhenLarge主题应用于manifest元素:

    
    

    这一个需要在清单中定义。

    这是因为
    对话框片段
    不是
    活动
    。为什么要尝试将其添加到清单中?它确实不是活动,但无论是否在清单中定义,它都会强制关闭。我在调试问题时在清单中定义了它。logcat建议我在清单中定义活动。请解释下一票。好的,我更新了答案。实际上,您缺少活动类。片段存在于活动中。
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    
    public class launcher extends FragmentActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity);
    
            showMyDialog();
        }
    
        public void showMyDialog() {    
          CustomDialogFragment newFragment = new CustomDialogFragment();
          newFragment.show(getSupportFragmentManager(), "custom");
        }
    }
    
    <activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" >