개발중인 프로그램이 있는데 하도 안되길래 일단 테스트본을 간단하게 만들어봤습니다.
1. resultlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/resultBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="true"
android:text="전체 보기"
/>
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
2.row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tr3_label1"
android:layout_width="wrap_content"
android:layout_height="30px"
android:layout_marginRight="10dp"
android:gravity="center"
android:textSize="20dp"
android:textColor="#FFCC00"
/>
<TextView
android:id="@+id/tr3_label2"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:textSize="20dp"
android:textColor="#FFFF00"
/>
<TextView
android:id="@+id/tr3_label3"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:textSize="20dp"
android:textColor="#FFFFFF"
/>
<Button
android:id="@+id/addFavorite"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="테스트"
android:onClick="true" />
</LinearLayout>
상단 resultlist.xml 내의 android:id="@+id/list" --> 이 리스트에 들어가는 row가 row.xml입니다.
package android.text;
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ResultList extends Activity implements OnClickListener{
private ListView favList;
private ArrayList<TransInfoBean> favArrayList;
private InfoFavoriteAdapter favAdapter;
private TransInfoDataManager mDm;
private Button resultBtn;
private Button addFavorite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDm = new TransInfoDataManager(this);
setContentView(R.layout.resultlist);
initCustom();
initComponent();
refreshList();
}
@Override
protected void onDestroy() {
mDm.close();
super.onDestroy();
}
/**
* Process any initializing stop.
*/
private void initCustom() {
favArrayList = new ArrayList<TransInfoBean>();
favAdapter = new InfoFavoriteAdapter(favArrayList);
}
/**
* Initialize & Set Components
*/
private void initComponent() {
resultBtn = (Button) findViewById(R.id.resultBtn);
resultBtn.setFocusable(false);
resultBtn.setOnClickListener(this);
addFavorite = (Button) findViewById(R.id.addFavorite);
//addFavorite.setFocusable(false);
//addFavorite.setOnClickListener(this);
favList = (ListView) findViewById(R.id.list);
favList.setAdapter(favAdapter);
favList.setOnItemClickListener(new FavoriteItemClick());
}
class FavoriteItemClick implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "itemClick테스트", 1).show();
}
}
@Override
public void onClick(View v){
if(v == resultBtn){
Toast.makeText(getApplicationContext(), "상단버튼 테스트", 200).show();
}else if(v == addFavorite){
Toast.makeText(getApplicationContext(), "커스텀 리스트뷰 테스트", 200).show();
}
}
private void refreshList() {
XXXXXXXX
}
class InfoFavoriteAdapter extends BaseAdapter{
private ArrayList<TransInfoBean> object;
public InfoFavoriteAdapter(ArrayList<TransInfoBean> object) {
super();
this.object = object;
}
public int getCount(){
return object.size();
}
public Object getItem(int position){
return object.get(position);
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
if(view == null){
LayoutInflater inflater = LayoutInflater.from(ResultList.this);
view = inflater.inflate(R.layout.two_text_row, parent, false);
}
//addFavorite.setFocusable(false);
//addFavorite.setClickable(false);
TextView label_1 = (TextView) view.findViewById(R.id.tr3_label1);
TextView label_2 = (TextView) view.findViewById(R.id.tr3_label2);
TextView label_3 = (TextView) view.findViewById(R.id.tr3_label3);
label_1.setText(object.get(position).getUnivCode());
label_2.setText(object.get(position).getUnivName());
label_3.setText(object.get(position).getStatus());
return view;
}
}
}
위의 클래스를 보면 resultlist.xml의 Button에 있는 resultBtn을 통해서 클릭 이벤트를 접근하면 문제가 없습니다.
(주석처리해 놓은 곳 부근을 보면 됩니다.)
그러나 row.xml의 Button의 id인 addFavorite로 접근하면 문제가 발생합니다.
getView에서든 onCreate에서든 관련없이 무조건 오류가 발생합니다.
(getView에서 오류는 아예 자바클래스에 진입조차 못합니다. 처음에 불러오면서 오류가 발생해버리니까요.)
이걸 해결할 수 있는 방법이 있을까요?
findViewById까지는 문제가 없으나 그 이후에 버튼에 메소드를 매치시켜줄 때 무조건 에러가 떨어집니다.
addFavorite.setOnClickListener
addFavorite.setClickable
addFavorite.setFocusable
등등...전부 에러 떨어집니다.
에러에 대한 내용도 잘 모르겠습니다 -_-;;
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): FATAL EXCEPTION: main
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): java.lang.IllegalStateException: Could not find a method true(View) in the activity class android.test.ResultList for onClick handler on view class android.widget.Button with id 'addFavorite'
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at android.view.View$1.onClick(View.java:2147)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at android.view.View.performClick(View.java:2501)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at android.view.View$PerformClick.run(View.java:9107)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at android.os.Handler.handleCallback(Handler.java:587)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at android.os.Looper.loop(Looper.java:123)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at android.app.ActivityThread.main(ActivityThread.java:3839)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at java.lang.reflect.Method.invoke(Method.java:507)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at dalvik.system.NativeStart.main(Native Method)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): Caused by: java.lang.NoSuchMethodException: true
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at java.lang.Class.getMethod(Class.java:962)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): at android.view.View$1.onClick(View.java:2140)
05-17 21:43:49.645: ERROR/AndroidRuntime(13746): ... 11 more




안되는게 당연한데요...
addFavorite는 row.xml에 있는 id인데요... 현재 엑티비티에서는 resultlist.xml이 contentView로 되어있습니다.
한마디로 못찾죠....
그리고 리스트의 getView 쪽에서 보면...
view.findViewById를 이용해서 addFavorite를 찾아주신다음에 사용하셔야 합니다.