android - Error while creating an AlertDialog -
i'm beginner in android. so, have icon on clicking alertdialog should appear. simple.----android:onclick = "oncreatedialog"----
package com.example.android.guardian; import android.app.alertdialog; import android.content.context; import android.content.dialoginterface; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.imageview; public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public void oncreatedialog(view v) { imageview image = (imageview) findviewbyid(r.id.g_icon); image.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { alertdialog.builder builder = new alertdialog.builder(getapplicationcontext()); // layout inflater layoutinflater inflater = builder.getlayoutinflater(); // inflate , set layout dialog // pass null parent view because going in dialog layout builder.setview(inflater.inflate(r.layout.activity_g_pass, null)) // add action buttons .setpositivebutton(r.string.savepass, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { // sign in user ... } }) .setnegativebutton(r.string.cancel, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { } }); builder.create(); } }); } }
the above code skeleton. no details. want implement above function when clicked i.e open alertdialog. says cannot resolve getlayoutinflater() method strings.xml
guardian
<string name="hello_world">hello world!</string> <string name="action_settings">settings</string> <string name="title_activity_g_pass">google</string> <string name = "cancel">cancel</string> <string name = "savepass">save</string>
logcat
08-17 01:34:22.430 26909-26909/com.example.android.guardian e/androidruntime﹕ fatal exception: main process: com.example.android.guardian, pid: 26909 java.lang.illegalstateexception: not execute method of activity @ android.view.view$1.onclick(view.java:3829) @ android.view.view.performclick(view.java:4444) @ android.view.view$performclick.run(view.java:18457) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5047) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:609) @ dalvik.system.nativestart.main(native method) caused by: java.lang.reflect.invocationtargetexception @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ android.view.view$1.onclick(view.java:3824) at android.view.view.performclick(view.java:4444) at android.view.view$performclick.run(view.java:18457) at android.os.handler.handlecallback(handler.java:733) at android.os.handler.dispatchmessage(handler.java:95) at android.os.looper.loop(looper.java:136) at android.app.activitythread.main(activitythread.java:5047) 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:793) at com.android.internal.os.zygoteinit.main(zygoteinit.java:609) at dalvik.system.nativestart.main(native method) caused by: android.view.windowmanager$badtokenexception: unable add window -- token null not application @ android.view.viewrootimpl.setview(viewrootimpl.java:563) @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:260) @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:69) @ android.app.dialog.show(dialog.java:286) @ com.example.android.guardian.mainactivity.oncreatedialog(mainactivity.java:60) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:515) at android.view.view$1.onclick(view.java:3824) at android.view.view.performclick(view.java:4444) at android.view.view$performclick.run(view.java:18457) at android.os.handler.handlecallback(handler.java:733) at android.os.handler.dispatchmessage(handler.java:95) at android.os.looper.loop(looper.java:136) at android.app.activitythread.main(activitythread.java:5047) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:515)
manifest xml
<?xml version="1.0" encoding="utf-8"?>
<application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".g_pass" android:label="@string/title_activity_g_pass" android:parentactivityname=".mainactivity"> <meta-data android:name="android.support.parent_activity" android:value="com.example.android.guardian.mainactivity" /> </activity> </application>
replace this:
public void oncreatedialog(view v) { alertdialog.builder builder = new alertdialog.builder(this); layoutinflater inflater = this.getlayoutinflater(); view view = inflater.inflate(r.layout.activity_main, null); builder.setview(view); builder.setpositivebutton(r.string.savepass, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { // sign in user ... } }); builder.setnegativebutton(r.string.cancel, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { } }); alertdialog dialog = builder.create(); dialog.show(); } }
this open alertdialog when clicking imageview. in xml file imageview in, add:
android:onclick="oncreatedialog"
android:clickable="true"
then go strings.xml , change string "savepass" "savepass".
Comments
Post a Comment