TT_from_KZ
TT_from_KZ

Reputation: 55

Nullpointer Exception setOnTouchListener(this)

I have an Nullpointer Exception with this code, it worked clearly before. Exception shows at mainLayout.setOnTouchListener(this), but i don't understund why. A changed some code in another activity.:

public class Assistantbuilder extends Activity implements OnTouchListener
{
    private ViewFlipper flipper = null;
    private float fromPosition;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);
        mainLayout.setOnTouchListener(this);

        flipper = (ViewFlipper) findViewById(R.id.flipper);

        LayoutInflater inflater = (LayoutInflater)         getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int layouts[] = new int[]{ R.layout.dashboard_layout, R.layout.dashboard_layout2, R.layout.dashboard_layout3, R.layout.dashboard_layout4 };
        for (int layout : layouts)
            flipper.addView(inflater.inflate(layout, null));

        Button btndimensions_rafters = (Button) findViewById(R.id.home_btn_dimensions_rafters);
        btndimensions_rafters.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                switch (arg0.getId()) {
                case R.id.home_btn_dimensions_rafters:
                    Intent i = new Intent(Assistantbuilder.this,
                            dimensions_rafters.class);
                    startActivity(i);
                    break;
                }
            }
        });
        Button btngable_roof = (Button) findViewById(R.id.home_btn_gable_roof);
        btngable_roof.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                switch (arg0.getId()) {
                case R.id.home_btn_gable_roof:
                    Intent i = new Intent(Assistantbuilder.this,
                            GableRoof.class);
                    startActivity(i);
                    break;
                }
            }
        });

    }

May be here some mistake:

    public boolean onTouch(View view, MotionEvent event)
    {
        switch (event.getAction())
        {
        case MotionEvent.ACTION_DOWN:
            fromPosition = event.getX();
            break;
        case MotionEvent.ACTION_UP:
            float toPosition = event.getX();
            if (fromPosition > toPosition)
            {
                flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.go_next_in));
                flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.go_next_out));
                flipper.showNext();
            }
            else if (fromPosition < toPosition)
            {
                flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.go_prev_in));
                flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.go_prev_out));
                flipper.showPrevious();
            }
        default:
            break;
        }
        return true;
    }
}

Thanks a lot.

Upvotes: 0

Views: 1163

Answers (1)

kabuko
kabuko

Reputation: 36312

Well, given that line is in the onCreate of your activity, this should always be valid, so the only other thing which could be null is mainLayout. Did you change your XML layout file? The runtime can't find the LinearLayout with id main_layout.

Upvotes: 1

Related Questions