gpt4 book ai didi

Android - 空对象引用上的 ViewPager 设置适配器

转载 作者:行者123 更新时间:2023-11-29 00:10:40 24 4
gpt4 key购买 nike

这是基本的错误描述(完整的描述在底部)

Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference

at mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

(抽屉和工具栏相关代码已删除)

MainActivity.java

//variables
private ViewPager mViewPager;
private SlidingTabLayout mSlidingTabLayout;

//onCreate

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
mSlidingTabLayout.setViewPager(mViewPager);

}

//PagerAdapter

public class MyPagerAdapter extends FragmentPagerAdapter {
String[] tabs;

public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs = getResources().getStringArray(R.array.tabs);
}

@Override
public Fragment getItem(int position) {
MyFragment myFragment = MyFragment.getInstance(position);
return myFragment;
}

@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}

@Override
public int getCount() {
return 3;
}
}

//MyFragment

public static class MyFragment extends Fragment {
private TextView textView;

public static MyFragment getInstance(int position) {
MyFragment myFragment = new MyFragment();
Bundle bundleArgs = new Bundle();

bundleArgs.putInt("position", position);
myFragment.setArguments(bundleArgs);
return myFragment;
}

//OnCreateView

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_my, container, false);
textView = (TextView) layout.findViewById(R.id.position);
Bundle bundleValues = getArguments();
if(bundleValues!=null){
textView.setText("The page currently selected is" + bundleValues.getInt("position"));
}
return layout;
}

activity_main.xml

<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.company.name.mskv4.MainActivity">

<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />

<com.piserve.geejo.mskv4.tabs.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.piserve.geejo.mskv4.tabs.SlidingTabLayout>

<com.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>

</LinearLayout>

<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.piserve.geejo.mskv4.NavigationDrawerFragment"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

fragmentmy.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/position"
android:layout_width="match_parent" android:layout_height="match_parent"
android:textColor="@color/color_primary_text"
android:textSize="20sp"
android:gravity="center"
android:text="The page number is 1">

</TextView>

strings.xml

<resources>
<string name="app_name">MSK v4</string>
<string name="title_activity_main">MyServiceKart</string>

<string-array name="tabs">
<item>Search</item>
<item>All</item>
<item>Featured</item>
</string-array>
</resources>

更多错误说明

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.name.mskv4/com.company.name.mskv4.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter (android.support.v4.view.PagerAdapter)' on a null object reference

最佳答案

主 Activity

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), getApplicationContext()));

寻呼机适配器

public MyPagerAdapter(FragmentManager fm, Context context) {
super(fm);
tabs = context.getResources().getStringArray(R.array.tabs);
}

试试吧..

关于Android - 空对象引用上的 ViewPager 设置适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30425205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com