我是java新手,我已经构建了一个使用片段的应用程序。每个选项卡都将项目作为本地sqllite数据库中的列表打开。
数据库打开正常,但我收到了inflater的指针错误。
加载子片段类的Frament类
public class Fragment2 extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.viewpager_main, container, false);
// Locate the ViewPager in viewpager_main.xml
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
// Set the ViewPagerAdapter into ViewPager
mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
return view;
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
从我的子片段
获取数据的ListViewAdapter.java
类
public class ListViewAdapter extends SimpleCursorAdapter {
// Declare Variables
Context context;
int rank;
int[] flag;
LayoutInflater inflater;
public ListViewAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int rank) {
super(context, layout , cursor, from, to);
this.rank = rank;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Create the idno textview with background image
TextView idno = (TextView) view.findViewById(R.id.rank);
idno.setText(cursor.getString(3));
// create the material textview
TextView materials = (TextView) view.findViewById(R.id.country);
materials.setText(cursor.getString(1));
}
public int getCount() {
return rank;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtrank;
TextView txtcountry;
ImageView imgflag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Locate the TextViews in listview_item.xml
txtrank = (TextView) itemView.findViewById(R.id.rank);
txtcountry = (TextView) itemView.findViewById(R.id.country);
return itemView;
}
}
和我的FragmentTab.java
类从db
public class FragmentTab1 extends SherlockFragment {
private static final String DB_NAME = "animalsDB.sqllite3";
//������� ��������� �������� ������� ���� ����� �� �����������
private static final String TABLE_NAME = "animals";
private static final String FRIEND_ID = "_id";
private static final String FRIEND_NAME = "name";
private static final String FRIEND_GAME = "type";
public static final String[] ALL_KEYS = new String[] {FRIEND_ID, FRIEND_NAME, FRIEND_GAME};
private SQLiteDatabase database;
int rank;
int[] flag;
ListView list;
ListViewAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab1, container,
false);
ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(getActivity(), DB_NAME);
database = dbOpenHelper.openDataBase();
//���, ���� �������!
rank = (int) count();
Cursor cursor = getAllRows();
// Allow activity to manage lifetime of the cursor.
// DEPRECATED! Runs on the UI thread, OK for small/short queries.
getActivity().startManagingCursor(cursor);
// Setup mapping from cursor to view fields:
String[] fromFieldNames = new String[]
{FRIEND_ID, FRIEND_NAME, FRIEND_GAME };
int[] toViewIDs = new int[]
{ R.id.item_icon, R.id.item_name, R.id.item_game};
list = (ListView) rootView.findViewById(R.id.listview);
adapter = new ListViewAdapter(getActivity(), R.layout.fragmenttab1, cursor, fromFieldNames, toViewIDs, rank);
// Binds the Adapter to the ListView
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(getActivity(), SingleItemView.class);
// Pass all data rank
i.putExtra("rank", FRIEND_NAME);
// Pass all data country
i.putExtra("country", FRIEND_GAME);
// Pass a single position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}});
return rootView;
}
public Cursor getAllRows() {
String where = null;
Cursor c = database.query(true, TABLE_NAME, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public long count() {
return DatabaseUtils.queryNumEntries(database,"animals");
}
}
我得到的错误是在我的ListViewAdapter.java
课程中
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
错误日志:
12-26 13:32:07.056: E/AndroidRuntime(1389): FATAL EXCEPTION: main
12-26 13:32:07.056: E/AndroidRuntime(1389): java.lang.NullPointerException
12-26 13:32:07.056: E/AndroidRuntime(1389): at kf.kaunozoo.ListViewAdapter.getView(ListViewAdapter.java:64)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.ListView.makeAndAddView(ListView.java:1840)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.ListView.fillDown(ListView.java:675)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.ListView.fillFromTop(ListView.java:736)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.ListView.layoutChildren(ListView.java:1655)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.AbsListView.onLayout(AbsListView.java:2012)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:678)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.View.layout(View.java:14289)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.Choreographer.doFrame(Choreographer.java:532)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.os.Handler.handleCallback(Handler.java:730)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.os.Handler.dispatchMessage(Handler.java:92)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.os.Looper.loop(Looper.java:137)
12-26 13:32:07.056: E/AndroidRuntime(1389): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-26 13:32:07.056: E/AndroidRuntime(1389): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 13:32:07.056: E/AndroidRuntime(1389): at java.lang.reflect.Method.invoke(Method.java:525)
12-26 13:32:07.056: E/AndroidRuntime(1389): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-26 13:32:07.056: E/AndroidRuntime(1389): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-26 13:32:07.056: E/AndroidRuntime(1389): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
Context context
永远不会在Adapter类中初始化。因此,您得到NullPointerException
。
将其初始化为
Context context;
int rank;
int[] flag;
LayoutInflater inflater;
public ListViewAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int rank) {
super(context, layout , cursor, from, to);
this.rank = rank;
this.context = context; //missing
}