I am stuck while coding and need help on that.
I am using view binding in my project.
I have unable binding and that is working fine.
I have used same code in my earlier project except view binding and that project was working fine.
Here is my XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".Home">
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation_menu" />
</RelativeLayout>
Here is my Java code
package com.example.professionals;
public class Home extends AppCompatActivity {
private ActivityHomeBinding Homebinding;
protected ViewPager viewPager;
protected Mypager_Adapter mypager_adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Homebinding = ActivityHomeBinding.inflate(getLayoutInflater());
View view = Homebinding.getRoot();
setContentView(view);
Homebinding.pager.findViewById(R.id.pager);
mypager_adapter = new Mypager_Adapter(this);
viewPager.setAdapter(mypager_adapter);
Homebinding.bottomNavigation.setOnItemSelectedListener(new
NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
showMsg("Home");
break;
case R.id.profile:
showMsg("Profile");
break;
}
return false;
}
});
}
private void showMsg(String msg) {
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.show();
}
}
Here is the error list
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.professionals, PID: 17743
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.professionals/com.example.professionals.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager.widget.ViewPager.setAdapter(androidx.viewpager.widget.PagerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$1100(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5582)
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.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager.widget.ViewPager.setAdapter(androidx.viewpager.widget.PagerAdapter)' on a null object reference
at com.example.professionals.Home.onCreate(Home.java:32)
at android.app.Activity.performCreate(Activity.java:6321)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$1100(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5582)
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)
Thanks in advance for help!
2
Answers
You need to add
Before this line
Edit:
Actually it seems like you’re attempting to use ViewBinding as well as regular findViewById, if I were you I’d stick with ViewBinding and delete this line
Then try this instead
You need initialize your viewPager. For that you have couple of options
Initialize with findViewById before setting adapter.
As you are using view binding would be the good idea