App stops running after setting tablayout with ViewPager - android

I created my own TabLayout menu (from an empty Activity) with a modified SectionPagerAdapter from a TabActivity project. I only changed the getItem() method and removed the page title method.
public Fragment getItem(int position) {
switch (position)
{
case 0:
HomeActivity home = new HomeActivity();
return home;
case 1:
PayActivity pay = new PayActivity();
return pay;
case 2:
VideoActivity video = new VideoActivity();
return video;
case 3:
BillActivity bill = new BillActivity();
return bill;
default:
return null;
}
}
}
And after the execution of
TabLayout tabToolbar = (TabLayout) findViewById(R.id.tabToolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.container);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mSectionsPagerAdapter);
tabToolbar.setupWithViewPager(viewPager);
my App has been stopped.
If I comment
tabToolbar.setupWithViewPager(viewPager);
than my menu doesn't work but I can swipe between my fragments within the ViewPager.
I want to swipe with my menu button and by swipping with my mousecursor as well.
If you need some more information or resources please let me know.
Edit:
The error log shows:
09-08 06:19:05.822 4207-4207/com.example.fdai3744.t_online20 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fdai3744.t_online20, PID: 4207
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fdai3744.t_online20/com.example.fdai3744.t_online20.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
at com.example.fdai3744.t_online20.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
2nd edit:
I found out that my adapter has no "public CharSequence getPageTitle(int position)" method.

add this line of code below setupwithviewpager;
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabtoolbar));
EDIT :
i have updated the answer , you can refer this answer for tabs with icons only.
https://stackoverflow.com/a/32173670/5339829

Related

Nullpointerexception after memory fill

My app works with different listviews. Acts normally, but when memory fills (during simulation or naturally) and system dumps it, app crashes with nullpointerexception on one of the declared ListViews.
Views are declared onCreate()
a_images = (ListView) findViewById(R.id.ascii_images);
a_emotes = (ListView) findViewById(R.id.ascii_emotes);
and then called onSectionAttached()
public void onSectionAttached(int number) {
switch (number) {
case 1:
status = 1;
mTitle = getString(R.string.title_section1);
a_images.invalidateViews();
a_images.refreshDrawableState();
Here's the logcat.
05-21 20:16:54.162 11230-11230/com.joudoviny.jonda.copypasta E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.joudoviny.jonda.copypasta, PID: 11230
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joudoviny.jonda.copypasta/com.example.jonda.copypasta.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.invalidateViews()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.invalidateViews()' on a null object reference
at com.example.jonda.copypasta.MainActivity.onSectionAttached(MainActivity.java:303)
at com.example.jonda.copypasta.MainActivity$PlaceholderFragment.onAttach(MainActivity.java:530)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1363)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:3166)
at android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:181)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:319)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)
at com.example.jonda.copypasta.MainActivity.onCreate(MainActivity.java:99)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2784)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:172) 
at android.app.ActivityThread.main(ActivityThread.java:6637) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
This happens whenever I use the ListView, in this case in invalidateViews() method.

Android App crashes when run from phone but works fine in emulator

Recently my android app crash when it tested on real device. but it works fine while tested on emulator. My app has fragment that implement RecyclerView and when the item in RecyclerView clicked it will start new Detail activity, and it crash when back press button clicked. I've role back to the previous commit that works on device but it still crashing.
this the Error Log :
07-10 14:07:15.146 12250-12250/com.luckyendey.polymarkapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.luckyendey.polymarkapp, PID: 12250
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.luckyendey.polymarkapp/com.luckyendey.polymarkapp.main.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.luckyendey.polymarkapp.bonsementara.BonSementaraHeaderTabOpenFragment$LayoutManagerType.ordinal()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
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:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.luckyendey.polymarkapp.bonsementara.BonSementaraHeaderTabOpenFragment$LayoutManagerType.ordinal()' on a null object reference
at com.luckyendey.polymarkapp.bonsementara.BonSementaraHeaderTabOpenFragment.setRecyclerViewLayoutManager(BonSementaraHeaderTabOpenFragment.java:98)
at com.luckyendey.polymarkapp.bonsementara.BonSementaraHeaderTabOpenFragment.onCreateView(BonSementaraHeaderTabOpenFragment.java:60)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2900)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2207)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1326)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2900)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:603)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
at android.app.ActivityThread.access$800(ActivityThread.java:156) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:211) 
at android.app.ActivityThread.main(ActivityThread.java:5389) 
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:1020) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
This is My code :
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tab_purchase_request_open, container, false);
access_token = GlobalMethod.getPref("access_token",getContext());
presenter = new PurchaseRequestHeaderOpenPresenter(this);
presenter.getListDataPurchaseRequest(access_token,"");
presenter.getRequestType(access_token);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_purchase_request_open);
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
if (savedInstanceState != null) {
// Restore saved layout manager type.
mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
.getSerializable(KEY_LAYOUT_MANAGER); // this is return null that causing the error
}
setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_purchase_request_open);
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,R.color.colorAccent,R.color.colorPrimary);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
presenter.getListDataPurchaseRequest(access_token,"");
}
});
return view;
}
On emulator onCreateView not trigger when back press clicked on detail activity, but in device it call again.
I've been solved this problem by turning off Don't keep activities under Developer Options. I forgot that I was enabled it and now my app works great.

