My app crashes, trying to add a menu - java

I have added a menu in a simple app of hello world! But my app keeps crashing.
I am not adding my xml code. My xml file name is menu inside the menu folder in res.
Here is my java file.
MyActivity.java
public boolean onCreateOptionsMenu(Menu menu1){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu1); //line 19
return true;
}
Logcat
06-03 18:03:57.682 13542-13542/com.example.kaushalraj.a424 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kaushalraj.a424, PID: 13542
android.view.InflateException: Couldn't resolve menu item onClick handler action in class com.example.kaushalraj.a424.MainActivity
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:253)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:481)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:529)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127)
at com.example.kaushalraj.a424.MainActivity.onCreateOptionsMenu(MainActivity.java:19)
at android.app.Activity.onCreatePanelMenu(Activity.java:3388)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1377)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1657)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NoSuchMethodException: action [interface android.view.MenuItem]
at java.lang.Class.getMethod(Class.java:2068)
at java.lang.Class.getMethod(Class.java:1690)
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:249)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:481) 
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:529) 
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205) 
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127) 
at com.example.kaushalraj.a424.MainActivity.onCreateOptionsMenu(MainActivity.java:19) 
at android.app.Activity.onCreatePanelMenu(Activity.java:3388) 
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364) 
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93) 
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332) 
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1377) 
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1657) 
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

I don't know if this your case (and i don't have the reputation to comment) but: What app theme are you using? If you are using a theme that does not have a actionbar at the top then you will get this error when you try to attach your menu to it, as the actionbar does not exist.
You have two solutions:
Check your theme in res/values/styles.xml if you are using something like Theme.AppCompat.Light.NoActionBar you must change it to something like Theme.AppCompat.Light.DarkActionBar. This will add the actionbar to your activity.
Using the support toolbar library, add the toolbar to your activity xml layout then from your activity class get the view in OnCreate() and use setSupportActionBar(myToolbar). This will set your toolbar view as the actionbar where your menu will be attached to.

Related

Not able to intent to next layout

i'm working on my project and i created an interface of my app.
now i want that when ever i click in image view of Automation i go to my next layout. So i tried to write this code
the layout in which i want to move after clicking Automation is this :
But when i'm running app it's saying
Application error
Unfortunately,animation(name of my project) has stopped.
Logcat:
Caused by: java.lang.NullPointerException
at com.example.animation.dashboard.onCreate(dashboard.java:25)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
at android.app.ActivityThread.access$600(ActivityThread.java:156) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:153) 
at android.app.ActivityThread.main(ActivityThread.java:5336) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
at dalvik.system.NativeStart.main(Native Method) 
What's mistake i'm making here ? i'm new to android so kindly help me out here.
Your view variable is not defined and you are trying to use it as it is null. But on the another hand, I dont think you need this, just go with findViewById without this view
It looks like view_rooms is not an activity - the second argument in your intent needs to be the context of the destination.
So if you want to go to new activity change your intent like this:
startActivity(new Intent(dashboard.this, SecondActivity.class));
You are using this in the dashboard activity line number 25
imgauto = view.findViewById(R.id.autoimg)
while your variable view is empty you have not assigned it something and also you are in activity so use it directly like this
imgauto = findViewById(R.id.autoimg)
try this and i am sure it will work perfectly.

NullPointerException when using getSupportActionBar [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am trying to implement a simple navigation drawer in my Android app but I am not able to solve a certain bug.
Attempting to run an app throws a NullPointerException:
Process: com.emertonalex.whiskynotes04, PID: 22225
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emertonalex.whiskynotes04/com.emertonalex.whiskynotes04.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at com.emertonalex.whiskynotes04.MainActivity.setupToolbar(MainActivity.java:320)
at com.emertonalex.whiskynotes04.MainActivity.onCreate(MainActivity.java:97)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
As far as I see it the getSupportActionBar() function call that messes stuff up. I tried covering the call with the "assert" in setupToolbar() and it seems like it passes that stage and fails at the
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The pieces of code themselves that call the getSupportActionBar() are these:
In MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_notes);
...
setupToolbar();
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
...
}
And
void setupToolbar(){
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
I am new to Android/Java dev and would be very appreciative of any help.
Also, it would be great if you tell me why this error appears, to begin with, so I can avoid it in the future.
Thank you
Changing from Support Action Bar to ActionBar should fix the issue:
getActionBar().setDisplayHomeAsUpEnabled(true);

Error with my Activity, FATAL EXCEPTION: main [duplicate]

This question already has answers here:
Your content must have a ListView whose id attribute is 'android.R.id.list'
(7 answers)
Closed 6 years ago.
I've got a problem when trying to run my app in Android Studio. I get a
FATAL EXCEPTION: main
more precisely there seems to be a problem at:
dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)
which is:
setContentView(R.layout.activity_chords_list);
here is the ChordsListActivity class:
import android.app.ListActivity;
import android.os.Bundle;
public class ChordsListActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chords_list);
String[] accordi = new String[] {"Do maggiore", "Re maggiore", "Si maggiore"};
String[] note = new String[] {"note: Do, Mi, Sol", "note: La, Re, Fa#", "note: Fa#, Si, Re#"};
int[] immagini = new int[] {R.drawable.do_maggiore, R.drawable.re_maggiore, R.drawable.si_maggiore};
CustomListAdapter adapter = new CustomListAdapter(getApplicationContext(), accordi, note, immagini);
setListAdapter(adapter);
}
}
I was kinda able to solve it by extending Activity instead of ListActivity but then I cannot use the setListAdapter() method.
I'd really appreciate if someone could help me fix the code!
please let me know if you need me to post the custom Adapter or any other file.
Edit: added the whole stack trace
FATAL EXCEPTION: main
Process: dancam.com.chords, PID: 3080
java.lang.RuntimeException: Unable to start activity ComponentInfo{dancam.com.chords/dancam.com.chords.ChordsListActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:398)
at android.app.Activity.setContentView(Activity.java:2166)
at dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
This is problem with ListView.
Go to R.layout.activity_chords_list and add <ListView> and set id android:id="#android:id/list"
Like Exception says: 'Your content must have a ListView whose id attribute is 'android.R.id.list'
It should works if rest of the code is ok.
When you extend from ListActivity, it already has ListView, but Activity doesn't. You have to create list inbActivity's layout- better use RecyclerView instead of List view. In your activity create RecyclerView reference, initialize it in onCreate. Create adapter for your data in activity (onCreate method). Finnaly, set adapter to listview reference. In order to modify data in a list, set new data in adapter and call .notifyDatasetChange () on it. Notice, for RecyclerView you will also need LayoutManager (see docs)

