从抽屉打开SlideUpPanel OnClickListener NULL指针

时间:2015-05-30 14:32:20

标签: android google-maps android-layout android-fragments android-studio

我是初学者,使用谷歌地图V2,导航抽屉和AndroidSlidingUpPanel Library的SlideUpPanel开发地理定位应用 在我的MapsActivity中,我有一个公共课" MyAdapter"我在哪里使用自定义行。 当我想在我的适配器类中使用滑动布局(mLayout)打开滑动面板onClickListener时,android studio无法解析符号' layout'。

如何将此布局调用到我的适配器中? 抱歉我的英文不好

class MyAdpater extends BaseAdapter {
private Context context ;
String [] telecentreAurillac;
int[] images = {R.drawable.ic_pointeur,R.drawable.ic_pointeur,R.drawable.ic_pointeur,R.drawable.ic_pointeur,R.drawable.ic_pointeur,
        R.drawable.ic_pointeur,R.drawable.ic_pointeur,R.drawable.ic_pointeur,R.drawable.ic_pointeur,R.drawable.ic_pointeur};


public MyAdpater (Context context) {

    this.context=context;

    telecentreAurillac = context.getResources().getStringArray(R.array.telecentreAurillac);
}

@Override
public int getCount() {
    return telecentreAurillac.length;
}

@Override
public Object getItem(int position) {
    return telecentreAurillac[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     View row = null;

    if(convertView == null)
    {
        LayoutInflater inflater =(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.custom_row, parent, false);
    }
    else
    {
        row = convertView;
    }
    // Relier avec custom row
    TextView titleTextView =(TextView) row.findViewById(R.id.TelecentreTexViewDrawer);
    ImageView titleImageView = (ImageView) row.findViewById(R.id.imageDrawer);
    //affichage pour chaque position
    titleTextView.setText(telecentreAurillac[position]);
    titleImageView.setImageResource(images[position]);


    //LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //View drawer = inflater.inflate(R.layout.activity_maps, null);


    row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("MapsActivity", "clickOnTV");



            // drawerLayout.closeDrawers();

            mLayout.setAnchorPoint(1.0f);
            // mLayout.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN);
            mLayout.setPanelState(SlidingUpPanelLayout.PanelState.ANCHORED);


        }
    });

    return row;

}

}

在我的主要活动中

    mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
    //Log.i("test test", mLayout.toString());
    mLayout.setPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() {
        @Override
        public void onPanelSlide(View panel, float slideOffset) {
            Log.i(TAG, "onPanelSlide, offset " + slideOffset);
        }

        @Override
        public void onPanelExpanded(View panel) {
            Log.i(TAG, "onPanelExpanded");

        }

        @Override
        public void onPanelCollapsed(View panel) {
            Log.i(TAG, "onPanelCollapsed");

        }

        @Override
        public void onPanelAnchored(View panel) {
            Log.i(TAG, "onPanelAnchored");
        }

        @Override
        public void onPanelHidden(View panel) {
            Log.i(TAG, "onPanelHidden");
        }
    });

1 个答案:

答案 0 :(得分:0)

简单的方法是将其设置为静态并访问它,如:

 Activity.mlayout..

但正确的方法是通过构造函数参数发送对象引用,如:

public MyAdpater (Context context, SlidingUpPanel mlayout) {

this.mlayout = mlayout;
this.context=context;

telecentreAurillac = context.getResources().getStringArray(R.array.telecentreAurillac);

}

更好的方法是在Activity中处理listview项目的点击次数:

listView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
      mLayout.setAnchorPoint(1.0f); 
        // mLayout.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN); 
        mLayout.setPanelState(SlidingUpPanelLayout.PanelState.ANCHORED); 
  }
});