用按钮在android中滑动

时间:2013-04-03 16:18:40

标签: android button android-viewpager swipe android-4.0-ice-cream-sandwich

我遇到了这个问题:

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;
      }
}
}

我正在尝试在名为beginner的活动上实现按钮,但是如果我在一个名为ibDistance的按钮的情况2中放置一个OnClickListner,程序崩溃

    ibDistance.setOnClickListener(new View.OnClickListener() {

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

Logcat文件:

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)

我该怎么办? 谢谢:))

1 个答案:

答案 0 :(得分:2)

我首先要问的一个问题是你尝试使用主要活动的一部分按钮? findViewById的工作原理是搜索您在setContentView中为您要查找的视图提供的视图 - 在本例中为按钮。如果按钮实际上不在布局中,那么它将无法找到并且会给您一个错误。

假设按钮位于您的主要活动布局中,这就是我认为正在发生的事情。

通常,您需要在onClickListener方法中添加onCreate。有一些方法可以将onClickListener移到onCreate之外,例如using a private class。我认为您可以通过以下方式解决此问题:

(1)在onCreate

之前将您的按钮声明为实例变量
ImageButton ibDistance;

(2)初始化按钮并在onClickListener方法中设置onCreate

ibDistance = (ImageButton) findViewById(R.id.ibDistance);
ibDistance.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                }
            });

如果您正在更改案例2下的布局或添加按钮(我无法确定您正在做什么),那么您只需参考您之前在代码部分中声明的ImageButton

另一种方法是不要使用onClickListener。按钮都具有onClick XML属性,允许您指定要触发的方法,而不必使用onClickListener。要使用此功能,您需要在按钮触发某事的活动中声明一个公共方法。

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

注释部分的内容是按下按钮时会发生什么。显然,您更改了方法的名称以及您在该按钮的onClick属性中放置的内容以满足您的需求。