getText(R.string.really).tostring()在Toast

时间:2017-01-11 22:16:45

标签: android toast

我有点无言以对 我有活动A和B. 当我在B中并获得方法deleteItemOption( much input )时,Toast会崩溃我的应用程序:D

如果我在活动A中打开方法,一切都很好。 但我需要活动A和B中的方法,所以我编写了方法全局,但Toast有点讨厌:D

.setpositiveButton中有一个Toast。 为什么Toast会崩溃我的应用程序如果有:

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

或:

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

但这有效:

Toast.makeText(context, "deleted", Toast.LENGTH_LONG).show();
编辑:我也发现这有效:

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

我从那里得到我的背景:

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;

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>

有Logcat:

  

E / AndroidRuntime:致命异常:主要                                                                                     过程:com.example.niklas.wiederladen,PID:1549                                                                                     java.lang.NullPointerException:尝试调用虚方法   'android.content.res.Resources android.content.Context.getResources()'   在null对象引用上                                                                                         在android.content.ContextWrapper.getResources(ContextWrapper.java:86)                                                                                         在   android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)                                                                                         在android.content.Context.getString(Context.java:377)                                                                                         在   com.example.myname.myappname.Hersteller $ 4 $ 2.onClick(Hersteller.java:251)                                                                                         在   android.support.v7.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:153)                                                                                         在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                         在android.os.Looper.loop(Looper.java:211)                                                                                         在android.app.ActivityThread.main(ActivityThread.java:5389)                                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                                         在java.lang.reflect.Method.invoke(Method.java:372)                                                                                         在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1020)                                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

2 个答案:

答案 0 :(得分:2)

试试这个:

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

Toast上的文档说明您可以直接传入resId而不是调用getString()

答案 1 :(得分:0)

尝试:

getResources().getString(R.string.your_string);

确保your_string文件中定义了stirng.xml