I am recreating the custom dialog layout on Android Developers. with minor tweaks. I am getting an issue telling me that the dialogfragment cannot be cast to android.app.activity. I am having trouble understandin why I am gettin this error in the logcat.
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)
Activity
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");
}
}
Manifest:
<?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>
If you want to show your custom dialog fragment, you don't need to define it in the Manifest, rather you need to create an instance and call its show method in your activity. That's why you are receiving that error. You have in your manifest an activity defined: com.customdialogfragment.launcher, you need to create that activity and then create the fragment inside the activiy.
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");
}
}
*Note: you should probably rename the activity MainActivity or something equivalent