2

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 :)

Eliah Kagan
  • 1,704
  • 3
  • 22
  • 38
Lasse
  • 597
  • 2
  • 10
  • 33

1 Answers1

2

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.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46
  • The button is located in the view of beginner.. that was why i was trying to put the OnClickListner in case 2. – Lasse Apr 03 '13 at 17:13
  • if you put the OnClik inside of this case, (case2) it says that "void" is an invalid type for the variable.. – Lasse Apr 03 '13 at 17:36
  • Right - it is void because findViewById is looking inside the view that you supplied with setContentView - not the view for beginner. Is beginner a seperate activity? A new layout? how is it functioning in the app? – Rarw Apr 03 '13 at 17:41
  • Yep, the beginner is a new activity, with the layout of beginner. As it is right now, i think MainActivity only switch layout, but not the activity. – Lasse Apr 03 '13 at 17:45
  • Ok - to switch to the new activity you need to use an `intent`. You can read about [switching acivities with intent](http://stackoverflow.com/questions/736571/using-intent-in-an-android-application-to-show-another-activity) in this post. You cannot simply change the `contentView`. – Rarw Apr 03 '13 at 18:03
  • I know, but does that ruins the effect of swiping? – Lasse Apr 03 '13 at 18:05
  • 1
    I did not even notice you were trying to use a `viewpager`. In that case you need to convert your activities to fragments, add those fragments to the view pager, and then that will let you swipe between. [Read this tutorial](http://developer.android.com/training/animation/screen-slide.html) from the android docs. In that case, youre question is really something different than what you asked. – Rarw Apr 03 '13 at 19:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27507/discussion-between-rarw-and-lasse) – Rarw Apr 03 '13 at 19:09