Search code examples
androidbuttonandroid-viewpagerswipeandroid-4.0-ice-cream-sandwich

Swiping in android with Buttons


I'm having trouble with this:

package com.app.BoomBase;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.ViewPager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(1);     

    }

private class MyPagerAdapter extends PagerAdapter {
    public int getCount() {
        return 3;
}

public Object instantiateItem(View collection, int position) {
    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId =0;
        switch (position) 
        {
        case 0:
                resId = R.layout.advanced;
                break;
        case 1: 
                resId = R.layout.activity_main;
                break;
        case 2:
                resId = R.layout.beginner;
                ImageButton ibDistance = (ImageButton)         findViewById(R.id.ibDistance);
                RelativeLayout RelativeB = (RelativeLayout) findViewById(R.id.RelativeB);
                break;
        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);

        return view;
   }
   @Override
     public void destroyItem(View arg0, int arg1, Object arg2) {
     ((ViewPager) arg0).removeView((View) arg2);
    }
   @Override
      public boolean isViewFromObject(View arg0, Object arg1) {
      return arg0 == ((View) arg1);
     }
   @Override
      public Parcelable saveState() {
      return null;
      }
}
}

I'm trying to implement buttons on the activity called beginner, but if i put a OnClickListner in case 2 for the button called ibDistance, the program crashes

    ibDistance.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        //Open Activity here, via Intent
                    }
                });

The Logcat file:

E/AndroidRuntime(13680): FATAL EXCEPTION: main
E/AndroidRuntime(13680): java.lang.NullPointerException
E/AndroidRuntime(13680):    at com.app.BoomBase.MainActivity$MyPagerAdapter.instantiateItem(MainActivity.java:52)
E/AndroidRuntime(13680):    at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
E/AndroidRuntime(13680):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:800)
E/AndroidRuntime(13680):    at android.support.v4.view.ViewPager.populate(ViewPager.java:991)
E/AndroidRuntime(13680):    at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
E/AndroidRuntime(13680):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
E/AndroidRuntime(13680):    at android.view.View.measure(View.java:15504)
E/AndroidRuntime(13680):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
E/AndroidRuntime(13680):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
E/AndroidRuntime(13680):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
E/AndroidRuntime(13680):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime(13680):    at android.view.View.measure(View.java:15504)
E/AndroidRuntime(13680):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
E/AndroidRuntime(13680):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(13680):    at android.view.View.measure(View.java:15504)
E/AndroidRuntime(13680):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
E/AndroidRuntime(13680):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime(13680):    at android.view.View.measure(View.java:15504)
E/AndroidRuntime(13680):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
E/AndroidRuntime(13680):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(13680):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2385)
E/AndroidRuntime(13680):    at android.view.View.measure(View.java:15504)
E/AndroidRuntime(13680):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1979)
E/AndroidRuntime(13680):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1222)
E/AndroidRuntime(13680):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
E/AndroidRuntime(13680):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
E/AndroidRuntime(13680):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526)
E/AndroidRuntime(13680):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
E/AndroidRuntime(13680):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
E/AndroidRuntime(13680):    at android.view.Choreographer.doFrame(Choreographer.java:525)
E/AndroidRuntime(13680):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
E/AndroidRuntime(13680):    at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(13680):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(13680):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(13680):    at android.app.ActivityThread.main(ActivityThread.java:4921)
E/AndroidRuntime(13680):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(13680):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(13680):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
E/AndroidRuntime(13680):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
E/AndroidRuntime(13680):    at dalvik.system.NativeStart.main(Native Method)

What can i do ? Thanks :)


Solution

  • One question I need to ask first - is the button you are trying to use part of your main activity? findViewById works by searching the view that you supplied in setContentView for the view you are looking for - in this case the button. If the button is not actually in the layout then it will not be found and would give you an error.

    Assuming the button is in your main activity layout here's what I think is going on.

    Generally you need to add the onClickListener within your onCreate method. There are ways to move the onClickListener outside onCreate by, for example, using a private class. I think you can fix this problem with the following:

    (1) Declare your button as an instance variable before your onCreate

    ImageButton ibDistance;
    

    (2) Initialize your button and set the onClickListener within your onCreate method

    ibDistance = (ImageButton) findViewById(R.id.ibDistance);
    ibDistance.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                    }
                });
    

    If you're changing the layout under case 2 or adding buttons (I can't tell what you're doing really) then you would just refer to the ImageButton you declared earlier in that section of the code.

    Another way to do this is to not use an onClickListener at all. Buttons all have an onClick XML property that allows you to specify what method to trigger without having to use an onClickListener. To use this function you declare a public method in the activity you want the button to trigger something.

    public void runThisOnClick(View v){
        //do something 
    }
    

    What goes in the commented section is what will happen when you press the button. Obviously you change the name of the method and what you place in the onClick attribute for that button to fit your needs.