java.lang.NullPointerException 空对象引用 viewpager
java.lang.NullPointerException null object reference viewpager
我在编码时遇到困难,需要帮助。
我在我的项目中使用视图绑定。
我无法绑定并且工作正常。
除了视图绑定之外,我在我的早期项目中使用了相同的代码,并且该项目工作正常。
这是我的 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>
这是我的 Java 代码
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();
}
}
这是错误列表
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.access00(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.access00(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)
在此先感谢您的帮助!
您需要添加
viewPager = findViewById<ViewPager>(R.id.pager)
这一行之前
viewPager.setAdapter(mypager_adapter);
编辑:
实际上,您似乎在尝试使用 ViewBinding 以及常规的 findViewById,如果我是您,我会坚持使用 ViewBinding 并删除此行
protected ViewPager viewPager;
那就试试这个
Homebinding.pager.setAdapter(mypager_adapter);
You haven't initialize your ViewPager.
您需要初始化您的viewPager。为此你有几个选择
在设置适配器之前使用 findViewById 进行初始化。
viewPager = findViewById(R.id.pager)
因为你正在使用视图绑定是个好主意
Homebinding.pager.setAdapter(mypager_adapter)
我在编码时遇到困难,需要帮助。 我在我的项目中使用视图绑定。 我无法绑定并且工作正常。 除了视图绑定之外,我在我的早期项目中使用了相同的代码,并且该项目工作正常。
这是我的 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>
这是我的 Java 代码
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();
}
}
这是错误列表
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.access00(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.access00(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)
在此先感谢您的帮助!
您需要添加
viewPager = findViewById<ViewPager>(R.id.pager)
这一行之前
viewPager.setAdapter(mypager_adapter);
编辑:
实际上,您似乎在尝试使用 ViewBinding 以及常规的 findViewById,如果我是您,我会坚持使用 ViewBinding 并删除此行
protected ViewPager viewPager;
那就试试这个
Homebinding.pager.setAdapter(mypager_adapter);
You haven't initialize your ViewPager.
您需要初始化您的viewPager。为此你有几个选择
在设置适配器之前使用 findViewById 进行初始化。
viewPager = findViewById(R.id.pager)
因为你正在使用视图绑定是个好主意
Homebinding.pager.setAdapter(mypager_adapter)