Search code examples
androidtoast

getText(R.string.really).tostring() doesn't work in a Toast


I'm a little bit speechless.
I have activity A and B. When I'm in B and get the method deleteItemOption( much input ) then the Toast crashes my app :D

if I open the method in activity A everything is fine. But I need the method in activity A and B, so I programmed the methode global but the Toast is a little bit nasty :D

in the .setpositiveButton there is a Toast. Why does the Toast crash my App if there stands:

Toast.makeText(context, getText(R.string.deleted).toString(), Toast.LENGTH_LONG).show();

or:

Toast.makeText(context, getString(R.string.deleted), Toast.LENGTH_LONG).show();

but this works:

Toast.makeText(context, "deleted", Toast.LENGTH_LONG).show();

EDIT: also I found out that this works:

public void deleteItemOption(.... ,final String toast2, ....){
...
    Toast.makeText(context, toast2, Toast.LENGTH_LONG).show();
...
}

I get my context from there:

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.deleteHersteller:
                ListView listViewDelete = (ListView) findViewById(R.id.listViewHersteller);
                String toast2 = getText(R.string.deleteSuccesfully).toString();
                deleteItemOption(Hersteller.this, listViewDelete, getText(R.string.takeHersteller).toString(), toast2 , mainFolder, getText(R.string.hersteller).toString(), getText(R.string.really).toString());
                return true;
        }
        return true;

The code in Class A:

public void deleteItemOption(final Context context, ListView listView, String toast, final String folder, final String title, final String really){
        Toast.makeText(context, toast, Toast.LENGTH_LONG).show();
        final ListView listViewDelete = listView;
        listViewDelete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                final String deleteName = listViewDelete.getItemAtPosition(position).toString();
                builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        deleteFolder(folder + deleteName);
                        setContent(context, folder, listViewDelete);
                        //this Toast chrash the app
                        Toast.makeText(context, getText(R.string.really).toString(), Toast.LENGTH_LONG).show();
                        //recreate();
                        /* würde gut funktionieren wenn man den ganzen Code überall in jeder activity stehen hat aber da ich
                        global eine Methode gemacht habe, geht das recreate() nicht*/

                    }
                })
                        .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                //recreate();
                                return;
                            }
                        });
                AlertDialog alertDialog = builder.create();
                String titleString = title+" '"+deleteName+"' "+really;
                alertDialog.setTitle(titleString);
                alertDialog.show();
            }
        });


    }

string.xml

<resources>

    <string name="deleteGeschoss">Geschoss löschen</string>
    <string name="deleteGeschossQuestion">Geschoss wirklich löschen?</string>
    <string name="deleted">Löschen erfolgreich</string>

</resources>

There is the Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.niklas.wiederladen, PID: 1549 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.content.ContextWrapper.getResources(ContextWrapper.java:86) at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74) at android.content.Context.getString(Context.java:377) at com.example.myname.myappname.Hersteller$4$2.onClick(Hersteller.java:251) at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:153) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5389) 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:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)


Solution

  • Try this:

    Toast.makeText(context, R.string.deleted, Toast.LENGTH_SHORT).show();
    

    The documentation on Toast says that you can pass in the resId directly instead of calling getString().