gpt4 book ai didi

android - 您需要在创建 AlertDialog 时使用 Theme.AppCompat 主题

转载 作者:行者123 更新时间:2023-11-29 01:25:03 26 4
gpt4 key购买 nike

我的 MainActivity 类:

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
....
...
}

样式.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

当我运行该应用程序时,它运行良好,但是当我尝试从我的适配器类创建/显示 AlertDialog.Builder 时,它会出错

 `You need to use a Theme.AppCompat theme (or descendant) with this activity.at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:124)`

list 文件:

 <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

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

AlertDialog代码:

public class mainviewAdapter  extends ArrayAdapter<MainCategory>
{

AlertDialog.Builder builderSingle;
public mainviewAdapter (Context context, ArrayList<MainCategory> values) {
super(context, R.layout.home_rt, values);

this.context = context;
this.values = values;
builderSingle = new AlertDialog.Builder(context);
builderSingle.setIcon(R.mipmap.ic_launcher);
builderSingle.setTitle("Select Your Category");


}

on button click:

builderSingle.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

builderSingle.setAdapter(arrayAdapter,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
}
});

builderSingle.show();

如何解决 Alertdialog 上的这个错误?

最佳答案

尝试通过替换您的代码行将主题设置为您的 AlertDialog :

builderSingle = new AlertDialog.Builder(context);

与:

builderSingle = new AlertDialog.Builder(context,R.style.dialogTheme);

在您的 style.xml 中添加代码:

<style name="dialogTheme" parent="@style/Theme.AppCompat">
<!-- Any customizations for your app running on devices with Theme.Holo here -->
</style>

并记住要避免的一件事 Unable to add window -- token null is not for an application Exception 而不是 getApplicationContext(),只需使用 Activityname。这个

关于android - 您需要在创建 AlertDialog 时使用 Theme.AppCompat 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34523384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com