Error inflating class Button Android studio - android

I am working on an app in Android Studio, and after adding buttons to MainActivity to move to other activities, despite the fact that the layout shows up properly in the editor, I get the error which will be posted at the end. There are other activities in my project which I did not include, because the error refers to activity_main.xml, but I will include them if they are necessary to solve the problem.
Here is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<ImageView
android:id="#+id/view_img_main_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/grocery_img_1" />
<Button
android:id="#+id/btn_inventory"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_below="#id/view_img_main_menu"
android:text="#string/btn_my_groceries_text"
android:theme="#style/BtnTheme" />
<Button
android:id="#+id/btn_shopping_list"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_below="#id/btn_inventory"
android:text="#string/btn_shopping_list_text"
android:theme="#style/BtnTheme" />
</RelativeLayout>
Here is MainActivity:
public class MainActivity extends AppCompatActivity {
private Button btnInventory;
private Button btnShopList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnInventory = findViewById(R.id.btn_inventory);
btnShopList = findViewById(R.id.btn_shopping_list);
btnInventory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent inventoryIntent = new Intent(MainActivity.this, Inventory.class);
startActivity(inventoryIntent);
}
});
btnShopList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent shopListIntent = new Intent(MainActivity.this, ShoppingList.class);
startActivity(shopListIntent);
}
});
}
Here is the error I get awhen I try to run the code:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grocerystatusapplication/com.example.grocerystatusapplication.MainActivity}: android.view.InflateException: Binary XML file line #15 in com.example.grocerystatusapplication:layout/activity_main: Binary XML file line #15 in com.example.grocerystatusapplication:layout/activity_main: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3344)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3488)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
at android.os.Handler.dispatchMessage(Han
E/AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:956)
Here is styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/colorLayoutBackground</item>
</style>
<style name="BtnTheme" parent="TextAppearance.AppCompat.Widget.Button.Colored">
<item name="colorButtonNormal">#color/colorViewBackground</item>
</style>
<style name="TextViewTheme" parent="">
<item name="android:background">#drawable/rounded_corners</item>
<item name="android:textColor">#android:color/white</item>
</style>
I have tried changing the attributes of the Button that the error refers to (line 15, btn_inventory), but I did not get anywhere. Did not come across this error before and don't really understand what is the problem. What can I change to make it work? After looking through posts about the same error, I did not get an answer to my question, there seemed to be a quite a few reasons this error came up.

In both Button tags change
android:theme="#style/BtnTheme"
to
style="#style/BtnTheme"

Related

Error inflating the class for toolbar

I am using the toolbar for my app it works fine in the lollipop version.But in the previous version the app gets crashed.I have tried changing the styles to
Theme.AppCompat.NoActionBar but it didn't works.
Styles:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppThemes" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
XmlToolbar:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
Logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{nidhinkumar.webviewss/nidhinkumar.webviewss.ParseMainAct}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:305)
at android.app.Activity.setContentView(Activity.java:1966)
at nidhinkumar.webviewss.ParseMainAct.onCreate(ParseMainAct.java:36)
at android.app.Activity.performCreate(Activity.java:5286)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
            
ParseMainAct:
public class ParseMainAct extends Activity{
private static String TAG = ParseMainAct.class.getSimpleName();
private Toolbar mToolbar;
private ListView listView;
private List<Message> listMessages = new ArrayList<>();
private MessageAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parseact_main);
listView = (ListView) findViewById(R.id.list_view);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
adapter = new MessageAdapter(this);
listView.setAdapter(adapter);
Intent intent = getIntent();
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String message = intent.getStringExtra("message");
Message m = new Message(message, System.currentTimeMillis());
listMessages.add(0, m);
adapter.notifyDataSetChanged();
}
private class MessageAdapter extends BaseAdapter {
LayoutInflater inflater;
public MessageAdapter(Activity activity) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listMessages.size();
}
#Override
public Object getItem(int position) {
return listMessages.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.parselist_row, null);
}
TextView txtMessage = (TextView) view.findViewById(R.id.message);
TextView txtTimestamp = (TextView) view.findViewById(R.id.timestamp);
Message message = listMessages.get(position);
txtMessage.setText(message.getMessage());
CharSequence ago = DateUtils.getRelativeTimeSpanString(message.getTimestamp(), System.currentTimeMillis(),
0L, DateUtils.FORMAT_ABBREV_ALL);
txtTimestamp.setText(String.valueOf(ago));
return view;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}
parse_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="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/my_tool_bar" />
</LinearLayout>
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
You should create your toolbar.xml inside you layout:
<RelativeLayout 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"
tools:context="xxxxxx">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:orientation="vertical">
<include
android:id="#+id/main_menu_activity_toolbar"
layout="#layout/XmlToolbar" />
And then inflate it your activity:
mToolbar = (Toolbar) findViewById(R.id.main_menu_activity_toolbar);
EDIT
Use NoActionBar in your base application theme
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
May be somewhere in your app AppTheme or 'AppThemes' has been used. you see you have give parent theme as Theme.AppCompat.Light.DarkActionBar. that means your Activity having this theme has ActionBar.
You cannot use ToolBar As long as ActionBaris there.
just Change Theme.AppCompat.Light.DarkActionBar
to Theme.AppCompat.Light.NoActionBar. and you are good to go.
Hope this will help you.
Update
Set your ToolBaras Your ActionBar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
You have to made two styles.xml file one for device below lollipop and other is styles.xml (v-21) for device lollipop and above version.
Sample of style.xml
<style name="MyAppThemes" parent="MyTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
Sample of style.xml(v-21)
<style name="MyAppThemes" parent="MyAppThemes.Base">
<item name="android:windowActionBar">false</item>
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
Now inside your AndroidMainfest.xml change application theme
android:theme="#style/MyAppThemes">
I hope it will solve your problem.

