我正在创建Android启动器,它有多个带有滑动手势的标签,可以从一个片段到另一个片段,除了ShelfViewActivity(尽管它说活动是一个片段)之外,所有屏幕都工作正常但一切正常但是当我滚动到ShelfViewActivity时,启动器崩溃了。我这里有logcat以及类。错误在第35行我想......我已经评论了第35行是什么。我不知道我做错了什么我还在学习所以请不要判断
logcat的:
11-12 05:30:55.337: E/AndroidRuntime(3889): FATAL EXCEPTION: main
11-12 05:30:55.337: E/AndroidRuntime(3889): java.lang.NullPointerException
11-12 05:30:55.337: E/AndroidRuntime(3889): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
11-12 05:30:55.337: E/AndroidRuntime(3889): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
11-12 05:30:55.337: E/AndroidRuntime(3889): at com.d4a.tobias.ShelfViewActivity$VerticalAdapter.<init>(ShelfViewActivity.java:51)
11-12 05:30:55.337: E/AndroidRuntime(3889): at com.d4a.tobias.ShelfViewActivity.<init>(ShelfViewActivity.java:35)
11-12 05:30:55.337: E/AndroidRuntime(3889): at it.gmariotti.android.examples.googleaccount.MainFrag$MyAdapter.getItem(MainFrag.java:80)
11-12 05:30:55.337: E/AndroidRuntime(3889): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:95)
ShelfViewActivity.java
package com.d4a.tobias;
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ShelfViewActivity extends ListFragment {
/** Called when the activity is first created. */
private VerticalAdapter verListAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, container, false);
}
/*
* Calling Library & BookItem classes for create list of groups
* groupbyArrayBookItem return back array of array of items
*/
Library lb = new Library();{
for (BookItem item : BookItem.ALL_BOOKS) {
lb.addBookItem(item);
}
ArrayList<ArrayList<BookItem>> groupList = new ArrayList<ArrayList<BookItem>>();
groupList = lb.groupbyArrayBookItem(Library.AUTHOR);
verListAdapter = new VerticalAdapter(getActivity(), R.layout.bookrow, groupList); //line 35
setListAdapter(verListAdapter);
verListAdapter.notifyDataSetChanged();
}
/**
* This class add a list of ArrayList to ListView that it include multi
* items as bookItem.
*/
private class VerticalAdapter extends ArrayAdapter<ArrayList<BookItem>> {
private int resource;
public VerticalAdapter(Context _context, int _ResourceId,
ArrayList<ArrayList<BookItem>> _items) {
super(_context, _ResourceId, _items);
this.resource = _ResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
if (convertView == null) {
rowView = LayoutInflater.from(getContext()).inflate(resource,
null);
} else {
rowView = convertView;
}
HorizontalListView hListView = (HorizontalListView) rowView
.findViewById(R.id.subListview);
HorizontalAdapter horListAdapter = new HorizontalAdapter(
getContext(), R.layout.item, getItem(position));
hListView.setAdapter(horListAdapter);
return rowView;
}
}
/*
* This class add some items to Horizontal ListView this ListView include
* several bookItem.
*/
private class HorizontalAdapter extends ArrayAdapter<BookItem> {
private int resource;
public HorizontalAdapter(Context _context, int _textViewResourceId,
ArrayList<BookItem> _items) {
super(_context, _textViewResourceId, _items);
this.resource = _textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(getContext()).inflate(
this.resource, null);
TextView topText = (TextView) retval.findViewById(R.id.title);
TextView bottomText = (TextView) retval
.findViewById(R.id.author);
topText.setText(getItem(position).getAuthor());
bottomText.setText(getItem(position).getTitle());
return retval;
}
}
}
任何帮助都会很棒
提前谢谢!
答案 0 :(得分:0)
尝试更改此内容:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, container, false);
}
/*
* Calling Library & BookItem classes for create list of groups
* groupbyArrayBookItem return back array of array of items
*/
Library lb = new Library();{
for (BookItem item : BookItem.ALL_BOOKS) {
lb.addBookItem(item);
}
ArrayList<ArrayList<BookItem>> groupList = new ArrayList<ArrayList<BookItem>>();
groupList = lb.groupbyArrayBookItem(Library.AUTHOR);
verListAdapter = new VerticalAdapter(getActivity(), R.layout.bookrow, groupList); //line 35
setListAdapter(verListAdapter);
verListAdapter.notifyDataSetChanged();
}
对此:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
/*
* Calling Library & BookItem classes for create list of groups
* groupbyArrayBookItem return back array of array of items
*/
Library lb = new Library();{
for (BookItem item : BookItem.ALL_BOOKS) {
lb.addBookItem(item);
}
ArrayList<ArrayList<BookItem>> groupList = new ArrayList<ArrayList<BookItem>>();
groupList = lb.groupbyArrayBookItem(Library.AUTHOR);
verListAdapter = new VerticalAdapter(getActivity(), R.layout.bookrow, groupList); //line 35
setListAdapter(verListAdapter);
verListAdapter.notifyDataSetChanged();
return rootView;
}