Java 帐户设置适配器问题
我创建了一个帐户设置页面,其中包含了通过片段打开的多个选项。然而,我似乎面临着一个我无法解决的问题。我尝试了多种解决方案,但似乎出现了错误。谢谢你的帮助 单击按钮打开AccountSettingsActivity时,应用程序崩溃。错误包括在下面 这是我的密码: AccountSettingsActivity.javaJava 帐户设置适配器问题,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,我创建了一个帐户设置页面,其中包含了通过片段打开的多个选项。然而,我似乎面临着一个我无法解决的问题。我尝试了多种解决方案,但似乎出现了错误。谢谢你的帮助 单击按钮打开AccountSettingsActivity时,应用程序崩溃。错误包括在下面 这是我的密码: AccountSettingsActivity.java import android.content.Context; import android.os.Bundle; import android.view.View; import
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import com.archive.pod.R;
import com.archive.pod.Utils.SectionsStatePagerAdapter;
import java.util.ArrayList;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
public class AccountSettingsActivity extends AppCompatActivity {
//Initializing
private Context mContext;
private SectionsStatePagerAdapter pagerAdapter;
private ViewPager mViewPager;
private RelativeLayout mRelativeLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accountsettings);
mContext = AccountSettingsActivity.this;
mViewPager = findViewById(R.id.container);
mRelativeLayout = findViewById(R.id.relLayout1);
//Account Settings Options List Initializer
setupSettingsList();
//Account Settings Options Fragment Initializer
setupFragments();
//Back Arrow Initialization for Navigation Back to "Profile Activity"
ImageButton backArrow = findViewById(R.id.back_arrow);
backArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
private void setupFragments() {
pagerAdapter = new SectionsStatePagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(new EditProfileFragment(), getString(R.string.editProfile)); //fragment 0
pagerAdapter.addFragment(new LogoutFragment(), getString(R.string.logout)); // fragment 1
}
//Allows for the overlay to show fragment on top of account settings activity
private void setViewPager(int fragmentNumber) {
mRelativeLayout.setVisibility(View.GONE);
mViewPager.setAdapter(pagerAdapter);
mViewPager.setCurrentItem(fragmentNumber);
}
//All Options in Account Settings
private void setupSettingsList() {
ListView listView = findViewById(R.id.lvAccountSettings);
ArrayList < Object > options = new ArrayList < > ();
options.add(("My Account"));
options.add(new AccountSettingsOptions("Edit Profile", "Change your profile information.")); //fragment 0
options.add(("Notifications"));
options.add(new AccountSettingsOptions("Notifications", "Manage preferences for notifications.")); //fragment 1
options.add(("Privacy Settings"));
options.add(new AccountSettingsOptions("Privacy", "Manage your privacy settings.")); //fragment 2
options.add(new AccountSettingsOptions("Privacy Policy", "Important for both of us.")); //fragment 3
options.add(("More"));
options.add(new AccountSettingsOptions("Ads", "Learn more about the ads you see.")); //fragment 4
options.add(new AccountSettingsOptions("Support", "Get help from us and the community.")); //fragment 5
options.add(new AccountSettingsOptions("Logout", "See you next time.")); //fragment 6
listView.setAdapter(new AccountSettingsAdapter(this, options));
//Navigation to fragment page
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView < ? > parent, View view, int position, long id) {
setViewPager(position);
}
});
}
}
活动\u accountsettings.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/layout_center_viewpager"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relLayout1">
<!-- *************** Top Section ***************** -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relLayout2">
<include layout="@layout/snippet_top_accountsettingtoolbar"/>
</RelativeLayout>
<!-- *************** Middle Section ***************** -->
<RelativeLayout
android:id="@+id/relLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/relLayout2"
android:padding="30dp"
android:background="@color/windowBackground">
<ListView
android:id="@+id/lvAccountSettings"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp">
</ListView>
</RelativeLayout>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我已经包括了所有的代码,所以请随意创建您自己的项目 在这里查看异常的堆栈跟踪
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)'
on a null object reference
at com.archive.pod.Profile.AccountSettingsAdapter.getView(AccountSettingsAdapter.java: 78)
指示正在对不存在且为null的TextView调用setText。
在AccountSettingsAdapter中,您正在使用以下布局对这些活动进行膨胀:
R.layout.activity\u账户设置或
R.layout.listview\u节\u标题\u帐户设置
stacktrace第78行表示找不到这些文本视图:
TextView name = view.findViewById(R.id.listViewTitle);
TextView description = view.findViewById(R.id.listViewDescription);
在上面创建的活动中,没有id与R.id.listViewTitle或/和R.id.listViewDescription中的一个或两个匹配的文本视图
您只需要在各自的目标活动中引用相应的视图。
尝试在switch语句中放置一个断点,以准确地确认发生了什么
固定发行
listView.setOnItemClickListener中存在语法错误。。。onItemClick(AdapterView父项…在通配符之前有一个额外的<左尖括号。从我可以看出,问题必须在
AccountSettingsAdapter.java的这一行中
case OPTIONS:
view = inflater.inflate(R.layout.activity_accountsettings, viewGroup, false);
您确定要为列表中的行扩展正确的视图吗?此布局适用于AccountSettingsActivity.java
,其中包含您尝试填充的listview。此外,此布局似乎不包含其他人在其评论中提到的R.id.listViewTitle
等字段,除非您包括ot她的视图在其xml中包含此内容。不知何故,在下面的代码中的AccountSettingsAdapter中
switch (getItemViewType(i)) {
case OPTIONS:
TextView name = view.findViewById(R.id.listViewTitle);
TextView description = view.findViewById(R.id.listViewDescription);
name.setText(((AccountSettingsOptions) list.get(i)).getName());
description.setText(((AccountSettingsOptions) list.get(i)).getDescription());
break;
case HEADER:
TextView title = view.findViewById(R.id.optionListViewHeader);
title.setText(((String) list.get(i)));
break;
名称和说明未成功初始化,因此名称会导致空obejcet引用错误。感谢您指出!奇怪的是,这实际上没有显示在我的实际代码中,但经过调整后,它并没有解决我的问题:(上面没有包括一些视图,即TextView name=view.findViewById(R.id.listViewTitle);TextView description=view.findViewById(R.id.listViewDescription);这些视图可以吗?-ie不可以为空。然后调试下面的列表是否提供任何细节?name.setText((AccountSettingsOptions)list.get(i)).getName();description.setText(((AccountSettingsOptions)list.get(i)).getDescription();因此当我将鼠标悬停在findViewById(R.id.listViewTitle)的顶部时;上面说,findViewById可能会产生“NullPointerException”。我不确定这是否是问题所在。关于你关于不包含某些视图的评论,我不确定你指的是什么。请你在回答中复制粘贴并调整我的代码,我将复制粘贴,看看是否有什么不同。非常感谢你的回复祝你好运!哈哈,非常感谢你能发布代码片段\u top\u accountsetingtoolbar.xml吗
TextView name = view.findViewById(R.id.listViewTitle);
TextView description = view.findViewById(R.id.listViewDescription);
case OPTIONS:
view = inflater.inflate(R.layout.activity_accountsettings, viewGroup, false);
switch (getItemViewType(i)) {
case OPTIONS:
TextView name = view.findViewById(R.id.listViewTitle);
TextView description = view.findViewById(R.id.listViewDescription);
name.setText(((AccountSettingsOptions) list.get(i)).getName());
description.setText(((AccountSettingsOptions) list.get(i)).getDescription());
break;
case HEADER:
TextView title = view.findViewById(R.id.optionListViewHeader);
title.setText(((String) list.get(i)));
break;