片段对话框崩溃应用程序,任何替代修复?

时间:2015-05-16 15:56:56

标签: android

尝试删除Android应用的顶部横幅图层, 我在Android Manifest文件中进行了此更改,

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

这使应用程序崩溃,而Stackoverflow本身的另一个问题,我改变了

public class MainActivity extends AppCompatActivity

public class MainActivity extends FragmentActivity

然而,它有效,但在同一个Activity中还有一个Dialog存在,每当点击对话框时,它再次崩溃应用程序,所以寻找修复!任何帮助将不胜感激。

这是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)

对话框活动的代码:

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();
   }
}

3 个答案:

答案 0 :(得分:3)

更改

import android.support.v7.app.AlertDialog;

import android.app.AlertDialog;

答案 1 :(得分:0)

如果您要阅读logcat,您也会看到这一行:

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

所以修复是自我解释的:在android清单中添加这个而不是你的主题:

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

答案 2 :(得分:0)

您需要使用AppCompat主题。

而不是使用android:theme="@android:style/Theme.NoTitleBar"

在res / styles中创建自定义主题

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

然后在您的清单中将主题更改为该自定义主题android:theme="@style/AppTheme"