I know that some people asked the same question before, I checked them and it didn't help, so I'm building an Android application, and I'm using the list view, I added a button in the items, to delete the entry and I want to show a confirmation dialog.
When I press that button the application crashes, when I searched for the problem I found that there is a problem in the context I'm passing, I tried so many things but I couldn't find the solution, here is the code. This is the item Adaptor code:
@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;
}
and this is the code of the activity using it:
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);
}
and here is the log:
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)
To show a Dialog
you need to use the Activity
context. Here you're using the Application
context which is incorrect.
productsAdapter = new ProductsAdapter(getApplicationContext(), 0);
To make it work change the above line to use the Activity
context. Since this call is made inside an Activity
you can use:
productsAdapter = new ProductsAdapter(this, 0);
But this is not the best way to do so because your parameter type is Context
and not Acticity
and as it's happening to you, the context may be incorrect.
A better idea is to provide a click listener callback to your Activity
and manage the Dialog
inside the Activity
instead of the Adapter
.