-1

I am working on View Flipper in android . while compiling it does not showing any error . but as soon as application starts in my mobile Devices , it crashes. Since i'm very beginner in Android and does not have enough knowledge. Report bug shows Null Pointer Exception to invoke viewFlipper.getDisplayChild() Main java file codes are

package com.example.joshiyogesh.classapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {

    public ViewFlipper viewFlipper;
    public float lastX;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewFlipper = (ViewFlipper)findViewById(R.id.view_flipper);
    }


    public boolean onTouchEvent(MotionEvent touchEvent){


        switch (touchEvent.getAction()){
            case MotionEvent.ACTION_DOWN:
            {
                lastX = touchEvent.getX();
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                float currentX = touchEvent.getX();

                if (lastX<currentX){
                    if(viewFlipper.getDisplayedChild() == 0)
                        break;
                    viewFlipper.setInAnimation(this,R.anim.in_from_left);
                    viewFlipper.setOutAnimation(this,R.anim.out_from_right);
      //show next Screen
                    viewFlipper.showNext();
                }
                if(lastX>currentX){
                    if(viewFlipper.getDisplayedChild()==1)
                        break;
                    viewFlipper.setInAnimation(this,R.anim.in_from_right);
                    viewFlipper.setOutAnimation(this,R.anim.out_from_left);
                    viewFlipper.showPrevious();
                }

               break;
            }
        }


        return false;
    }
}

ViewFlipper's file codes are

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

    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_flipper"
        android:layout_margin="6dip">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical"
            >
    <ImageView
    android:layout_width="450dp"
    android:layout_height="450dp"
    android:layout_marginTop="15dp"
    android:id="@+id/image_view1"
    android:src="@drawable/images"/>
        </LinearLayout>



    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical"
        >
        <ImageView
            android:layout_width="450dp"
            android:layout_height="450dp"
            android:layout_marginTop="15dp"
            android:id="@+id/image_view2"
            android:src="@drawable/large"/>
    </LinearLayout>




    </ViewFlipper>
</LinearLayout>

Any help would be Thankful.Thanks in advance.

update Report Bug shown by devices

Joshi Yogesh
  • 142
  • 1
  • 12

1 Answers1

0

When using onTouchEvent the activity will send directly to it if no other functions invoke it first (ontouch listener onclick listener), you got two options

Option 1

Check if viewFlipper is null before using

Option 2 Use ontouchlistener in oncreate after findviewbyid to the flipper

viewFlipper.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){

        // Do what you want
            return true;
        }
        return false;
    }
});
yanivtwin
  • 617
  • 8
  • 32