OBX
OBX

Reputation: 6114

Fragment dialog crashes app, any alternative fix?

trying to remove the top banner layer of an Android app, and I made this change in Android Manifest file,

android:theme="@android:style/Theme.NoTitleBar"

This crashed the app, and from another question in Stackoverflow itself, I changed

public class MainActivity extends AppCompatActivity

to

public class MainActivity extends FragmentActivity

And it worked, however, in the same Activity there is also a Dialog present, and whenever the dialog is clicked, it crashes the app again, So looking for a fix! any help will be appreciated.

Here is the logcat:

05-16 21:25:27.796    1839-1839/zyia.alarm.zyia.zyia E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: zyia.alarm.zyia.zyia, PID: 1839
    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:113)
            at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
            at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:47)
            at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:92)
            at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:882)
            at zyia.alarm.zyia.zyia.FireMissiles.onCreateDialog(FireMissiles.java:26)
            at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:308)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

The code for the dialog Activity :

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;

public class FireMissiles extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.main_info, null))
            .setPositiveButton(R.string.fire, new  

   DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // FIRE ZE MISSILES!
                }
            });

    // Create the AlertDialog object and return it
    return builder.create();
   }
}

Upvotes: 2

Views: 838

Answers (3)

frankie015
frankie015

Reputation: 358

You need to use a AppCompat theme.

Instead of using android:theme="@android:style/Theme.NoTitleBar"

Make a custom theme in res/styles

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
// Here you can customize the theme
</style>

Then in your manifest change the theme to that custom theme android:theme="@style/AppTheme"

Upvotes: 0

Chaoz
Chaoz

Reputation: 2260

If you would read the logcat you would see this line too:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

So the fix is self-explaining: in the android manifest add this instead of your theme:

android:theme="@android:style/Theme.AppCompat.NoTitleBar"

Upvotes: 0

natario
natario

Reputation: 25194

Change

import android.support.v7.app.AlertDialog;

to

import android.app.AlertDialog;

Upvotes: 3

Related Questions