I tried to display options menu in FragmentActivity, and run on Android 5, but the option menu doesn't shown!
My case is:
Min Sdk Version: API 16: Android 4.1 (Jelly Bean)
Target Sdk Version: API 23; Android 6.0 (Marshmallow)
Compile Sdk Version: API 23: Android 6.0 (Marshmallow)
Build Tools Version: 23.0.1
values/styles.xml
menu/activity_main.xml
MainActivity.java
package com.blogspot.android_er.testfragmentactivity;
import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.view.Menu;import android.view.MenuItem;import android.widget.Toast;public class MainActivity extends FragmentActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch(item.getItemId()){case R.id.option1:Toast.makeText(MainActivity.this, "Option 1", Toast.LENGTH_LONG).show();return true;case R.id.option2:Toast.makeText(MainActivity.this, "Option 2", Toast.LENGTH_LONG).show();return true;case R.id.option3:Toast.makeText(MainActivity.this, "Option 3", Toast.LENGTH_LONG).show();return true;}return super.onOptionsItemSelected(item);}}
To fix it:
(approach 1)
Edit MainActivity.java, extends ActionBarActivity (deprecated) or AppCompatActivity, instead of FragmentActivity.
Refer to the document of FragmentActivity, If you want to implement an activity that includes an action bar, you should instead use the ActionBarActivity class, which is a subclass of this one, so allows you to use Fragment APIs on API level 7 and higher.
And, ActionBarActivity is deprecated, so you should use AppCompatActivity instead.
(approach 2)
Alternatively, edit values/styles.xml, set parent of
0 تعليقات