1

i want to create a viewpager with one layout that contains a listview but i'm always getting this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.Join_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

The viewpager works fine without the listview, and the listview works fine without the viewpager. I know the problem, i'm using "setContentView" with a different layout where my listview is, but i don't know how to solve it, please help, here is the code:

public class Join_activity extends AppCompatActivity {

ListView lv_zona;
ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.join_pager);//here is the problem
    //my listview is in join3.xml
    //ViewPager
    viewPager = (ViewPager) findViewById(R.id.viewpaginador);
    viewPager.setAdapter(new Join_Adapter(this));

    //ListView
    lv_zona = (ListView)findViewById(R.id.lv_zona);
    lv_zona.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,new String[]{"A1","B1","C1","D1"}));


   viewPager.addOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
        @Override
        public void onPageSelected(int position) {}
        @Override
        public void onPageScrollStateChanged(int state){}
    });

}//end oncreate


}//end class

*

public class Join_Adapter extends PagerAdapter {

private Context mContext;
public Join_Adapter(Context context) {
    mContext = context;
}

@Override
public Object instantiateItem(ViewGroup coleccion, int posicion) {
    ModelObject modelObject = ModelObject.values()[posicion];
    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup mlayout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), coleccion, false);
    coleccion.addView(mlayout);
    return mlayout;
}

@Override
public void destroyItem(ViewGroup collection, int position, Object view)   
{collection.removeView((View) view);}

@Override
public int getCount() 
{return ModelObject.values().length;}

@Override
public boolean isViewFromObject(View view, Object object) 
{return view == object;}

@Override
public CharSequence getPageTitle(int position) {
    ModelObject customPagerEnum = ModelObject.values()[position];
    return mContext.getString(customPagerEnum.getTitleResId());
}
}

My files: My_files.jpg

My xmls:

***join3.xml - Here is my ListView***
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lv_zona"
            android:choiceMode="singleChoice" />


    </LinearLayout> </LinearLayout>

*

***join_pager.xml - my main layout***
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Paginador">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView2" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpaginador"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

zgluis
  • 3,021
  • 2
  • 17
  • 22

2 Answers2

0

You should use fragments for this. You are using a view pager. Now you should display the list inside a fragment that you will load in the activity. This answer shows how to do that. Your fragment xml will contain the listView. And the getItem of the adapter will return the fragment.

Community
  • 1
  • 1
varunkr
  • 5,364
  • 11
  • 50
  • 99
  • Thank you! it worked, now I'm trying to implement some editTexts in my fragments and gather all their values with one button in one final fragment, do you have any idea? I'm getting some NullPointerExceptions – zgluis May 14 '16 at 00:59
  • @user3891850 Null pointer exceptions are trivial. You should post a new question, definitely someone will answer, else I will answer !! – varunkr May 14 '16 at 02:37
0

The reason of NullPointerException is the list activity is not in the main layout, so, findViewById returns null.

To put a listview in a ViewPager, see: https://stackoverflow.com/a/12535150/189961

Community
  • 1
  • 1
llj098
  • 1,404
  • 11
  • 13