Theme.AppCompat 非 activity AlertDialog 所需的主题

Theme.AppCompat theme required for non activity for AlertDialog

我正在尝试在 class(不是 activity)中添加一个 AlertDialog,它处理对后端的异步调用并更新传递给 activity 的 gui它 onPostExecute()。传递给此 class 的 activity 确实实现了 Theme.AppCompat 的后代,但 class 本身没有(因为它不是 activity)但我得到了关注错误:

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

具有异步任务的 class 如下所示:

public class QueryUserTask extends AsyncTask<Void, Void, Seller>{
private Context mainContext;
private View mainView;
int barcode;

public QueryUserTask(Context context, View rootView, int sellerID) {
    this.mainContext = context;
    this.mainView = rootView;
    this.barcode = sellerID;
}

protected Seller doInBackground(Void... unused) {
    Seller seller = null;
    try {
        ApiRequester API = new ApiRequester();
        seller = API.getSellerFromApi(barcode);

    } catch (NoSuchAlgorithmException e) {
        Log.d("Seller API Error", e.getMessage(), e);
        Toast APIError = Toast.makeText(mainContext,R.string.NoSuchAlgorithmException, Toast.LENGTH_LONG);
        APIError.show();
    }
    return seller;
}

protected void onPostExecute(Seller seller) {
    // Do something with the result.
    TextView sellerNameTextView = (TextView) mainView.findViewById(R.id.sellername);
    TextView sellerIDTextView = (TextView) mainView.findViewById(R.id.sellerid);
    TextView newspaperCountTextView = (TextView) mainView.findViewById(R.id.newsCount);
    TextView calenderCountTextView = (TextView) mainView.findViewById(R.id.calCount);

    sellerNameTextView.setText(String.valueOf(seller.getName()));
    // sellerIDTextView.setText(String.valueOf());
    newspaperCountTextView.setText(String.valueOf(seller.getNewspapers()));
    calenderCountTextView.setText(String.valueOf(seller.getCalendars()));

    ImageView sellerImage = (ImageView)mainView.findViewById(R.id.imageView2);
    sellerImage.setImageBitmap(seller.getImage());

    String text = seller.getText();
    Log.d("text", text);
    if(!text.equals("")){

        AlertDialog.Builder builder = new AlertDialog.Builder(mainContext);
        builder.setMessage("test")
                .setTitle("test");
        AlertDialog dialog = builder.create();
        dialog.show();

传递给 class 的上下文和根视图来自我的 HomeActivity。主页activity 在清单文件中看起来像这样:

    <activity
        android:name=".HomeActivity"
        android:parentActivityName=".BarcodeCaptureActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
    <activity

非常感谢

更改代码以传递调用 AsyncTaskActivity :

Activity mActivity;

public QueryUserTask(Activity activity, View rootView, int sellerID) {
    this.mActivity= activity;
    this.mainView = rootView;
    this.barcode = sellerID;
}

现在像这样使用 Activity

AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);