when edited it's still error when edit finish .this code mainactivity
package com.example.actionbar; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.menuitem; import android.widget.toast; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); getactionbar().setdisplayhomeasupenabled(true); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } //ถ้ามีการเลือก actionเกิดขึ้น object ที่นำมาใช้จะเรียกชื่อobject นี้ว่า onoptionitemselected() public boolean onoptionitemselect(menuitem item){ //ใช้switch case ในการกำหนดว่าเมือ เลือก action นี้เเล้วทำอะไร switch (item.getitemid()){ case r.id.action_search: toast.maketext(this,"menu search selected", toast.length_short).show(); break; case r.id.action_settings: toast.maketext(this,"menu setting selected", toast.length_short).show(); break; default: break; } return true; } }
now add string string.xml
<string name="action_search">search</string>
this code in res/menu/main
<item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:name="@string/action_search" android:showasaction="ifroom"/>
this code @ manifest error @ (android:label="@string/theme.appcompat.light") .
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.actionbar" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="11" android:targetsdkversion="18" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.example.actionbar.mainactivity" android:label="@string/theme.appcompat.light" > <<<<<<<<<<<error <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
this error in r & theme.appcompat.light.
r cannot resolved variable no resource found matches given name (at 'label' value '@string/theme.appcompat.light')
how can ?? thank you
you forgot +
. change android:id="@id/action_search"
android:id="@+id/action_search"
.
and make sure have declared string name action_search
, given value search
in strings.xml
file. ex. <string name="action_search">search<string>
.
cheers.