关于在内部类中启动一个Android Intent的疑惑

在网上看到的所有android例子都是把adapter和listener写在一起,多是内部类的实现方式。我今天想把他们分离开。即adapter和listener写在一个class里面,adapter和listener在同一个class中以内部类形式存在。但是adapter在相关操作上正常,但是listener缺出现启动一个activity时null指针问题。代码如下

 

在一个JshopActivityIndex中我使用了viewpager控件

我先把viewpager的相关adapter和listener写在了JshopViewpagerAdapter

 

package com.jshop.android.widget;

import java.util.ArrayList;

import com.jshop.android.index.JshopActivityIndex;
import com.jshop.android.index.JshopMIndex;
import com.jshop.android.index.R;
import com.jshop.android.index.WelcomeAct;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;

public class JshopViewpagerAdapter extends Activity {

	public class JshopActivityIndexViewpagerAdapter extends PagerAdapter {

		private ArrayList<View> pageViews;

		public JshopActivityIndexViewpagerAdapter() {
			super();
			// TODO Auto-generated constructor stub
		}

		/**
		 * 对左右滚动空间进行适配器定义和操作
		 */
		public JshopActivityIndexViewpagerAdapter(ArrayList<View> pv) {
			this.pageViews = pv;
		}

		@Override
		public int getCount() {
			return pageViews.size();
		}

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

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(pageViews.get(position));
		}

		@Override
		public void finishUpdate(View container) {
			// TODO Auto-generated method stub
		}

		@Override
		public Object instantiateItem(View container, int position) {
			// TODO Auto-generated method stub
			((ViewPager) container).addView(pageViews.get(position));
			return pageViews.get(position);
		}

		@Override
		public void setPrimaryItem(View container, int position, Object object) {
			// TODO Auto-generated method stub
			super.setPrimaryItem(container, position, object);
		}

		@Override
		public void startUpdate(View container) {
			// TODO Auto-generated method stub
			super.startUpdate(container);
		}

	}
	 // 指引页面更改事件监听器
    public class JshopActivityIndexViewpagerPageChangeListener  implements OnPageChangeListener {  
    	private Context context;
    	private ImageView[] imageViews;
    	
    	public JshopActivityIndexViewpagerPageChangeListener(Context ctx,ImageView[] v){
    		this.context=ctx;
    		this.imageViews=v;
    	}
        @Override  
        public void onPageScrollStateChanged(int arg0) {  
            // TODO Auto-generated method stub  
        }
  
        @Override  
        public void onPageScrolled(int arg0, float arg1, int arg2) {  
            // TODO Auto-generated method stub  
        }  
  
        @Override  
        public void onPageSelected(int arg0) {
            for (int i = 0; i < imageViews.length; i++) {  
                imageViews[arg0].setBackgroundResource(R.drawable.page_indicator_focused);
                
                if (arg0 != i) {  
                    imageViews[i].setBackgroundResource(R.drawable.page_indicator);  
                }  
            }
            if(arg0==5){
            	Intent intent = new Intent(context,JshopMIndex.class);
				startActivity(intent);
            }
        }  
    }

	
}

当程序执行到

 

 if(arg0==5){
            	Intent intent = new Intent(context,JshopMIndex.class);
				startActivity(intent);
            }


启动另一个activity时候就会报错。

 

我是这样调用这个内部类的

 

	viewPager.setAdapter(new JshopViewpagerAdapter().new JshopActivityIndexViewpagerAdapter(pageViews));
		viewPager.setOnPageChangeListener(new JshopViewpagerAdapter().new JshopActivityIndexViewpagerPageChangeListener(JshopActivityIndex.this,imageViews));


我也在网上查到了类似问题地址是

http://stackoverflow.com/questions/6140157/startactivityforresult-null-pointer-exception-context

我心里想把adapter和listener分离,可以让activity的代码不用那么长,可是目前只能做到分离adapter。希望有高人指点下

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值