Tengo un recyclerview al cual le quiero implementar un viewpager en un fragment pero al momento de implementarlo e iniciar la aplicacion se crashea y quisiera saber cual es mi problema
Mi layout:
<FrameLayout 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:background="@drawable/bgfooter"
tools:context="com.example.proyect.slider.Fragments.InicioFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/imageViewPager"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:id="@+id/SliderDots"
android:layout_below="@+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"/>
</LinearLayout>
Mi adapter:
public class AdapterViewPager extends PagerAdapter {
List<Slider> sliderList = new ArrayList<>();
Context context;
public AdapterViewPager(List<Slider> sliderList, Context context) {
this.sliderList = sliderList;
this.context = context;
}
@Override
public int getCount() {
return sliderList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.view_image, container, false);
ImageView imagePager = (ImageView) view.findViewById(R.id.imagePager);
Picasso.with(context).load(sliderList.get(position).getImagen()).into(imagePager);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}}
Mi Fragment:
public class InicioFragment extends Fragment {
RecyclerView recyclerview;
List<Recycler> listRecycler;
AdapterRecycler adapterRecycler;
ViewPager viewPager;
List<Slider> sliderList;
AdapterViewPager adapterViewPager;
LinearLayout sliderPanel;
private OnFragmentInteractionListener mListener;
public InicioFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_inicio, container, false);
viewPager = (ViewPager)view.findViewById(R.id.imageViewPager);
sliderPanel = (LinearLayout)view.findViewById(R.id.SliderPanel);
recyclerview = (RecyclerView)view.findViewById(R.id.recyclerview);
LinearLayoutManager linearLayoutManager = new
LinearLayoutManager(this.getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview.setLayoutManager(linearLayoutManager);
pedirImagen
if (isOnLine()) {
pedirImagen("http://servidor.mx/app/WebServiceRecycler.php");
pedirImagen("http://servidor.mx/app/WebServiceSlider.php");//Sera por esta dos url_
} else {
Toast.makeText(getActivity().getApplicationContext(), "No se pudo conectar", Toast.LENGTH_LONG).show();
}
return view;
}
public void cagarImagen() {
adapterViewPager = new AdapterViewPager(sliderList, getContext());
viewPager.setAdapter(adapterViewPager);
adapterRecyler = new AdapterRecyler(getContext(), listRecycler);
recyclerview.setAdapter(adapterRecyler);
recyclerview.setHasFixedSize(true);
}
public void pedirImagen(String uri){
TaskFragment taskFragment = new TaskFragment();
taskFragment.execute(uri);
}
private boolean isOnLine() {
ConnectivityManager connectivityManager = (ConnectivityManager)
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = connectivityManager.getActiveNetworkInfo();
if (network != null && network.isConnectedOrConnecting()){
return true;
}else {
return false;
}
}
private class TaskFragment extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String content = Http.getData(params[0]);
return content;
}
protected void onPostExecute(String resultado){
super.onPostExecute(resultado);
if (resultado == null){
Toast.makeText(getActivity().getApplication(),"No se pudo conectar",Toast.LENGTH_LONG);
return;
}
sliderList = ParserSlider.parse(resultado);
listRecycler = ParserRecycler.parse(resultado);
cagarImagen();
}
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}}
Error que aparece en el logcat:
06-14 11:52:16.423 24370-24370/com.example.proyect.slider E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.proyect.slider, PID: 24370
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.proyect.slider.Adapters.AdapterViewPager.getCount(AdapterViewPager.java:32)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:532)
at com.example.proyect.slider.Fragments.InicioFragment.cagarImagen(InicioFragment.java:71)
at com.example.proyect.slider.Fragments.InicioFragment$TaskFragment.onPostExecute(InicioFragment.java:148)
at com.example.proyect.slider.Fragments.InicioFragment$TaskFragment.onPostExecute(InicioFragment.java:127)
at android.os.AsyncTask.finish(AsyncTask.java:692)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:709)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
sliderList
esnull
al pasarselo como parámetro al constructor delAdapterViewPager
. Revisa quesliderList = ParserSlider.parse(resultado);
no devuelvanull
el resultado, antes de llamarcargarImagen()
. De igual formalistRecycler = ParserRecycler.parse(resultado);
al pasar el parametro en el constructor deAdapterRecyler
. Si no traen resultados, debe como minimo devolver una lista vacia.