我一直在尝试从Android中的片段中的数据库填充ListView。不幸的是,在启动应用程序时,我在尝试填充时会得到NullpointerExeption。创建我的列表。 我获取NullPointerExeption的代码
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文件。
答案 0 :(得分:2)
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
只能将文本绑定到其中一个。{1}}。由于我们通过了R.id.TitleName
, adpater 只会将文本绑定到第一个TextView
。
要分割您的 adpater 项并在多个视图中显示构成列表项的数据,您需要扩展{{1类和实现ArrayAdapter
。这是在线提供的众多教程之一,展示了如何实施一个:ListView with Custom Adpater。
答案 1 :(得分:0)
您尚未初始化ListView AlarmListView;
因此任何参考变量的默认值为null
,这就是您获得Null Pointer Exception
所以当你拨打这一行时
AlarmListView.setAdapter(adapter);
实际上是
null.setAdapter(adapter);
/\
||
So Null Pointer Exception