2015-01-14 5 views
0

listView.setAdapter()이 (가) 내 프로그램을 중단시키고 있습니다. 누구라도 충돌이 일어날 수있는 이유를 알고 있습니까?listView.setAdapter()가 내 프로그램을 크래시하고 있습니다.

로그 캣 오류 :

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> 

답변

3

AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException

listView 때문에이 초기화되지 않았습니다.

전화 getSavedVideo() 방법 listView 초기화 후 :

listView = (ListView)rootview.findViewById(R.id.savedVideoListView); 
getSavedVideo();