识别生成的gridview按钮的问题

时间:2011-05-05 01:33:35

标签: android button toast

每当我尝试根据点击的按钮显示Toast消息时,我都会收到以下错误。

这是我正在使用的代码

[ButtonAdapter]

 public Context mContext;  

 String[] filenames = { "one", "two", "lul" };


 // Gets the context so it can be used later  
 public ButtonAdapter(Context c) {  
  mContext = c;  
 }  

 // Total number of things contained within the adapter  
 public int getCount() {  
  return filenames.length;  
 }  

  // Require for structure, not really used in my code.  
 public Object getItem(int position) {  
  return null;  
 }  

 // Require for structure, not really used in my code. Can  
 // be used to get the id of an item in the adapter for  
 // manual control.  
 public long getItemId(int position) {  
  return position;  
 }  

 public View getView(int position, View convertView, ViewGroup parent) {  
  Button btn;  

  if (convertView == null) {  
   // if it's not recycled, initialise some attributes  
   btn = new Button(mContext);  
   btn.setLayoutParams(new GridView.LayoutParams(100, 55));  
   btn.setPadding(8, 8, 8, 8);  
   }  
  else {  
   btn = (Button) convertView;  
  }  
  btn.setText(filenames[position]);  
  // filenames is an array of strings  
  btn.setTextColor(Color.WHITE);  
  btn.setBackgroundResource(R.drawable.button);  
  btn.setId(position);  

  btn.setOnClickListener(new MyOnClickListener(position));  


  return btn;  
 }

}

这是onClickListener

class MyOnClickListener extends gridview implements OnClickListener {

    private final int position;

    public MyOnClickListener(int position) {
        this.position = position;
    }

    @Override
    public void onClick(View v) {
        function3(this.position);
    }

最后,我的主要活动

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

    setUpViews();

    }

public void function3(int buttonposition) {
    if (buttonposition == 1) {
        Toast.makeText(this, "test", Toast.LENGTH_SHORT);
    } else {
        Toast.makeText(this, "test", Toast.LENGTH_LONG);
    }   
}

private void setUpViews() {

    GridView gridview = (GridView) findViewById(R.id.gridView2);  
    gridview.setAdapter(new ButtonAdapter(this));  
    }

}

导致以下错误

05-05 00:41:12.949: ERROR/AndroidRuntime(1570): java.lang.NullPointerException
05-05 00:41:12.949: ERROR/AndroidRuntime(1570):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
05-05 00:41:12.949: ERROR/AndroidRuntime(1570):     at android.widget.Toast.<init>(Toast.java:89)
05-05 00:41:12.949: ERROR/AndroidRuntime(1570):     at android.widget.Toast.makeText(Toast.java:231)
05-05 00:41:12.949: ERROR/AndroidRuntime(1570):     at com.gridview.msg.function3(msg.java:113)
05-05 00:41:12.949: ERROR/AndroidRuntime(1570):     at com.gridview.msg.MyOnClickListener.onClick(MyOnClickListener.java:21)

这已经很久了。我确定它与if (buttonposition == 1)...语句有关,我只是没有以正确的方式或其他方式调用它。有谁知道什么是错的?

2 个答案:

答案 0 :(得分:0)

.show()来电结束时,您遗失了makeText()Toast.makeText()构造Toast对象,但仍需要调用show()来显示它:

Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();

答案 1 :(得分:0)

将Buttons属性设置为可聚焦且可点击为false。因为在当前情况下按钮捕获焦点和单击事件,而不是在网格视图项上捕获。链路..... https://stackoverflow.com/a/11752450/2476688