我正在尝试在类(不是和活动)中添加一个AlertDialog来处理对后端的异步调用,并更新传递给它的onPostExecute()活动的gui。传递给这个类的活动确实实现了Theme.AppCompat的后代,但是类本身没有(因为它不是一个活动)但是我得到了以下错误:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
具有异步任务的类如下所示:
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();
传递给该类的上下文和根视图来自我的HomeActivity。在清单文件中,homeactivity看起来像这样:
<activity
android:name=".HomeActivity"
android:parentActivityName=".BarcodeCaptureActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
提前多多感谢
答案 0 :(得分:1)
更改您的代码,以便通过调用Activity
:
AsyncTask
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);