lundi 2 mars 2015

Leaked window error using Material Dialog Compact (Alert Dialog)



I'm getting an error when i try and delete an item from a listview using material dialog compact. Here is the error message below. Any help would be appreciated. I'm new to Java so my experience with debugging is poor.



03-02 17:11:38.194 30222-30222/com.m4tchb0x87.rhinote E/WindowManager﹕ android.view.WindowLeaked: Activity com.m4tchb0x87.rhinote.ReadNoteMenu has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42a9a7a8 V.E..... R....... 0,0-1061,428} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java)
at android.app.Dialog.show(Dialog.java:289)
at com.afollestad.materialdialogs.MaterialDialog.show(MaterialDialog.java:1464)
at com.afollestad.materialdialogs.MaterialDialogCompat$Builder.show(MaterialDialogCompat.java:148)
at com.m4tchb0x87.rhinote.ReadNoteMenu$2.onItemLongClick(ReadNoteMenu.java:172)
at android.widget.AbsListView.performLongPress(AbsListView.java)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)


My code:



public class ReadNoteMenu extends ActionBarActivity {



public static final String EXTRA_MESSAGE = "com.m4tchb0x87.rhinote.MESSAGE";

Context context;

public ReadNoteMenu() {

this.context = this;
}

ArrayAdapter mArrayAdapter;
ListView listView;


protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
this.setContentView(R.layout.activity_read_note_menu);

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

listView = (ListView) this.findViewById(R.id.readListView);
String fileNames[] = new File(String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Rhinote").list();
mArrayAdapter = new ArrayAdapter<>(this, R.layout.list_item_1, fileNames);
listView.setAdapter(mArrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView adapterView, View view, int i, long l) {
String string = (String) listView.getItemAtPosition(i);
Intent intent = new Intent(ReadNoteMenu.this, (Class) ReadNote.class);
intent.putExtra(EXTRA_MESSAGE, string);
ReadNoteMenu.this.startActivity(intent);
}
});

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {


public boolean onItemLongClick(AdapterView adapterView, View view, final int position, long l) {




ContextThemeWrapper CTW = new ContextThemeWrapper( context, R.style.ADM_theme);
final MaterialDialogCompat.Builder MDM1 = new MaterialDialogCompat.Builder(CTW);

MDM1.setMessage("Edit or delete file?");
MDM1.setPositiveButton("Delete", new DialogInterface.OnClickListener() {



public void onClick(DialogInterface dialog, int which) {

ContextThemeWrapper CTW = new ContextThemeWrapper( context, R.style.ADM_theme);
final MaterialDialogCompat.Builder MDM2 = new MaterialDialogCompat.Builder(CTW);



MDM2.setMessage("Confirm delete file?");
MDM2.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {




public void onClick(DialogInterface dialog, int which) {


String str = (String) listView.getItemAtPosition(position);
new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/RhiNote" + "/" + str).delete();

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_custom,
(ViewGroup) findViewById(R.id.toast_layout_root_0));
TextView text = (TextView) layout.findViewById(R.id.text_0);
text.setText("Note deleted!");
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();

ReadNoteMenu.this.finish();
ReadNoteMenu.this.startActivity(ReadNoteMenu.this.getIntent());

}
});

MDM2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}
});

MDM2.show();


}

});


MDM1.setNegativeButton("Edit", new DialogInterface.OnClickListener() {


String fileText = "";

public void onClick(DialogInterface dialogInterface, int which) {

String str1 = (String)listView.getItemAtPosition(position);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/RhiNote" + "/" + str1);


{
try {

Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String string = scanner.nextLine();
this.fileText = String.valueOf(this.fileText) + string + "\n";
}
} catch (FileNotFoundException localFileNotFoundException) {

localFileNotFoundException.printStackTrace();
}
}

Intent intent = new Intent(ReadNoteMenu.this.context, EditNote.class);
intent.putExtra("TEXTBODY", fileText);
intent.putExtra("TEXTTITLE", str1);
ReadNoteMenu.this.startActivity(intent);



}



});


MDM1.show();

MDM1.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {


dialog.dismiss();


}
});

MDM1.show();



return true;
}
});
}
}



Aucun commentaire:

Enregistrer un commentaire