TextInputLayout: RuntimeException - Failed to resolve attribute at index 24

I keep on getting this error when I try to setErrorEnabled on my textInputLayout :
03-12 12:29:03.206 5706-5706/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 5706
java.lang.RuntimeException: Failed to resolve attribute at index 24
at android.content.res.TypedArray.getColor(TypedArray.java:401)
at android.widget.TextView.<init>(TextView.java:696)
at android.widget.TextView.<init>(TextView.java:632)
at android.widget.TextView.<init>(TextView.java:628)
at android.widget.TextView.<init>(TextView.java:624)
at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:380)
at com.bekwaai.popupwindow.RGNamePopUp$1.onClick(RGNamePopUp.java:48)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
This is the scenario - I have a textInputLayout inside a popupwindow. This is the code inside the popupwidow.
public class NamePopUp extends PopupWindow {
android.support.v7.widget.AppCompatEditText name;
TextInputLayout nameInput;
public NamePopUp(final Context context) {
super(context);
View popupView = LayoutInflater.from(context).inflate(R.layout.popup_rg_confirm, null);
nameInput = (TextInputLayout) popupView.findViewById(R.id.name_input_layout);
}
}
This is my xml for the popupwindow layout:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:theme="#style/TextLabel"
app:backgroundTint="#color/white"
app:rippleColor="#color/white"
android:id="#+id/name_input_layout"
android:layout_marginBottom="8dp"
android:paddingRight="24dp"
android:paddingLeft="24dp">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/enter_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textCursorDrawable="#drawable/color_cursor"
android:hint="#string/groupname"/>
</android.support.design.widget.TextInputLayout>
This is the style I'm using:
<style name="TextLabel" parent="TextAppearance.AppCompat">
<!-- Hint color and label color in FALSE state -->
<item name="android:textSize">20sp</item>
<item name="android:textColorHint">#color/white</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/white</item>
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/white</item>
</style>
The popupwindow is called inside a android.support.v4.app.Fragment and the fragment is inside a AppCompatActivity.
The error occurs here when the user clicks the ok button but the name edittext is blank. In other words, the user has not entered anything in the edittext and have clicked ok:
button_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (StringUtils.isNotEmpty(name.getText().toString())) {
nameInput.setErrorEnabled(false);
groupNameEvent.doEvent(name.getText().toString());
} else {
nameInput.setErrorEnabled(true); //ERROR OCCURS HERE!
nameInput.setError(context.getString(R.string.no_name));
}
}
});
How do I get this error to disappear?
Just change the base theme for TextLabel to Widget.Design.TextInputLayout
<style name="TextLabel" parent="Widget.Design.TextInputLayout">
For me a following works (https://stackoverflow.com/a/42779409/2914140):
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="textColorError">#color/design_textinput_error_color_light</item>
</style>

App name showing on top of Alert Activity popup

Im showing a popup alert activity, when select a notification from tray.
But on top it's showing the App name (MCP), with improper alignment.
I don't want the top part, just a normal dialog.
Manifest:
<activity
android:name=".activity.AlertDialogActivity"
android:configChanges="orientation|locale|screenSize|uiMode|fontScale"
android:excludeFromRecents="true"
android:theme="#style/AlertDialogTheme" />
Style:
<style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Alert activity:
public class AlertDialogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
this.setFinishOnTouchOutside(false);
String message = getIntent().getExtras().getString("message");
TextView tvPushMessage = (TextView) findViewById(R.id.tvAlertMessage);
tvPushMessage.setText(message);
Button btnPushOk = (Button) findViewById(R.id.btnAlertOk);
btnPushOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#drawable/background_round_rectangle"
android:padding="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/tvAlertMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="Message"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />;
<Button
android:id="#+id/btnAlertOk"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="#id/tvAlertMessage"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#drawable/btn_use"
android:text="#string/ok"
android:textColor="#color/white" />
</RelativeLayout>`
Also tried:
requestWindowFeature(Window.FEATURE_NO_TITLE);
and:
getWindow().setFeatureDrawableResource(Window.FEATURE_NO_TITLE, android.R.drawable.ic_dialog_alert)
before setContentView()
Just Add Following code in style.xml
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
or You can hide it Programmatically.
getSupportActionBar().hide();
Try this code (without using "android:")
<style name="Theme.MyDialog" parent="#style/Theme.AppCompat.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
I used this:
In AppCompatActivity onCreate() add following code:
setTitle("")
and Add The following code in style.xml
<style name="PopupWindowTheme" parent="#style/Theme.AppCompat.Dialog.Alert">
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">#style/AnimBottom</item>
</style>
Add requestWindowFeature in this way may be work proper.
First Call Super after add it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.add__favourite_layout);
}

android: Application crashes on toolbar overflow menu click

I'm implementing material design to my application. I replaced native ActionBar with new android Toolbar. Menu items are added, onOptionsItemSelected works perfectly. But when I click overflow icon or item with several more menu items, the application crashes.
toolbar screenshot: https://www.dropbox.com/s/smay4k1qwkd8amz/Screenshot_2015-05-29-11-31-55.png?dl=0
activity_main.xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v7.widget.Toolbar
android:id="#+id/app_bar"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#color/my_primary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
/>
</LinearLayout>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/my_primary</item>
<item name="colorPrimaryDark">#color/my_primary_dark</item>
<item name="colorAccent">#color/my_accent</item>
</style>
</resources>
MyActivity.java
public class MyActivity extends ActionBarActivity {
private Toolbar toolbar;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
menu\main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:Compat="http://schemas.android.com/apk/res/com.example.myapp">
<item
android:id="#+id/action_add"
android:title="Add"
android:icon="#android:drawable/ic_menu_add"
Compat:showAsAction="always"/>
<item
android:title="Items"
android:icon="#android:drawable/ic_menu_agenda"
Compat:showAsAction="always"
>
<menu>
<item
android:id="#+id/action_1"
android:title="item1"/>
<item
android:id="#+id/action_2"
android:title="item2"/>
</menu>
</item>
<item
android:id="#+id/action_about"
android:title="About"
android:icon="#android:drawable/ic_menu_help"
Compat:showAsAction="never"/>
</menu>
The following message is shown after the crash:
05-29 11:21:18.012 31469-31469/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.myapp, PID: 31469
java.lang.NoSuchFieldError: No static field title of type I in class Landroid/support/v7/appcompat/R$id; or its superclasses (declaration of 'android.support.v7.appcompat.R$id' appears in /data/app/com.example.myapp-1/base.apk)
at android.support.v7.internal.view.menu.ListMenuItemView.onFinishInflate(ListMenuItemView.java:89)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:814)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:370)
at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:219)
at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:153)
at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:752)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Also I am using the last version of appcompat library. I followed tutorials, googled, but nothing is found. What is the reason of this crash? Please, help.
Read here.. http://developer.android.com/guide/topics/ui/menus.html
You have just created menus, you haven't provided their functions. in other words, you just inflate the menus on screen, not provided the function what they will do if use clicks on it.
To enable them you need to implement following method, for example
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
in your mainActivity.
If you are using trasitive = false ; in build.gradle then remove it

Add onclick event listener to transparent activity

I have transparent activity using transparent theme :
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
and I want when this activity appears then the user clicked it, it finishs
the main xml file code
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:onClick="hide">
</RelativeLayout>
and the code of main activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
public void hide(){
finish();
}
but of course it doesn't work.
Your function is missing the param. It should be
public void hide(View v){ // notice the parameter it takes for the onClick
finish();
}
Notice the third requirement stated in the Button Docs
The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:
Be public
Return void
Define a View as its only parameter (this will be the View that was clicked)

Categories

Resources