quincyHammond 2015-02-03 15:19 采纳率: 100%
浏览 2330
已采纳

android 为啥父类不闪退,但我重新写了个子类后,程序闪退?

求教各位大神,下面的是我的一个ViewPager的安卓小程序,如大家所见现在这个android代码是能正常运行的,但当我将代码中所有父类 ”ViewPager“ 换成 子类
”NoScrollViewPager“ 后,程序一打开就闪退。。。

为啥换了个子类就会闪退啊。。。

package com.example.testviewpagerandtabhost;

public class MainActivity extends Activity {

Context context = null;
LocalActivityManager manager = null;
ViewPager pager = null;
TabHost tabHost = null;
TextView t1,t2,t3;

private int offset = 0;// 动画图片偏移量
private int currIndex = 0;// 当前页卡编号
private int bmpW;// 动画图片宽度
private ImageView cursor;// 动画图片

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

    context = MainActivity.this;
    manager = new LocalActivityManager(this , true);
    manager.dispatchCreate(savedInstanceState);

    InitImageView();
    initTextView();
    initPagerViewer();

}
/**
 * 初始化标题
 */
private void initTextView() {
    t1 = (TextView) findViewById(R.id.text1);
    t2 = (TextView) findViewById(R.id.text2);
    t3 = (TextView) findViewById(R.id.text3);

    t1.setOnClickListener(new MyOnClickListener(0));
    t2.setOnClickListener(new MyOnClickListener(1));
    t3.setOnClickListener(new MyOnClickListener(2));

}
/**
 * 初始化PageViewer
 */
private void initPagerViewer() {
    pager = ((ViewPager) findViewById(R.id.viewpage));
    final ArrayList<View> list = new ArrayList<View>();
    Intent intent = new Intent(context, A.class);
    list.add(getView("A", intent));
    Intent intent2 = new Intent(context, B.class);
    list.add(getView("B", intent2));
    Intent intent3 = new Intent(context, C.class);
    list.add(getView("C", intent3));

    pager.setAdapter(new MyPagerAdapter(list));
    pager.setCurrentItem(0);
    pager.setOnPageChangeListener(new MyOnPageChangeListener());
}
/**
 * 初始化动画
 */
private void InitImageView() {
    cursor = (ImageView) findViewById(R.id.cursor);
    bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.roller)
    .getWidth();// 获取图片宽度
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenW = dm.widthPixels;// 获取分辨率宽度
    offset = (screenW / 3 - bmpW) / 2;// 计算偏移量
    Matrix matrix = new Matrix();
    matrix.postTranslate(offset, 0);
    cursor.setImageMatrix(matrix);// 设置动画初始位置
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
/**
 * 通过activity获取视图
 * @param id
 * @param intent
 * @return
 */
private View getView(String id, Intent intent) {
    return manager.startActivity(id, intent).getDecorView();
}

/**
 * Pager适配器
 */
public class MyPagerAdapter extends PagerAdapter{
    List<View> list =  new ArrayList<View>();
    public MyPagerAdapter(ArrayList<View> list) {
        this.list = list;
    }

    @Override
    public void destroyItem(ViewGroup container, int position,
            Object object) {
        ViewPager pViewPager = ((ViewPager) container);
        pViewPager.removeView(list.get(position));
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object instantiateItem(View arg0, int arg1) {
        ViewPager pViewPager = ((ViewPager) arg0);
        pViewPager.addView(list.get(arg1));
        return list.get(arg1);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
    }
}
/**
 * 页卡切换监听
 */
public class MyOnPageChangeListener implements OnPageChangeListener {

    int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
    int two = one * 2;// 页卡1 -> 页卡3 偏移量

    @Override
    public void onPageSelected(int arg0) {
        Animation animation = null;
        switch (arg0) {
        case 0:
            if (currIndex == 1) {
                animation = new TranslateAnimation(one, 0, 0, 0);
            } else if (currIndex == 2) {
                animation = new TranslateAnimation(two, 0, 0, 0);
            }
            break;
        case 1:
            if (currIndex == 0) {
                animation = new TranslateAnimation(0, one, 0, 0);
            } else if (currIndex == 2) {
                animation = new TranslateAnimation(two, one, 0, 0); 
            }
            break;
        case 2:
            if (currIndex == 0) {
                animation = new TranslateAnimation(0, two, 0, 0);
            } else if (currIndex == 1) {
                animation = new TranslateAnimation(one, two, 0, 0);
            }
            break;
        }
        currIndex = arg0;
        animation.setFillAfter(true);// True:图片停在动画结束位置
        animation.setDuration(300);
        cursor.startAnimation(animation);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }
}
/**
 * 头标点击监听
 */
public class MyOnClickListener implements View.OnClickListener {
    private int index = 0;

    public MyOnClickListener(int i) {
        index = i;
    }

    @Override
    public void onClick(View v) {
        pager.setCurrentItem(index);
    }
};








public class NoScrollViewPager extends ViewPager { 

    public NoScrollViewPager(Context context) {
        super(context);
        // TODO 自动生成的构造函数存根
    }

    private boolean noScroll = false;  



    public void setNoScroll(boolean noScroll) {  
        this.noScroll = noScroll;  
    }  

    @Override  
    public void scrollTo(int x, int y) {  
        super.scrollTo(x, y);  
    }  

    @Override  
    public boolean onTouchEvent(MotionEvent arg0) {  
        /* return false;//super.onTouchEvent(arg0); */  
        if (noScroll)  
            return false;  
        else  
            return super.onTouchEvent(arg0);  
    }  

    @Override  
    public boolean onInterceptTouchEvent(MotionEvent arg0) {  
        if (noScroll)  
            return false;  
        else  
            return super.onInterceptTouchEvent(arg0);  
    }  

    @Override  
    public void setCurrentItem(int item, boolean smoothScroll) {  
        super.setCurrentItem(item, smoothScroll);  
    }  

    @Override  
    public void setCurrentItem(int item) {  
        super.setCurrentItem(item);  
    }  

}  

}

  • 写回答

4条回答 默认 最新

  • danielinbiti 2015-02-03 16:11
    关注

    1、子类可以转换成父类,但父类没法转化成子类。把xml定义的viewpage改成你的子类定义。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题