简体   繁体   中英

Theme.AppCompat theme required for non activity for AlertDialog

I am trying to add an AlertDialog in a class (not and activity) that handles an async call to the back end and updates the gui of an activity passed to it onPostExecute(). The activity passed to this class does implement a decendent of Theme.AppCompat but the class itself does not (since it is not an activity) but I get following error:

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

The class with the async task looks like this:

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

The context and rootview passed to the class come from my HomeActivity. THe homeactivity looks like this in the manifest file:

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

Thanks a lot in advance

Change your code to pass the Activity that is calling the your AsyncTask :

Activity mActivity;

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

Now use the Activity like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM