android - Application crashes when pressing the button -
i have struggled find solution couple of days , has drive me nuts. following tutorial in http://developer.android.com/guide/topics/ui/actionbar.html#actionitems
unfortunately application crashes when send button pressed (it working fine before action bar implemented)
here involved files:
1. androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mycompany.myfirstapps" > <uses-sdk android:minsdkversion="7" android:targetsdkversion="18" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/theme.appcompat.light.darkactionbar"> <!-- parent activity--> <activity android:name=".myactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <!--second activity --> <activity android:name=".displaymessageactivity" android:label="@string/title_activity_display_message" android:parentactivityname=".myactivity" > <meta-data android:name="android.support.parent_activity" android:value=".myactivity" /> </activity> </application> </manifest>
- myactivity.java
package com.mycompany.myfirstapps; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.support.v7.app.appcompatactivity; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.content.intent; import android.widget.edittext; public class myactivity extends appcompatactivity { public final static string extra_message="com.mycompany.myfirstapps.message"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); getsupportactionbar().setdisplayhomeasupenabled(true); // if minsdkversion 11 or higher, instead use: //getactionbar().setdisplayhomeasupenabled(true); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu items use in action bar menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main_activity_actions, menu); return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle presses on action bar items switch (item.getitemid()) { case r.id.action_search: opensearch(); return true; case r.id.action_settings: opensettings(); return true; default: return super.onoptionsitemselected(item); } } //called when user click button public void sendmessage(view view){ intent intent = new intent(this,displaymessageactivity.class); edittext edittext=(edittext)findviewbyid(r.id.edit_message); string message=edittext.gettext().tostring(); intent.putextra(extra_message,message); startactivity(intent); } private void opensearch() { //do } private void opensettings() { //do } }
- displaymessageactivity.java
package com.mycompany.myfirstapps; import android.content.intent; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.menu; import android.view.menuitem; import android.widget.textview; public class displaymessageactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intent intent= getintent(); //create text view string message=intent.getstringextra(myactivity.extra_message); textview textview= new textview(this); textview.settextsize(40); textview.settext(message); //set text view activity layout setcontentview(textview); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_display_message, 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); } }
- activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:theme="@style/customactionbartheme"> <edittext android:id="@+id/edit_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/edit_message"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onclick="sendmessage"/> </linearlayout>
- build.gradle
apply plugin: 'com.android.application' android { compilesdkversion 22 buildtoolsversion "22.0.1" defaultconfig { applicationid "com.mycompany.myfirstapps" minsdkversion 8 targetsdkversion 22 versioncode 1 versionname "1.0" } buildtypes { release { minifyenabled false proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile filetree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' }
logcat
05-14 22:57:03.139 2514-2562/android.process.acore i/contactlocale﹕ addressbook labels [en-us]: [, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, Α, Β, Γ, Δ, Ε, Ζ, Η, Θ, Ι, Κ, Λ, Μ, Ν, Ξ, Ο, Π, Ρ, Σ, Τ, Υ, Φ, Χ, Ψ, Ω, , А, Б, В, Г, Д, Ђ, Е, Є, Ж, З, И, І, Ї, Й, Ј, К, Л, Љ, М, Н, Њ, О, П, Р, С, Т, Ћ, У, Ф, Х, Ц, Ч, Џ, Ш, Щ, Ю, Я, , א, ב, ג, ד, ה, ו, ז, ח, ט, י, כ, ל, מ, נ, ס, ע, פ, צ, ק, ר, ש, ת, , ا, ب, ت, ث, ج, ح, خ, د, ذ, ر, ز, س, ش, ص, ض, ط, ظ, ع, غ, ف, ق, ك, ل, م, ن, ه, و, ي, , ก, ข, ฃ, ค, ฅ, ฆ, ง, จ, ฉ, ช, ซ, ฌ, ญ, ฎ, ฏ, ฐ, ฑ, ฒ, ณ, ด, ต, ถ, ท, ธ, น, บ, ป, ผ, ฝ, พ, ฟ, ภ, ม, ย, ร, ฤ, ล, ฦ, ว, ศ, ษ, ส, ห, ฬ, อ, ฮ, , ㄱ, ㄴ, ㄷ, ㄹ, ㅁ, ㅂ, ㅅ, ㅇ, ㅈ, ㅊ, ㅋ, ㅌ, ㅍ, ㅎ, , あ, か, さ, た, な, は, ま, や, ら, わ, #, ] 05-14 22:57:05.181 2514-2532/android.process.acore v/backupservicebinder﹕ dobackup() invoked 05-14 22:57:05.508 2514-2532/android.process.acore i/art﹕ waitforgctocomplete blocked 267.334ms cause disablemovinggc 05-14 22:57:05.619 2514-2532/android.process.acore e/dictionarybackupagent﹕ couldn't read cursor 05-14 22:57:05.722 2514-2562/android.process.acore i/art﹕ waitforgctocomplete blocked 357.236ms cause disablemovinggc 05-15 01:35:32.770 2514-2525/android.process.acore w/art﹕ suspending threads took: 57.246ms 05-15 01:35:35.813 2514-2527/android.process.acore e/strictmode﹕ resource acquired @ attached stack trace never released. see java.io.closeable information on avoiding resource leaks. java.lang.throwable: explicit termination method 'close' not called @ dalvik.system.closeguard.open(closeguard.java:184) @ android.os.parcelfiledescriptor.<init>(parcelfiledescriptor.java:180) @ android.os.parcelfiledescriptor$1.createfromparcel(parcelfiledescriptor.java:916) @ android.os.parcelfiledescriptor$1.createfromparcel(parcelfiledescriptor.java:906) @ android.app.ibackupagent$stub.ontransact(ibackupagent.java:57) @ android.os.binder.exectransact(binder.java:446) 05-15 01:35:35.925 2514-2527/android.process.acore e/strictmode﹕ resource acquired @ attached stack trace never released. see java.io.closeable information on avoiding resource leaks. java.lang.throwable: explicit termination method 'close' not called @ dalvik.system.closeguard.open(closeguard.java:184) @ android.os.parcelfiledescriptor.<init>(parcelfiledescriptor.java:180) @ android.os.parcelfiledescriptor$1.createfromparcel(parcelfiledescriptor.java:916) @ android.os.parcelfiledescriptor$1.createfromparcel(parcelfiledescriptor.java:906) @ android.app.ibackupagent$stub.ontransact(ibackupagent.java:64) @ android.os.binder.exectransact(binder.java:446) 05-15 01:35:36.578 2514-2527/android.process.acore e/strictmode﹕ resource acquired @ attached stack trace never released. see java.io.closeable information on avoiding resource leaks. java.lang.throwable: explicit termination method 'close' not called @ dalvik.system.closeguard.open(closeguard.java:184) @ android.os.parcelfiledescriptor.<init>(parcelfiledescriptor.java:180) @ android.os.parcelfiledescriptor$1.createfromparcel(parcelfiledescriptor.java:916) @ android.os.parcelfiledescriptor$1.createfromparcel(parcelfiledescriptor.java:906) @ android.app.ibackupagent$stub.ontransact(ibackupagent.java:71) @ android.os.binder.exectransact(binder.java:446)
what wrong?
move edittext inside of oncreate() method in myactivity.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edittext edittext=(edittext)findviewbyid(r.id.edit_message); getsupportactionbar().setdisplayhomeasupenabled(true); // if minsdkversion 11 or higher, instead use: //getactionbar().setdisplayhomeasupenabled(true); }
and inside sendmessage() method, remove line.
edit: seems you're extending new appcompatactivity. try changing actionbaractivity in both activities. example:
public class myactivity extends actionbaractivity {...} public class displaymessageactivity extends actionbaractivity {...}
edit: if you're planning on not using actionbaractivity, need define custom toolbar , initialze this..
toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true);
Comments
Post a Comment