Search Widget, NullPointer?

HI'm having an issue implementing the search widget in my application. It seems to not be able to find the "actionview" of the menu item, but it is finding the item just fine.
I've looked around for answers and haven't seen a clear cut solution.
Here's the menu I'm declaring in XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/group_search_box"
android:title="#string/search_label"
android:icon="#drawable/ic_action_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
And here is how it is implemented.
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.group_search_box);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
Here are the libraries I'm adding, maybe I'm adding an incorrect one?
compile 'com.android.support:support-v4:+'
compile 'com.android.support:appcompat-v7:+'
and finally here is the crash logs
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
at com.example.myapp.fragments.GroupFragment.onCreateOptionsMenu(GroupFragment.java:88)
at android.app.Fragment.performCreateOptionsMenu(Fragment.java:1780)
at android.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1927)
at android.app.Activity.onCreatePanelMenu(Activity.java:2539)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
I've followed the docs to a T and still can't figure out the issue.
I faced the same issue today and got rid of the confusion as follows,
I am also using the following dependencies,
compile 'com.android.support:support-v4:+'
compile 'com.android.support:appcompat-v7:+'
Which means I am extending the activity class from AppCompatActivity rather than Activity class.
So in this code,
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
this, SearchView should be, android.support.v7.widget.SearchView not android.widget.SearchView. This is the reason you got,
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
And also,
<item
android:id="#+id/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#android:drawable/ic_search_category_default"
android:title="Search"
app:showAsAction="always|collapseActionView" />
Notice that,
it is, app:actionViewClass NOT android:actionViewClass
I had been facing the same problem and none of the solutions provided in the answers worked for me. But this change in the code worked for me.
Use
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/group_search_box"
android:title="#string/search_label"
android:icon="#drawable/ic_action_action_search"
appcompat:showAsAction="ifRoom|collapseActionView"
appcompat:actionViewClass="android.support.v7.widget.SearchView" />
Notice the change, "app:actionViewClass" has been changed to " appcompat:actionViewClass".
Worked good for me, hope it works for you too.
Check the menu Folder in res.
in My case Two Files are created
First original and 2nd Auto Create with 21.
Delete the other file issue resolve if Duplicate happen
You should remove getActivity() from searchManager like below,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
MenuItem searchItem = menu.findItem(R.id.searchMenuItem);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}

Android replace fragment doesn't hide underlying fragment

I have the following problem:
My app consists of several fragments that are dynammicly added. There is one fragment with a push button and an textedit (called 'fragA').
If I click the push button I want to show an different fragment with some text (called 'fragB'). I do this with the following code (in fragA class):
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Fragment howFragment = new HowFragment();
FragmentTransaction transaction = null;
transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.flQuestion, howFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
Now the problem is this:
When i push the button on fragA the fragment (fragB) is created and show on the screen but when I push on the location where the pushbutton was on fragA it makes an new fragment (fragB). Also if i push where the textedit on fragA was located it opens an keyboard on fragB..
It looks like FragB is just overlaying fragA without replacing it.
I also want to achieve that when i swype to the next fragment that fragB is removed and fragA is just showed normally (state when not pressed button)
Update #
When trying to add and remove this is the following logcat output:
FATAL EXCEPTION: main
java.lang.IllegalArgumentException: No view found for id 0x7f090015 (com.example.eindwerkappv1:id/flQuestion) for fragment HowFragment{419c93c8 #3 id=0x7f090015}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
I would guess your fragA fragment is defined in your XML layout?
According to the documentation, this is what happens when you use the <fragment> tag:
the system inserts the View returned by the fragment directly in place
of the element.
This is why you cannot remove the previous fragment, as it does not exist.
If you want to changes fragment from code, you have to add the first fragment from code too.
You need to use a container like a FrameLayout, and add the first fragment to this container in the onCreate() of your activity, using FragmentTransaction.add().
Then FragmentTransaction.replace() method should work.
So why don't you just call the .remove() on the EditText Fragment and .add() the new TextView Framgent instead on osing the .replace()
do some thing like this:
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(addCommentFragment)
.add(R.id.containerForFragments, commentFragment, "comment"+
String.valueOf(numOfComments)).commit();
this worked for me.
I found the solution now.
I still use the replace method but I had to catch the ontouch event in the new fragment:
Fragment over another fragment issue
See previous link for the answer

Categories