【问题标题】:AlertDialog and ArrayAdapterAlertDialog 和 ArrayAdapter
【发布时间】:2016-09-24 04:52:37
【问题描述】:

我有一个扩展 ArrayAdapter 的适配器。通过这个适配器类,我在 MainActivity 中管理了一个 ListView
我在列表的每一行中也有一些按钮,当我单击一个按钮时,我希望显示一个 AlertDialog。这样做时,我收到了一个关于 Theme 的错误。
我在清单中阅读了有关更改主题的信息,但即使我更改它也不起作用。

holder.delete=(ImageButton)row.findViewById(R.id.delete_btn_item);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder dialog= new AlertDialog.Builder(getContext());
                dialog.setTitle("Deleting...");
                dialog.setMessage("You are deleting DEFINITELY <" + db.getAllRecipes().get(position).getRec_name() + ">. Are you sure to continue?");
                dialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                dialog.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        db.deleteRecipe(db.getAllRecipes().get(position));
                        dialog.dismiss();
                        Toast.makeText(getContext(), "deleted " + db.getAllRecipes().get(position).getRec_name(), Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.create();
                dialog.show();
            }
        });

我在 getView 方法中有持有者。我的第一行代码是:

public class RecipeAdapter extends ArrayAdapter {
DatabaseHelper db;
LayoutInflater layoutInflater;
private Context context;
PopupWindow popUpWndw;


List list= new ArrayList();
public RecipeAdapter(Context context, int resource) {
    super(context, resource);
   this.context = context;
   db=new DatabaseHelper(context.getApplicationContext());
}

我得到的错误是这样的:

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

【问题讨论】:

  • 尝试将AlertDialog.Builder dialog= new AlertDialog.Builder(getContext());更改为AlertDialog.Builder dialog= new AlertDialog.Builder(context)
  • 看来您正在将支持库类与“标准”类混合。检查您的 Activity 和对话框是否匹配。
  • 您能否向我们展示您的AndroidManifest.xmlstyle.xml 和错误日志。
  • 我传递了上下文而不是getContext(),但错误消息仍然保持不变

标签: android android-alertdialog android-adapter android-context


【解决方案1】:

您需要将android:theme="@style/Theme.AppCompat.Light" 添加到AndroidManifest.xml 文件中的应用程序标签中。

【讨论】:

    【解决方案2】:

    将您的mainactivity context 传递给您的自定义适配器并使用该context 创建您的alertdialog

     yourAdaptor(this); // this is mainactivity context 
    

    你的适配器

    private Context mContext;
    
        public yourAdaptor(Context context){
        mContext = context;
        }
    
        AlertDialog.Builder dialog= new AlertDialog.Builder(mContext );
    

    更新 ::

    只需传递适配器的全局上下文实例。

    AlertDialog.Builder dialog= new AlertDialog.Builder(context);
    

    【讨论】:

    • 我看到他已经将上下文传递给Adapter。(private Context context;)
    • 你是对的@PhanVănLinh。哦,所以他只能使用那个上下文。
    【解决方案3】:

    在您的清单文件中使用以下主题进行此活动

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

    【讨论】:

      【解决方案4】:

      您通过其构造函数将上下文传递给适配器类,并将其保存在变量“context”中。因此请使用上下文而不是 getcontext()。它对我有用。我认为这会对你有所帮助。

          holder.delete.setOnClickListener(new View.OnClickListener() {
      
              @Override
              public void onClick(View v) {
      
                  AlertDialog.Builder dialog= new AlertDialog.Builder(context);
                  dialog.setTitle("Deleting...");
                  dialog.setMessage("You are deleting DEFINITELY <" + db.getAllRecipes().get(position).getRec_name() + ">. Are you sure to continue?");
                  dialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.dismiss();
                      }
                  });
                  dialog.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          db.deleteRecipe(db.getAllRecipes().get(position));
                          dialog.dismiss();
                          Toast.makeText(context, "deleted " + db.getAllRecipes().get(position).getRec_name(), Toast.LENGTH_SHORT).show();
                      }
                  });
                  dialog.create();
                  dialog.show();
              }
          });
      

      【讨论】:

        【解决方案5】:

        使用以下代码更改您的 style.xml:-

        价值观

        “styles.xml”

        <resources>
        
            <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
                <!-- Customize your theme here. -->
            </style>
        </resources>
        

        Androidmanifest.xml

         <activity
            android:name="com.package name.your activity"
            android:theme="@style/AppTheme">
         </activity>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-07
          • 1970-01-01
          • 2011-03-10
          • 2011-06-08
          相关资源
          最近更新 更多