Communicating with fragment NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm very new to Android UI, I've been looking for a place I can learn the basic structure of UI and how communication and layouts should work. But regardless. My problem comes from me trying to change the value of a TextView within a fragment. I keep getting a nullPointerException when i try to change the value. I am using a fragment manager to refer to the fragment.
Here's the fragment code:
package com.archronix.infotainment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment1 extends android.support.v4.app.Fragment {
public TextView status;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fragment1, container,
false);
return v;
}
public static Fragment1 newInstance() {
Fragment1 f = new Fragment1();
return f;
}
public void postText(String str){
status.setText(status.getText()+"\n"+str);
}
}
And this is how i declare my fragment manager:
fragment1 = (Fragment1)
getSupportFragmentManager().findFragmentById(R.id.fragment1);
And, how I use it:
fragment1.postText("testtext");
My logcat:
05-15 10:56:42.406 4109-4109/com.archronix.infotainment E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.archronix.infotainment, PID: 4109
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.archronix.infotainment/com.archronix.infotainment.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.archronix.infotainment.Fragment1.postText(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.archronix.infotainment.Fragment1.postText(java.lang.String)' on a null object reference
at com.archronix.infotainment.MainActivity.onCreate(MainActivity.java:102)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
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:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Apologies for my newbieness. I've read 20 stack overflow posts regarding similar topics but all seem to be either outdated or not consistent. What is the best way to accomplish my task? Thank you.
status variable is not assigned to any textview.
Add the following code in onCreateView before return statement
status=v.findViewById(R.id.<nameoftextview>);
In onCreateView you should initialize your TextView like so:
status = (TextView) v.findViewById(R.id.tv_status);
If you know the text that you want to set before the FragmentTransaction you can use a Bundle (How to transfer some data to another Fragment?)

Unable to add header in mikepenz material drawer

I was using mikepenz material drawer library and was not able to add header in the material drawer. Following below is my code
In gradle I used this dependency
compile('com.mikepenz:materialdrawer:4.0.0#aar') {
transitive = true
}
Then in MainActivity.java i used the same code snippet as shown in their documentation.
AccountHeader headerresult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.sunlight_foundation)
.addProfiles(
new ProfileDrawerItem().withName("Random Name").withEmail("random#gmail.com").withIcon(getResources().getDrawable(R.drawable.sunlight_foundation)),
).withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
#Override
public boolean onProfileChanged(View view, IProfile iProfile, boolean b) {
return false;
}
}).build();
//create the drawer and remember the `Drawer` result object
final Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerresult)
.withToolbar(toolbar)
.build();
And i'm getting this error.
Process: com.example.rajni.congress, PID: 19041
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rajni.congress/com.example.rajni.congress.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mikepenz.materialdrawer.adapter.BaseDrawerAdapter.addHeaderDrawerItems(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem[])' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mikepenz.materialdrawer.adapter.BaseDrawerAdapter.addHeaderDrawerItems(com.mikepenz.materialdrawer.model.interfaces.IDrawerItem[])' on a null object reference
at com.mikepenz.materialdrawer.DrawerUtils.handleHeaderView(DrawerUtils.java:231)
at com.mikepenz.materialdrawer.DrawerBuilder.createContent(DrawerBuilder.java:1446)
at com.mikepenz.materialdrawer.DrawerBuilder.buildView(DrawerBuilder.java:1280)
at com.mikepenz.materialdrawer.DrawerBuilder.build(DrawerBuilder.java:1152)
at com.example.rajni.congress.MainActivity.onCreate(MainActivity.java:82)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method)
Any help would be highly appreciated. Thanks in advance.
Try after creating the headerresult even setting your profile as active profile.
Like this:
headerresult.setActiveProfile(headerResult.getProfiles().get(0));

Android app null error

I am creating a restaurant app that requires an expandable listview to show a menu for the waiter to take a customer's menu. The app is crashing and I think it has something to do with the code below:
public class Menu extends AppCompatActivity {
HashMap < String, List < String >> Food_menu;
List < String > Food_list;
ExpandableListView Exp_list;
FoodMenuAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
Food_menu = DataProvider.getInfo();
Food_list = new ArrayList < String > (Food_menu.keySet());
adapter = new FoodMenuAdapter(this, Food_menu, Food_list);
Exp_list.setAdapter(adapter);
}
}
When I debugged the app, it said that:
Exp_list
Food_menu
Food_list
were all coming back as null. I'm new to android studios and I'm not sure how to remedy the problem. Any help would be greatly appreciated, thanks.
Logcat:
04-11 22:05:53.841 1960-1960/com.example.ben.restaurantapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ben.restaurantapp, PID: 1960
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ben.restaurantapp/com.example.ben.restaurantapp.Menu}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
at com.example.ben.restaurantapp.Menu.onCreate(Menu.java:38)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
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:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
I think the second parameter of your FoodMenuAdapter has to be a resource layout file.
For example:
Food Food_menu = DataProvider.getInfo();
adapter = new FoodMenuAdapter(this, R.layout.your_xml_layout_here, Food_list);
Exp_list = (ExapandableListView) findViewById(R.id.exp_list);
Exp_list.setAdapter(adapter);
Where your_xml_layout_here is the layout that all the information will be displayed on. Keep in mind that an adapter is the middleman of information and what you see on your application screen.
From your logcat you can see the exact problem:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
which means that at the point you try to use setAdapter method on the Exp_List, the said variable is null.
What you can do from here on is to check if your layout activity_main actually contains an ExpandableListView with an id of exp_list (maybe post your layout?).
Your question should not be android studio null error as the null has nothing to do with Android Studio - it comes from the programming language, in this specific case - Java.
You can learn how to fix NullPointerExceptions here.

Categories

Resources