カスタム BaseAdapter を使用してリストを作成しようとしています。nullpointerexception エラーが発生しています。データをアダプターに正しくロードできなかったか、ビューが間違っているようです。
以下は私のコードです:
FragmentActivity
public class NewsItemListActivity extends FragmentActivity implements NewsItemListFragment.Callbacks {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newsitem_list);
if (savedInstanceState != null) {
mActivePosition = savedInstanceState.getInt(STATE_ACTIVE_POSITION);
}
if (findViewById(R.id.newsitem_detail_container) != null) {
listfragment = ((NewsItemListFragment) getSupportFragmentManager()
.findFragmentById(R.id.newsitem_list));
listfragment.setActivateOnItemClick(true);
}
}
Activity_newsitem_list レイアウト
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/newsitem_list"
android:name="com.example.app.NewsItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context=".NewsItemListActivity"
tools:layout="@android:layout/list_content" />
リストフラグメント
public class NewsItemListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<NewsData>>{
List<NewsData> listdata;
CustomAdapter mListAdapter;
public NewsItemListFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Initially there is no data
setEmptyText("No Data Here");
mListAdapter = new CustomAdapter(getActivity(),listdata);
setListAdapter(mListAdapter);
// Start out with a progress indicator.
setListShown(false);
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<List<NewsData>> onCreateLoader(int arg0, Bundle arg1) {
return new ListLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<List<NewsData>> arg0,
List<NewsData> data) {
listdata = data;
mListAdapter.notifyDataSetChanged();
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}
@Override
public void onLoaderReset(Loader<List<NewsData>> arg0) {
listdata = null;
}
}
ベースアダプター
public class CustomAdapter extends BaseAdapter{
private List<NewsData> list;
Context context;
public CustomAdapter(Context c,List<NewsData> data){
context = c;
list = data;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.list_layout, parent, false);
} else {
view = convertView;
}
NewsData item = list.get(position);
((TextView)view.findViewById(R.id.subject)).setText(item.getSubject());
((TextView)view.findViewById(R.id.cdate)).setText(item.getCDate());
return view;
}
}
NewsData クラス
public class NewsData {
public String id;
public String subject;
public String cdate;
public NewsData(String id, String subject,String cdate) {
this.id = id;
this.subject = subject;
this.cdate = cdate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
........
}
ローダーを追加
@Override
public List<NewsData> loadInBackground() {
List<NewsData> entries = null;
.....
return entries.add(new NewsData(i,subject,cdate));
}
@Override
public void deliverResult(List<NewsData> listOfData) {
List<NewsData> oldNews = listOfData;
mNewsData = listOfData;
if (isStarted()) {
// If the Loader is currently started, we can immediately
// deliver its results.
super.deliverResult(listOfData);
}
}
エラーログ
04-05 07:31:17.951: E/AndroidRuntime(1495): FATAL EXCEPTION: main
04-05 07:31:17.951: E/AndroidRuntime(1495): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appapp/com.example.appapp.NewsItemListActivity}: java.lang.NullPointerException
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.os.Looper.loop(Looper.java:137)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-05 07:31:17.951: E/AndroidRuntime(1495): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 07:31:17.951: E/AndroidRuntime(1495): at java.lang.reflect.Method.invoke(Method.java:511)
04-05 07:31:17.951: E/AndroidRuntime(1495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-05 07:31:17.951: E/AndroidRuntime(1495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-05 07:31:17.951: E/AndroidRuntime(1495): at dalvik.system.NativeStart.main(Native Method)
04-05 07:31:17.951: E/AndroidRuntime(1495): Caused by: java.lang.NullPointerException
04-05 07:31:17.951: E/AndroidRuntime(1495): at com.example.appapp.CustomAdapter.getCount(CustomAdapter.java:25)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.widget.ListView.setAdapter(ListView.java:460)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.support.v4.app.ListFragment.setListAdapter(ListFragment.java:182)
04-05 07:31:17.951: E/AndroidRuntime(1495): at com.example.appapp.NewsItemListFragment.onActivityCreated(NewsItemListFragment.java:96)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.app.Activity.performStart(Activity.java:5018)
04-05 07:31:17.951: E/AndroidRuntime(1495): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
04-05 07:31:17.951: E/AndroidRuntime(1495): ... 11 more
長文すみません…