listView.setAdapter()
正在崩溃我的程序。任何人都知道为什么会发生崩溃?
Logcat错误:
AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
的活动:
public class SavedVideosFragment extends Fragment {
View rootview;
MySQLiteHelper mydb;
private SavedVideoAdapter mSavedVideoAdapter;
ListView listView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.saved_video_fragment_layout,container,false);
getSavedVideo();
listView = (ListView)rootview.findViewById(R.id.savedVideoListView);
return rootview;
}
public void getSavedVideo() {
mydb = new MySQLiteHelper(getActivity());
ArrayList savedVideoIDs = mydb.getAllSavedVideo();
mSavedVideoAdapter = new SavedVideoAdapter(getActivity(), savedVideoIDs, mydb);
listView.setAdapter(mSavedVideoAdapter);
}
}
适配器:
public class SavedVideoAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<Object> mData = new ArrayList<Object>();
MySQLiteHelper mydb;
private LayoutInflater mInflater;
public SavedVideoAdapter(Context context, ArrayList<Object> data, MySQLiteHelper database) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData = data;
mydb = database;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
Object obj = mData.get(position);
return "";
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Cursor res = res = mydb.getData((Integer)mData.get(position));
convertView = mInflater.inflate(R.layout.saved_video_adapter, null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.thumbnail);
TextView activity = (TextView) convertView.findViewById(R.id.activity);
TextView activityDate = (TextView) convertView.findViewById(R.id.activityDate);
ImageView cancel = (ImageView) convertView.findViewById(R.id.x);
activity.setText(res.getColumnIndex("title"));
return convertView;
}
}
ListView XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/savedVideoListView"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
适配器布局xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:src="@drawable/ic_launcher" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="@id/thumbnail">
<TextView
android:id="@+id/activity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
<TextView
android:id="@+id/activityDate"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_below="@+id/activity"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="?android:attr/listPreferredItemHeight"
android:layout_height="fill_parent"
android:layout_alignParentRight="true">
<ImageView
android:id="@+id/x"
android:layout_width="?android:attr/listPreferredItemHeight"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="@drawable/x" />
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:3)
AndroidRuntime:FATAL EXCEPTION:main java.lang.NullPointerException
因为listView
未初始化。
初始化getSavedVideo()
后调用listView
方法:
listView = (ListView)rootview.findViewById(R.id.savedVideoListView);
getSavedVideo();