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"
/>