안드로이드 개발 질문/답변
(글 수 45,052)
CustomAdapter 를 이용하여 ListView를 생성하고, 이에 이미지버튼을 추가하였는데요
각 List를 생성하는 버튼은 따로 만들었구요
List에 버튼을 추가한 후 그 버튼에 대해서 Listener를 만드려고 하는데 컴파일은 되지만
List 생성 시 NullPointException이 발생합니다.
final ArrayList<Recent> mRecent = new ArrayList<Recent>();
final RecentAdapter mRecent_Adap =
new RecentAdapter(this, R.layout.row, mRecent);
Recent r1 = new Recent(mTitle.getText().toString(),
mContent.getText().toString(),
mFileaddr.getText().toString());
mRecent.add(0, r1);
mRecent_Adap.notifyDataSetChanged();
setListAdapter(mRecent_Adap);
<중간 생략>
private class RecentAdapter extends ArrayAdapter<Object>
{
private ArrayList<Recent> items;
@SuppressWarnings("unchecked")
public RecentAdapter(Context context, int textViewResourceId, ArrayList items)
{
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Recent p = items.get(position);
ImageButton mPaste = (ImageButton)findViewById(R.id.ImageButton02);
mPaste.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(Home.this, "aaa", Toast.LENGTH_SHORT).show();
}
});
01-24 21:52:47.182: ERROR/AndroidRuntime(334): java.lang.NullPointerException
01-24 21:52:47.182: ERROR/AndroidRuntime(334): at com.android.ojt.Home$RecentAdapter.getView(Home.java:309)
01-24 21:52:47.182: ERROR/AndroidRuntime(334): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
위의 r1 버튼은 List 생성하는 버튼이구요
r1버튼의 onClickListener 에서 해보고 getView 에도 해봤지만 이미지버튼에 대한 Listener는 생성이 안되네요
왜 이런걸까요?
각 List를 생성하는 버튼은 따로 만들었구요
List에 버튼을 추가한 후 그 버튼에 대해서 Listener를 만드려고 하는데 컴파일은 되지만
List 생성 시 NullPointException이 발생합니다.
final ArrayList<Recent> mRecent = new ArrayList<Recent>();
final RecentAdapter mRecent_Adap =
new RecentAdapter(this, R.layout.row, mRecent);
Recent r1 = new Recent(mTitle.getText().toString(),
mContent.getText().toString(),
mFileaddr.getText().toString());
mRecent.add(0, r1);
mRecent_Adap.notifyDataSetChanged();
setListAdapter(mRecent_Adap);
<중간 생략>
private class RecentAdapter extends ArrayAdapter<Object>
{
private ArrayList<Recent> items;
@SuppressWarnings("unchecked")
public RecentAdapter(Context context, int textViewResourceId, ArrayList items)
{
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Recent p = items.get(position);
ImageButton mPaste = (ImageButton)findViewById(R.id.ImageButton02);
mPaste.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(Home.this, "aaa", Toast.LENGTH_SHORT).show();
}
});
01-24 21:52:47.182: ERROR/AndroidRuntime(334): java.lang.NullPointerException
01-24 21:52:47.182: ERROR/AndroidRuntime(334): at com.android.ojt.Home$RecentAdapter.getView(Home.java:309)
01-24 21:52:47.182: ERROR/AndroidRuntime(334): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
위의 r1 버튼은 List 생성하는 버튼이구요
r1버튼의 onClickListener 에서 해보고 getView 에도 해봤지만 이미지버튼에 대한 Listener는 생성이 안되네요
왜 이런걸까요?
아..그리고 생성된 각 List 에 대해서 이미지나 내용을 다른 클래스에서 boolean 변수가 true일때 추가하도록 했는데요
boolean 변수 값이 변경되면 List 내용도 변경되던데 각 List의 값을 등록하면서 바뀌지 않도록 하는 방법이 있나요?
2010.01.25 09:55:48
ImageButton mPaste = (ImageButton)findViewById(R.id.ImageButton02);
mPaste.setOnClickListener(new OnClickListener() <- 에러가 발생한 위치(Home.java:309)가 여기인가요??
만약 그렇다면
ImageButton mPaste = (ImageButton)findViewById(R.id.ImageButton02); <- 이게 값을 못가져와서 mPaste가 null이라는 것이거든요..
해당 layout에 ImageButton02 <- 이게 있는지 확인해 보세요..
두번째 질문은 제가 잘 이해가 않되네요.. 다른 분이 답변해 주시길..;;;
mPaste.setOnClickListener(new OnClickListener() <- 에러가 발생한 위치(Home.java:309)가 여기인가요??
만약 그렇다면
ImageButton mPaste = (ImageButton)findViewById(R.id.ImageButton02); <- 이게 값을 못가져와서 mPaste가 null이라는 것이거든요..
해당 layout에 ImageButton02 <- 이게 있는지 확인해 보세요..
두번째 질문은 제가 잘 이해가 않되네요.. 다른 분이 답변해 주시길..;;;
2010.01.25 11:47:30
해당 layout 에 동일한 id 값을 가지는 ImageButton 을 생성하였습니다.
<ImageButton
android:id="@+id/PasteButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_toRightOf="@id/contenttext"
android:layout_alignParentRight="true"
android:src="@drawable/paste"
>
</ImageButton>
그런데 계속해서 NullPointException이 발생을 하네요;;
그리고 두번째 질문은...예를들어...A라는 클래스에 static boolean 변수를 만들어 놓고, 체크박스를 체크했을 때 해당 변수값을 true 로 만들어서 boolean 변수값이 true 일때 ListView 에 추가한 TextView 에 A라는 Text를 띄우게 했다면, 체크박스를 선택했을 때는 true 이어서 List상에 A라는 Text 가 출력이 되지만, 그 이후에 boolean 변수값을 false로 바꿔주면 List에 출력되었던 A 라는 Text 가 false 일때 띄울 Text로 변하게 됩니다. 이때, 각 List에 출력되는 이미지나 Text를 지속적으로 유지하고 싶은데 그게 잘 되질 않네요;;;
<ImageButton
android:id="@+id/PasteButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_toRightOf="@id/contenttext"
android:layout_alignParentRight="true"
android:src="@drawable/paste"
>
</ImageButton>
그런데 계속해서 NullPointException이 발생을 하네요;;
그리고 두번째 질문은...예를들어...A라는 클래스에 static boolean 변수를 만들어 놓고, 체크박스를 체크했을 때 해당 변수값을 true 로 만들어서 boolean 변수값이 true 일때 ListView 에 추가한 TextView 에 A라는 Text를 띄우게 했다면, 체크박스를 선택했을 때는 true 이어서 List상에 A라는 Text 가 출력이 되지만, 그 이후에 boolean 변수값을 false로 바꿔주면 List에 출력되었던 A 라는 Text 가 false 일때 띄울 Text로 변하게 됩니다. 이때, 각 List에 출력되는 이미지나 Text를 지속적으로 유지하고 싶은데 그게 잘 되질 않네요;;;
2010.01.25 15:51:01
<ImageButton
android:id="@+id/PasteButton" <- (ImageButton)findViewById(R.id.ImageButton02);
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_toRightOf="@id/contenttext"
android:layout_alignParentRight="true"
android:src="@drawable/paste"
>
</ImageButton>
소스 부분엔 여기다 쓰느라 임의로 적어 놓신건가요? 그러신거면 디버깅으로
mPaste.setOnClickListener(new OnClickListener() <- 이부분에서 mPaste가 null인지 한번 확인해 보세요.. 에러의 원인을 찾는게 먼저 일거 같아요..
android:id="@+id/PasteButton" <- (ImageButton)findViewById(R.id.ImageButton02);
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_toRightOf="@id/contenttext"
android:layout_alignParentRight="true"
android:src="@drawable/paste"
>
</ImageButton>
소스 부분엔 여기다 쓰느라 임의로 적어 놓신건가요? 그러신거면 디버깅으로
mPaste.setOnClickListener(new OnClickListener() <- 이부분에서 mPaste가 null인지 한번 확인해 보세요.. 에러의 원인을 찾는게 먼저 일거 같아요..
리스트에 아이템 추가는 mRecent_Adap.notifyDataSetChanged(); 로 추가된 아이템에 대해 adapter에 추가하라는
메시지를 전달하고 있습니다.
notify 를 boolean 값에 따라 분기시켜주시면 될것 같은데요..