gpt4 book ai didi

java - 警报对话框异常 "Unable to add windows -- token null is not for an application",我应该提供什么上下文?

转载 作者:行者123 更新时间:2023-12-02 11:06:38 25 4
gpt4 key购买 nike

我知道之前有人问过同样的问题,我检查了他们,但没有帮助,所以我正在构建一个Android应用程序,并且我使用 ListView ,我在项目中添加了一个按钮,删除该条目,并且我想显示一个确认对话框。

当我按下该按钮时,应用程序崩溃了,当我搜索问题时,我发现我传递的上下文中有问题,我尝试了很多方法,但找不到解决方案,这是代码。这是项目适配器代码:

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final View v;

LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflater.inflate(R.layout.product_cell, null);

final Produit currentProduit = getItem(position);
getviews(v);
fillViews(currentProduit);

supprimer.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
MyMethodsClass.displayToast("i'm working dude", view.getContext());
AlertDialog.Builder confirmDialog;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
confirmDialog = new AlertDialog.Builder(v.getContext(), android.R.style.Theme_Material_Dialog_Alert);
else
confirmDialog = new AlertDialog.Builder(v.getContext());

confirmDialog.setTitle("Supprimer?");
confirmDialog.setMessage("Etes vous sur de vouloir supprimer le produit" + currentProduit.getNomP());
confirmDialog.setNegativeButton("Annuler", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});
confirmDialog.setPositiveButton("Confirmer", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
deleteProduct(currentProduit);
productsAdapter.remove(currentProduit);
productsAdapter.notifyDataSetChanged();
}
}).show();
}
});

return v;
}

这是使用它的 Activity 的代码:

public class Stock extends AppCompatActivity implements View.OnClickListener {

String barcode;
EditText codebare, nomproduit;
Button scanbut, rechbut;
ListView stockProdList;
ArrayList<Produit> productsArray;
public static ProductsAdapter productsAdapter;
public Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_supprimer_produit);

barcode = getIntent().getStringExtra("code");
codebare = (EditText) findViewById(R.id.rechStockEditScan);
nomproduit = (EditText) findViewById(R.id.rechStockNomP);
scanbut = (Button) findViewById(R.id.scannStock);
rechbut = (Button) findViewById(R.id.rechercheStock);
stockProdList = (ListView) findViewById(R.id.listStockSup);

productsAdapter = new ProductsAdapter(getApplicationContext(), 0);
productsArray = new ArrayList<>();

if (!TextUtils.isEmpty(barcode))
codebare.setText(barcode);

stockProdList.setAdapter(productsAdapter);
fillListView();
}

void searchProduct() {
productsArray.clear();
String query = "SELECT * FROM produit WHERE ";
if (!nomproduit.getText().toString().isEmpty() && !codebare.getText().toString().isEmpty()) {
query += "NOMP='" + nomproduit.getText().toString() + "' AND BAREC='" + codebare.getText().toString() + "';";
} else if (!nomproduit.getText().toString().isEmpty()) {
query += "NOMP='" + nomproduit.getText().toString() + "';";
} else if (!codebare.getText().toString().isEmpty()) {
query += "BAREC='" + codebare.getText().toString() + "';";
} else {
MyMethodsClass.displayToast("FAUT REMPLIRE AU MOIN UN CHAMP POUR RECHERCHER", this);
}
try {
Connection connection = DataBaseConnection.CONNECT();
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(query);

while (rs.next()) {
productsArray.add(new Produit(rs.getString("NOMP"), rs.getString("BAREC"), rs.getString("PHOTOP"), rs.getFloat("PRIXA"), rs.getFloat("PRIXG"),
rs.getFloat("PRIXD"), rs.getInt("QUANTITEP"), rs.getInt("NBREMBA"), rs.getInt("IDP")));
}
} catch (Exception e) {
Log.e("ERROR WHILE SEARCHING FOR DATA LINE 70", e.getMessage());
}
Log.e("what the hell?", "it's running");
productsAdapter.clear();
productsAdapter.addAll(productsArray);
stockProdList.setAdapter(null);
stockProdList.setAdapter(productsAdapter);
}

这是日志:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.simapps.mobilestock, PID: 9260
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:684)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:289)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:311)
at android.app.AlertDialog$Builder.show(AlertDialog.java:993)
at Adapters.ProductsAdapter$1.onClick(ProductsAdapter.java:87)
at android.view.View.performClick(View.java:4855)
at android.view.View$PerformClick.run(View.java:20287)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

最佳答案

要显示Dialog,您需要使用Activity上下文。此处您使用的 Application 上下文是不正确的。

productsAdapter = new ProductsAdapter(getApplicationContext(), 0);

要使其正常工作,请更改上面的行以使用 Activity 上下文。由于此调用是在 Activity 内进行的,因此您可以使用:

productsAdapter = new ProductsAdapter(this, 0);

但这不是最好的方法,因为您的参数类型是 Context 而不是 Acticity 并且当它发生在您身上时,上下文可能不正确。

更好的想法是为您的 Activity 提供点击监听器回调,并管理 Activity 内的 Dialog 而不是 适配器

关于java - 警报对话框异常 "Unable to add windows -- token null is not for an application",我应该提供什么上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50916541/

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