'Display picture slider in fragment using
This is my MainFragment
public class MainFragment extends Fragment {
ViewPager viewPager;
CustomSwipeAdapter adapter;
public MainFragment() {
// Required empty public constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
viewPager = (ViewPager) getView().findViewById(R.id.view_pager);
adapter = new CustomSwipeAdapter(this.getActivity());
viewPager.setAdapter(adapter);
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
This is my CustomSwipeAdapter
package my.edu.tarc.hilti;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class CustomSwipeAdapter extends PagerAdapter {
private int[] image_resource = {R.drawable.sample1, R.drawable.sample2, R.drawable.sample3, R.drawable.sample4};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomSwipeAdapter(Context ctx) {
this.ctx = ctx;
}
@Override
public int getCount() {
return image_resource.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == (LinearLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
ImageView imageview = (ImageView) item_view.findViewById(R.id.image_view);
TextView textView = (TextView) item_view.findViewById(R.id.image_count);
imageview.setImageResource(image_resource[position]);
textView.setText("" + position);
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
This is my SwipeLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/image_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Hello World"
android:textSize="25dp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="25dp"
/>
<ImageView
android:id="@+id/image_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="25dp"
/>
</LinearLayout>
My Logcat is
12-13 10:19:14.866 9070-9070/? I/SELinux: Function: selinux_android_load_priority [0], There is no sepolicy file.
12-13 10:19:14.891 9070-9070/? I/SELinux: Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_GT-N7100_4.4.2_0040
12-13 10:19:14.891 9070-9070/? I/SELinux: selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
12-13 10:19:14.891 9070-9070/? D/dalvikvm: Late-enabling CheckJNI
12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti W/dalvikvm: VFY: unable to resolve interface method 17990: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti W/dalvikvm: VFY: unable to resolve interface method 17994: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti W/dalvikvm: VFY: unable to resolve virtual method 436: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti W/dalvikvm: VFY: unable to resolve virtual method 458: Landroid/content/res/TypedArray;.getType (I)I
12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-13 10:19:15.286 9070-9070/my.edu.tarc.hilti D/AndroidRuntime: Shutting down VM
12-13 10:19:15.286 9070-9070/my.edu.tarc.hilti W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41c0fc08)
12-13 10:19:15.286 9070-9070/my.edu.tarc.hilti E/AndroidRuntime: FATAL EXCEPTION: main
Process: my.edu.tarc.hilti, PID: 9070
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.edu.tarc.hilti/my.edu.tarc.hilti.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at my.edu.tarc.hilti.MainFragment.onCreateView(MainFragment.java:27)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177)
at android.app.Activity.performStart(Activity.java:5461)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
The problem I am facing now is, it have the error at the MainFragment, I am having a viewPager in the .xml file and I used fragment (MainFragment) to display out my picture slider, but I encounter the problem, 3 line below is the problem, I have no idea to solve this problem.
viewPager = (ViewPager) getView().findViewById(R.id.view_pager);
adapter = new CustomSwipeAdapter(this.getActivity());
viewPager.setAdapter(adapter);
Solution 1:[1]
You problem is here viewPager = (ViewPager) getView().findViewById(R.id.view_pager);
You try to manipulate UI elements in onCreateView, before UI is actually inflated.
To fix the issue, you have to first inflate the view, and only after that find your viewpager there. See fixed MainFragment class
public class MainFragment extends Fragment {
ViewPager viewPager;
CustomSwipeAdapter adapter;
public MainFragment() {
// Required empty public constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
viewPager = (ViewPager) view.findViewById(R.id.view_pager);
adapter = new CustomSwipeAdapter(this.getActivity());
viewPager.setAdapter(adapter);
return view;
}
}
To make sure that it works - I uploaded fixed version into my Dropbox - feel free to download it!
Though, I'd recommend to take a look also on this SO question
I hope, it helps!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
