gpt4 book ai didi

java - 尝试填充 fragment 中的列表时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 04:52:17 28 4
gpt4 key购买 nike

我一直在尝试从 Android 中的 fragment 内的数据库填充 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() 之后调用。当您从 onCreate() 调用 populateList() 方法时,您的 AlarmListView 尚未初始化。因此,只需将调用移至 onCreateView() 方法即可解决您的问题。

完整的Fragment生命周期如下:

Fragment Lifecycle

<小时/>

I am now getting another error unfortunately: ArrayAdapter requires the resource ID to be a 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 只能将文本绑定(bind)到其中一个。因为我们在那里传递了 R.id.TitleName,所以 adpater 只会将文本绑定(bind)到第一个 TextView

要拆分您的 adpater 项并在构成列表项的多个 View 中显示数据,您需要扩展 ArrayAdapter 类并实现 getView()。以下是在线提供的众多教程之一,展示了如何实现:ListView with Custom Adpater .

关于java - 尝试填充 fragment 中的列表时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29106016/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com