Je suis un peu sans voix.
J'ai l'activité A et B. Quand je suis en B et la méthode deleteItemOption(much input)
alors le Toast plante mon application: DgetText (R.string.really) .tostring() ne fonctionne pas dans un Toast
si j'ouvre la méthode de l'activité A tout va bien. Mais j'ai besoin de la méthode dans l'activité A et B, donc j'ai programmé la méthode globale mais le Toast est un peu méchant: D
dans le .setpositiveButton
il y a un Toast. Pourquoi le crash Toast mon App s'il se tient:
Toast.makeText(context, getText(R.string.deleted).toString(), Toast.LENGTH_LONG).show();
ou:
Toast.makeText(context, getString(R.string.deleted), Toast.LENGTH_LONG).show();
mais cela fonctionne:
Toast.makeText(context, "deleted", Toast.LENGTH_LONG).show();
EDIT: aussi je trouve que cela fonctionne:
public void deleteItemOption(.... ,final String toast2, ....){
...
Toast.makeText(context, toast2, Toast.LENGTH_LONG).show();
...
}
I obtenir mon contexte de là:
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;
Le code en classe 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>
Il est le Logcat:
E/AndroidRuntime : EXCEPTION FATALE: principal Processus: com.example.niklas.wiederladen, PID: 1549 java.lang.NullPointerException: Tentative d'invocation de la méthode virtuelle 'android.content.res.Resources android.content.Context.getResources()' sur une référence d'objet null à android.content.ContextWrapper.getResources (ContextWrapper.java:86) à android.view.ContextThemeWrapper.getResources (ContextThemeWrapper.java:74) à android.content.Context.getString (Context.java:377) au com.example.myname.myappname.Hersteller $ 4 $ 2.Cliquez sur (Hersteller.java:251) au 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) à java.lang.reflect.Method.invoke (Méthode native) à 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)
la chaîne est définie à string.xml, j'ai oublié d'écrire ceci^_^ – Niklas
D'où passez-vous votre 'Context'? C'est nul. – Pztar
Je l'ai mis ci-dessus où je reçois mon contexte. 'Hersteller.this' est le nom de la classe – Niklas