The normal button works fine, but clicking the three dots button makes an exception
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
my xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:icon="#drawable/refresh"
android:id="#+id/action_main_refresh"
app:showAsAction="always"
android:title="#string/refresh" />
<item
android:id="#+id/action_main_search"
android:title="#string/search"
android:icon="#drawable/seach"
app:showAsAction="always"/>
<item
android:id="#+id/action_main_top"
android:title="#string/sort_top"
app:showAsAction="never" />
</menu>
Logs
06-22 23:12:49.481 1289-1289/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at android.support.v7.view.menu.ListMenuItemView.setTitle(ListMenuItemView.java:127)
at android.support.v7.view.menu.ListMenuItemView.initialize(ListMenuItemView.java:113)
at android.support.v7.view.menu.MenuAdapter.getView(MenuAdapter.java:100)
at android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:160)
at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:153)
at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)
at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)
at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:803)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
click listener
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//Log.i(TAG, "sort URL: " + makeUrlCustom(item.getItemId()));
switch(item.getItemId()){
case R.id.action_main_refresh:
if(!adapter.isEmpty()) adapter.clear();
noData.setVisibility(View.GONE);
get(url, null);
return true;
case R.id.action_main_search:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText dialogText = new EditText(this);
alert.setView(dialogText);
alert.setTitle("Search subreddit");
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
url = dialogText.getText().toString();
if(!adapter.isEmpty()) adapter.clear();
noData.setVisibility(View.GONE);
Log.i(TAG, "url from searchText is: " + url);
if(url.equals("") || url == null){
Toast.makeText(MainActivity.this, "Please insert valid subreddit", Toast.LENGTH_SHORT).show();
}else{
get(url, null);
}
}
});
// create alert dialog
AlertDialog alertDialog = alert.create();
// show it
alertDialog.show();
return true;
case R.id.action_main_top:
Log.i(TAG, "sort top");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
clicking on the menu overflow causes npe (in this case, the action_main_top).
clicking on the normal menu (refresh & search) works fine.
after doing an exhaustive research, I found this answer here: App Crashes on Clicking Options
That answer solved my issue, aparently the issue is only for the Gradle version 2.3.3 and you need to downgrade to the 2.2.3 in order to fix it. (Hope that soon fix this bug to upgrade)
Here I paste the solution provided by Alex Bouroff
The solution that I have found was downgrading Gradle plugin from 3.3 down to 2.14.1
1) Replace the plugin version in top-level build.gradle:
dependencies {
//REPLACED 2.3.3->2.2.3(MENU ISSUE)
//classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:2.2.3'
...
}
If you use a wrapper 2) delete the /gradle/wrapper/gradle-wrapper.jar
3) replace the contents of /gradle/wrapper/gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
build-> clean project -> rebuild project -> the crash when acessing three-dot upper-right corner menu had gone.
Related
I'm having trouble converting some of my old Robolectric 3.x tests to the new style (also converted to AndroidX at the same time).
My setup:
- Android Gradle Plugin 3.2.1
- Robolectric 4.1
Some relevant config (gradle.properties):
android.useAndroidX=true
android.enableJetifier=false
Note, I am not using android.enableUnitTestBinaryResources (as it does not work on Windows in this version of the Android Gradle Plugin).
My fragment (snippet):
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
logger.debug(String.format("onCreateOptionsMenu(menu=[%s], inflater=[%s])", menu, inflater));
inflater.inflate(R.menu.key_details_actions, menu);
key_details_actions.xml (full):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="#+id/action_handin"
android:icon="#drawable/ic_return_key_wrapper"
android:title="#string/action_key_hand_in"
android:visible="false"
app:showAsAction="ifRoom|withText"
/>
</menu>
ic_return_key_wrapper.xml (full):
<?xml version="1.0" encoding="utf-8"?>
<!-- https://stackoverflow.com/questions/35813199/how-can-i-use-vectordrawable-with-the-android-toolbar -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_return_key"/>
</selector>
My test does the following (crashes):
final KeyDetailsActivity activity = Robolectric.buildActivity(KeyDetailsActivity.class)
.create(instanceState).start().get();
activity.getSupportFragmentManager().beginTransaction()
.add(R.id.container_key_fragment_details, fragment).commit();
final Context context = fragment.requireContext();
final Menu menu = new RoboMenu(context);
fragment.onCreateOptionsMenu(menu, new MenuInflater(context));
Stacktrace:
android.content.res.Resources$NotFoundException: File D:\Git\xxx\application\build\intermediates\merged-not-compiled-resources\debug\drawable\ic_return_key_wrapper.xml from drawable resource ID #0x7f0800d5
at android.content.res.Resources.loadDrawable(Resources.java:2096)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:283)
at org.robolectric.internal.bytecode.ShadowImpl.directlyOn(ShadowImpl.java:56)
at org.robolectric.shadow.api.Shadow.directlyOn(Shadow.java:56)
at org.robolectric.shadows.ShadowResources.loadDrawable(ShadowResources.java:231)
at android.content.res.Resources.loadDrawable(Resources.java)
at android.content.res.Resources.getDrawable(Resources.java:700)
at org.robolectric.fakes.RoboMenuItem.setIcon(RoboMenuItem.java:107)
at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:399)
at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:451)
at android.view.MenuInflater.parseMenu(MenuInflater.java:188)
at android.view.MenuInflater.inflate(MenuInflater.java:110)
You also should put to android DSL next lines:
testOptions {
unitTests {
returnDefaultValues = true
includeAndroidResources = true
}
}
I want to place a dropdown list on the right. Can I do this in a standard spinner?
I think Image represented is PopUp menu, To achieve the same see the code below,
To define popup menu, we need to create a new folder menu inside of our project resource directory (res/menu/) and add a new XML (popup_menu.xml) file to build the menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/first_item"
android:title="first" />
<item android:id="#+id/second_item"
android:title="second" />
<item android:id="#+id/third_item"
android:title="third" />
</menu>
In your Activity,
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v, Gravity.RIGHT);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
}
Now Override onMenuItemClick to provide functions to each item.
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.first_item:
// do your code
return true;
case R.id.second_item:
// do your code
return true;
case R.id.third_item:
// do your code
return true;
default:
return false;
}
I am trying to create a popup menu, but i am getting this error, saying "unknown member 'menu' of com.popup.trial.R
What should I do?
my xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/one"
android:title="One"/>
<item
android:id="#+id/two"
android:title="Two"/>
</menu>
and my java:
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, settings2);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
The R class is code-generated for you by the Android build tools when you build your project.
Clean your project or Build your project
reference : Beginner Android why is "id" is "Unknown member" with R.id?
follow this link for other example. : https://www.javatpoint.com/android-popup-menu-example
I've had problems with my app and eclipse so I saved my code, deleting eclipse, and re-downloaded and extracted. Immediately after I added the XML file to res/menu, I got a windows message saying aapt.exe has stopped working; I'd been getting this message constantly before. I've researched it before so I know that if an XML file isn't written correctly, aapt.exe will keep crashing. Only thing is, I don't see the problem with the file.
createlgmenu.xml (used as a popup menu for a button event):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/Create_List"
android:title="#string/Create_List"/>
<item
android:id="#+id/Create_Food_Group"
android:title="#string/Create_Food_Group"/>
</menu>
|
|
Other files:
I have practically no code in my mainactivity.java:
package com.example.groceryrunner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onCreateLGClick(View v) {
final int id = v.getId();
switch (id) {
case R.id.CreateLG:
//findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGPopup(v);
break;
/*case R.id.ListsButton:
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGMenu(v);
break;*/
}
}
public void createLGPopup(View v) {
PopupMenu LGMenu = new PopupMenu(this, v);
LGMenu.getMenuInflater().inflate(R.menu.createlgmenu, LGMenu.getMenu());
LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
String choice = new String((String) item.getTitle());
if (choice == "Create_List") {
//createListDialog();
}
else if (choice == "Create_Group") {
//createListDialog();
}
return false;
}
});
LGMenu.show();
}
}
Only one button so far in my activity_main.xml:
<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" >
<Button
android:id="#+id/CreateLG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="37dp"
android:layout_marginTop="21dp"
android:text="+"
android:textSize="40sp" />
</RelativeLayout>
In case you want to see my menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
If you are using the support library and a library project, then you need a custom namespace for the "showAsAction" tag for older platform versions.
So change this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
To this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
custom:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
Note that "custom" can be anything you want.
I think I've found my solution.
I had copied the xml and changed the items originally myself. So I deleted it and created it through Eclipse (right click on the res/menu folder > New > Other > Android XML File > Menu). I no longer seem to be getting the appt.exe error. I then added the items using Eclipse instead of writing or copying them myself.
The only difference in the two files is that the old one ended each item with this:
\>
And the one created by Eclipse uses this at the end of an item declaration:
></item>
As for Eclipse not recognizing my xml files, AdamM helped me find the solution to that problem as well:
1) I deleted the R import
2) Commented out the lines that were giving me errors in recognizing my xml files
3) Restarted Eclipse & Built All
Menu Items getting disappeared in my app.
Initially i created menu items as follow. but menu getting disappeared in some android versions.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHelper tabHelper = getTabHelper();
CompatTab menuTab = tabHelper.newTab("menu").setText(
R.string.tab_section1).setIcon(R.drawable.home_icon).setTabListener(
new InstantiatingTabListener(this, MenuFragment.class));
tabHelper.addTab(menuTab);
CompatTab favTab = tabHelper.newTab("favourites").setText(
R.string.tab_section2).setIcon(R.drawable.favourites_icon).setTabListener(
new InstantiatingTabListener(this, FavouritesFragment.class));
tabHelper.addTab(favTab);
}
THIS ABOVE CODE WORKS FINE IN LATEST ANDROID 4.1.3 BUT FAILS TO DISPLAY IN 4.0.1 and 4.0.3
SEEMS TO BE SOME LAYOUT ISSUE.
TO RESOLVE i added menu xml as follow
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuhome"
android:icon="#drawable/map_icon"
android:title="#string/tab_section1"
android:orderInCategory="1"
android:showAsAction="always|withText" />
<item android:id="#+id/menumap"
android:icon="#drawable/map_icon"
android:title="#string/tab_section2"
android:orderInCategory="2"
android:showAsAction="always|withText" />
and added onOptionItemSelected as follow
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.menuhome:
finish();
return true;
case R.id.menumap:
//ERROR
//HOW CAN I CALL FRAGMENT HERE.?
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}