2011-05-05 3 views
0

어떤 버튼을 클릭했는지에 따라 토스트 메시지를 표시하려고 할 때마다 다음 오류가 발생합니다. 여기 생성 된 gridview 버튼 식별 문제.

은 내가 사용하고 코드입니다

[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); 
    } 

그리고 마지막으로, 내 주요 활동

,

}이이 연령대에 내 머리를하고있다 다음과 같은 오류

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)... 진술로 확신 할 것이고 나는 그것을 올바른 방식으로 또는 뭔가 부르지 않을 것이다. 아무도 틀린 것을 아는가?

+0

나는 방금 전복 배지를 얻을 수 있었다. "1 주일 동안 득표 없음, 대답 없음, 의견 없음 및 낮은 의견으로 질문 함." 이걸 부딪쳐도 될까요? – jblz

답변

0

makeText() 통화가 끝나면 .show()이 누락되었습니다. Toast.makeText()는 토스트 객체를 생성,하지만 당신은 여전히 ​​표시 할 show()를 호출해야합니다 false로

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

설정 버튼 속성 포커스 및 클릭. 포커스 및 클릭 이벤트는 현재 사례의 버튼으로 캡처되기 때문에 그리드보기 항목에는 표시되지 않습니다. 링크 ..... https://stackoverflow.com/a/11752450/2476688