我已经阅读了15个关于这个主题的问题,但没有一个解决方案对我有用。
我创建了一个自定义适配器来填充ListView。每个ListView行都包含一个复选框,我试图使用setOnCheckedChangeListener在我的自定义适配器中捕获已检查的更改事件。
当我运行应用程序时,我在setOnCheckedChangeListener行上得到一个空指针异常。
这是我的自定义适配器类:
package com.android.toneturtle;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class ListAdapterPlaylist extends BaseAdapter{
private ArrayList<Playlist> playlists;
private Context context;
LayoutInflater inflater;
public ListAdapterPlaylist(Context context, ArrayList<Playlist> playlists){
this.context = context;
this.playlists = playlists;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// return the number of records in cursor
return playlists.size();
}
@Override
public Object getItem(int position) {
return playlists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if(view==null) {
view = inflater.inflate(R.layout.playlist_items, parent, false);
}
TextView name = (TextView)view.findViewById(R.id.text_view);
name.setText(playlists.get(position).getName());
CheckBox checkbox = (CheckBox)view.findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("test","i work!");
}
});
return view;
}
}
如果有人能帮我解决这个问题,我将非常感激。
修改 这是错误logcat
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.android.toneturtle.ListAdapterPlaylist.getView(ListAdapterPlaylist.java:52)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:230)
at android.widget.AbsListView.obtainView(AbsListView.java:2627)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1259)
at android.widget.ListView.onMeasure(ListView.java:1171)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16831)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
at android.view.View.measure(View.java:16831)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
at android.view.View.measure(View.java:16831)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2189)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1352)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1535)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
谢谢你Richa的帮助。
我在row.xml文件中有一个不正确的id名称。
......我觉得自己像个白痴:P