Calits95
Calits95

Reputation: 21

Button on PageViewer to switch page

i acutally have a problem with using a Button on a PageViewer Fragment. The Button shall bring the first Page to the Front, when it get clicked, but instead the app crashes.

public class Results extends FragmentActivity {
public static ViewPager viewPager=null;

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.results);
    LayoutInflater inflater = LayoutInflater.from(this);
   List<View> pages = new ArrayList<View>();

    for(int i=1;i<=20;i++){
        if(i<10){
            View page = inflater.inflate(R.layout.test_fragment2, null);
            TextView textView = (TextView) page.findViewById(R.id.tv1);
            textView.setText("Page "+ i + " of 20");
            pages.add(page);
        }else{
            View page = inflater.inflate(R.layout.text_fragment, null);
            TextView textView = (TextView) page.findViewById(R.id.tv1);
            textView.setText("Page "+ i + " of 20");
            pages.add(page);
        }

    }
   MyPageAdapter pagerAdapter = new MyPageAdapter(pages);
    ViewPager viewPager = new ViewPager(this);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setCurrentItem(0);    

    setContentView(viewPager);

}
 public void jumpPage(View v){
     viewPager.setCurrentItem(0,false);
    }


}

And the Button created on my Layout XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" 
android:background="@drawable/mh4design">

<Button
android:id="@+id/btback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/custom_button"
android:onClick="jumpPage" />

<TextView
android:id="@id/tv1"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textSize="20sp" />

</RelativeLayout>

Thats what the CatLog is showing me, when I click the Button

D/AndroidRuntime(781): Shutting down VM W/dalvikvm(781): threadid=1: thread exiting with uncaught exception (group=0x40a71930) E/AndroidRuntime(781): FATAL EXCEPTION: main E/AndroidRuntime(781): java.lang.IllegalStateException: Could not execute method of the activity E/AndroidRuntime(781): at android.view.View$1.onClick(View.java:3599) E/AndroidRuntime(781): at android.view.View.performClick(View.java:4204) E/AndroidRuntime(781): at android.view.View$PerformClick.run(View.java:17355) E/AndroidRuntime(781): at android.os.Handler.handleCallback(Handler.java:725) E/AndroidRuntime(781): at android.os.Handler.dispatchMessage(Handler.java:92) E/AndroidRuntime(781): at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime(781): at android.app.ActivityThread.main(ActivityThread.java:5041) E/AndroidRuntime(781): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(781): at java.lang.reflect.Method.invoke(Method.java:511) E/AndroidRuntime(781): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) E/AndroidRuntime(781): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) E/AndroidRuntime(781): at dalvik.system.NativeStart.main(Native Method) E/AndroidRuntime(781): Caused by: java.lang.reflect.InvocationTargetException E/AndroidRuntime(781): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(781): at java.lang.reflect.Method.invoke(Method.java:511) E/AndroidRuntime(781): at android.view.View$1.onClick(View.java:3594) E/AndroidRuntime(781): ... 11 more E/AndroidRuntime(781): Caused by: java.lang.NullPointerException E/AndroidRuntime(781): at com.CaliEng.mhsetsearcher.Results.jumpPage(Results.java:55) E/AndroidRuntime(781): ... 14 more

I hope someone can help me to solve that problem

Upvotes: 0

Views: 250

Answers (1)

Arthur Grangeiro
Arthur Grangeiro

Reputation: 11

In the onCreate method you are referencing View Pager like this:

ViewPager mViewPager = new ViewPager (this);

do not do this, declare the name:

mViewPager = new ViewPager (this);

The way you did there are two ViewPagers, one that only exists in the scope of onCreate and another that has not been initialized.

Upvotes: 1

Related Questions