我一直在尝试从Android片段中的数据库填充ListView。不幸的是,启动应用程序时,我在尝试填充并创建列表时得到NullPointerException
。
我在其中获取NullPointerException
的代码
public class AlarmFrag extends Fragment {
List<UserInputAlarmData> UserInputAlarmData = new ArrayList<UserInputAlarmData>();
DatabaseHandler dbHandler;
ListView AlarmListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_alarm, container, false);
AlarmListView = (ListView)rootView.findViewById(R.id.AlarmListView);
return rootView;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHandler = new DatabaseHandler(getActivity());
List<UserInputAlarmData> addableAlarmData = dbHandler.getAllAlarmData();
int AlarmDataCount = dbHandler.getAlarmDataCount();
for (int i = 0; i < AlarmDataCount; i++) {
UserInputAlarmData.add(addableAlarmData.get(i));
}
if (!addableAlarmData.isEmpty())
populateList();
}
private void populateList() {
ArrayAdapter<UserInputAlarmData> adapter = new AlarmListAdapter();
AlarmListView.setAdapter(adapter);
}
private void AddAlarm(int id, String name, String phone, String email, String address) {
UserInputAlarmData.add(new UserInputAlarmData(id, name, phone, email, address));
}
private class AlarmListAdapter extends ArrayAdapter<UserInputAlarmData> {
public AlarmListAdapter() {
super(getActivity(), R.layout.alarm_view_item, UserInputAlarmData);
}
}
}
引用的frag_alarm.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"
android:orientation="vertical"
android:background="#363">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="AlarmScreen"
android:textSize="20dp"
android:layout_centerInParent="true"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="100px"
android:text="Add New Alarm"
android:id="@+id/AlarmSetupButton"
android:onClick="AlarmSetupButtonClick"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ListView
android:layout_width="0dp"
android:layout_height="100dp"
android:id="@+id/AlarmListView"
android:layout_gravity="center"/>
</RelativeLayout>
引用的alarm_view_item.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="128dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Alarm Title:"
android:id="@+id/TitleName"
android:layout_weight="0.03" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Time:"
android:id="@+id/TimeName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Date:"
android:id="@+id/DateName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Song:"
android:id="@+id/SongName" />
</LinearLayout>
Logcat提供崩溃报告。
java.lang.NullPointerException
at com.goldin.foodtektrakr.AlarmFrag.populateList(AlarmFrag.java:49)
at com.goldin.foodtektrakr.AlarmFrag.onCreate(AlarmFrag.java:44)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:12752)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12752)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12752)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2267)
at android.view.View.measure(View.java:12752)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1074)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2462)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4532)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
在此先感谢您的帮助。我可以提供解决问题所需的任何其他类或xml文件。
最佳答案
在onCreateView()
之后调用onCreate()
方法。当您从populateList()
调用onCreate()
方法时,您的AlarmListView
尚未初始化。因此,只需将调用移至onCreateView()
方法即可解决您的问题。
完整的Fragment
生命周期如下:
不幸的是,我现在遇到另一个错误:ArrayAdapter要求资源ID为TextView
将您的AlarmListAdapter
实例化为
private class AlarmListAdapter extends ArrayAdapter<UserInputAlarmData> {
public AlarmListAdapter() {
super(getActivity(),
R.layout.alarm_view_item,
R.id.TitleName, // Passing TextView id
UserInputAlarmData);
}
}
虽然,这样可以消除错误,但是并不能完全满足您的期望。我看到您的行项目布局中有四个
TextView
,但是ArrayAdapter
只能将文本绑定到其中的一个。由于我们在此处传递了R.id.TitleName
,因此adpater仅将文本绑定到第一个TextView
。要拆分adpater项并在组成列表项的多个视图中显示数据,您需要扩展
ArrayAdapter
类并实现getView()
。这是在线提供的许多教程之一,它们显示了如何实现它们:ListView with Custom Adpater。