我尝试将viewPager与tablayout一起使用,但是当我使用视图寻呼机设置适配器时,我收到错误"java.lang.NullPointerException:尝试调用虚方法'java.lang.Class java.lang.Object.getClass()on空对象引用"我不明白为什么会显示此错误.
view pager set Adapter:
viewPager= (ViewPager) findViewById(R.id.viewPager_id);
viewPager.setAdapter(newViewPagerAdapterUseThisClass(getSupportFragmentManager(),14));
Run Code Online (Sandbox Code Playgroud)
适配器clss:
class ViewPagerAdapterUseThisClass extends FragmentPagerAdapter {
int countTabItem;
ViewPagerAdapterUseThisClass(FragmentManager fm,int contTabItem) {
super(fm);
this.countTabItem=contTabItem;
}
@Override
public Fragment getItem(int position) {
if(position==0){
EnthusamTab enthusamTab=new EnthusamTab();
return enthusamTab;
}
return null;
}
@Override
public int getCount() {
return countTabItem;
}
}
Run Code Online (Sandbox Code Playgroud)
查看寻呼机xml文件:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tab_layout_id"
android:id="@+id/viewPager_id"/>
Run Code Online (Sandbox Code Playgroud)
错误日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:380)
at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:375)
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:103)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1248) …Run Code Online (Sandbox Code Playgroud) 我尝试在我的应用程序中构建导航抽屉,导航抽屉在以前版本的牛轧糖中工作正常但在牛轧糖导航抽屉中没有来自状态栏.我尝试了很多解决方案,但不能在牛轧糖工作,请帮助!!
这是我的activity_main.xml文件:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayoutId"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:fitsSystemWindows="true"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.pushpam.helloworld.MainActivity">
<include
layout="@layout/custom_tool_bar"
android:id="@+id/customtollbarimportid"/>
<TextView
android:textSize="80dp"
android:textColor="#E9CC0014"
android:text="I"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/customtollbarimportid"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="65dp"
android:layout_marginStart="65dp"
android:layout_marginTop="79dp"
android:id="@+id/textView" />
<TextView
android:textSize="80dp"
android:textColor="#E9CC0014"
android:text="Love"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<fragment
android:fitsSystemWindows="true"
android:id="@+id/nav_drawer_fragment_id"
android:layout_width="280dp"
android:layout_height="match_parent"
app:layout="@layout/fragment_navigationfragment"
android:layout_gravity="start"
android:name="com.example.pushpam.helloworld.navigationfragment"
tools:layout="@layout/fragment_navigationfragment" />
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
我的styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="CustomToolBarTheme">
<item name="android:textColorPrimary">@color/toolBarTextColor</item> …Run Code Online (Sandbox Code Playgroud)