public class BorderLayout extends LinearLayout
{
public BorderLayout(Context context,AttributeSet attrs)
{
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.border,this);
ImageButton border_menu = (ImageButton) findViewById(R.id.border_menu);
ImageButton border_setting = (ImageButton) findViewById(R.id.border_setting);
ImageButton border_search = (ImageButton) findViewById(R.id.border_search);
border_menu.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast.makeText(getContext(), "You clicked Edit button",Toast.LENGTH_SHORT).show();
}
});
border_setting.setOnClickListener(new OnClickListener(){
public void onClick(View view)
{
Toast.makeText(getContext(),"Hello",Toast.LENGTH_SHORT).show();
//PopupMenu pop_menu = new PopupMenu(getContext(),view);
//pop_menu.getMenuInflater().inflate(R.menu.bord,pop_menu.getMenu());
}
});
}
}不添加点击事件没事,一加就闪退,求大神告知怎么回事,万分感激。

解决方案 »

  1.   

    发现三个ImageButton控件的findViewById返回null,网上百度一堆,在方法前指定view调用,也同样返回null,求大神指点一下。
      

  2.   

    没试过这个自定义View...
    你试试
    View view=LayoutInflater.from(context).inflate(R.layout.border,this);
    ImageButton border_menu = (ImageButton) view.findViewById(R.id.border_menu);
      

  3.   

    查看border.xml这个布局文件是否有这三个id的view
      

  4.   

    2楼正解,你findviewbyid的调用者错了
      

  5.   

    尝试着用这个:VIew view=Layoutainflater.from(contest).inflate(R.layout.border,null );然后各个控件的得到用:ImageVIew border_menu=(ImageVIew)view.findVIewById(R.id.border_setting);在此之前要确定你的界面里面要有这些控件对切有对应的id