-1

i tried to add an Error message for my user if the query doesn't find any result. I have tried this code but i don't now what the error means it gives me on building. I am very new to programming

Thanks in advance

 public void createRechnung(View v) {
        if (mBetragInput.getText().length() > 0) {
            mName = mNameInput.getSelectedItem().toString();
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen");
            query.whereEqualTo("name", mName);
            query.findInBackground(new FindCallback<ParseObject>() {
                public void done(List<ParseObject> objects, ParseException eg) {
                    if (eg == null && objects.size() > 0) {
                        mBetrag = mBetragInput.getText().toString();
                        ParseObject Grillen = objects.get(0);
                        Grillen.put("Betrag", mBetrag);
                        Grillen.put("Rechnung", true);
                        Grillen.saveInBackground(new SaveCallback() {
                            public void done(ParseException e) {
                                if (e == null) {
                                    //success, saved!
                                    Log.d("MyApp", "Successfully saved!");
                                }
                            }
                        });
                    }else {
                        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                        alertDialog.setTitle("Danke");
                        alertDialog.setMessage("Rechnung wurde erstellt.");
                        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                mBetragInput.setText("€");
                                mNameInput.setSelection(0);

                            }
                        });
                        alertDialog.show();
                    }
                }
            });
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Danke");
            alertDialog.setMessage("Rechnung wurde erstellt.");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    mBetragInput.setText("€");
                    mNameInput.setSelection(0);

                }
            });
            alertDialog.show();
        }
        else {
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Achtung");
            alertDialog.setMessage("Bitte gib einen Betrag ein!");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            alertDialog.show();
        }
    }
Aakash
  • 5,181
  • 5
  • 20
  • 37

2 Answers2

0

The error is pretty simple. You need the context not the FindCallback. Try getting the context(activity context not app context), like getActivity(if possible of course).

new AlertDialog.Builder(this)

The above method will never give you the context but instead FindCallback. Try using a method instead that gives you the activity context. Example:

new AlertDialog.Builder(WhateverActivity.this)


Similar:
Why does AlertDialog.Builder(Context context) only accepts Activity as a parameter?

Community
  • 1
  • 1
Andrei T
  • 2,985
  • 3
  • 21
  • 28
0

Change

AlertDialog alertDialog = new AlertDialog.Builder(this).create();

to

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();

where change MainActivity to your activity's name, the problem was that alert dialog is getting wrong context.

Aakash
  • 5,181
  • 5
  • 20
  • 37
  • thanks men after your help it is actuall pretty simple – Christian Lerch Oct 01 '15 at 21:23
  • maybe you can help me too with the eprecated in API level use in this part alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mBetragInput.setText("€"); mNameInput.setSelection(0); } – Christian Lerch Oct 01 '15 at 21:24
  • use alertDialog.setPositiveButton instead, for more reference check http://developer.android.com/guide/topics/ui/dialogs.html – Aakash Oct 01 '15 at 21:28
  • okay thank you i have looked at this but i musst have over read it – Christian Lerch Oct 01 '15 at 21:28