我正在Android开发者。上重新创建自定义对话框布局,只需稍作调整。我收到一个问题,告诉我不能将对话框片段转换为android.app.activity。我很难理解为什么我在逻辑猫中会犯这个错误。
逻辑猫:
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name"
android:scaleType="center"
android:src="@drawable/ic_launcher" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="username"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:fontFamily="sans-serif"
android:hint="password"
android:inputType="textPassword" />
</LinearLayout>发布于 2014-01-21 03:25:44
如果要显示自定义对话框片段,则不需要在报表中定义它,而是需要在activity中创建实例并调用其显示方法。所以你才会收到那个错误。您在清单中定义了一个活动: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或类似的东西。
发布于 2014-01-21 03:30:58
DialogFragment是Fragment的一个子类,因此不能将其转换为Activity。
创建对话框的3种方法。
AlertDialog,它扩展了Dialog。实际上它是一个Window。DialogFragment,它扩展了Fragment。Activity。将活动显示为大屏幕上的对话框 与在小屏幕上以全屏幕UI形式显示对话框不同,您可以通过在大屏幕上将活动显示为对话框来实现相同的结果。您所选择的方法取决于您的应用程序设计,但是当您的应用程序已经为小屏幕设计时,将一个活动显示为对话框通常是有用的,并且您希望通过将一个短暂的活动显示为一个对话框来改进平板电脑的体验。 若要仅在大屏幕上将活动显示为对话框,请将Theme.Holo.DialogWhenLarge主题应用于清单元素:
<activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" >这个人需要在清单中定义。
https://stackoverflow.com/questions/21248390
复制相似问题