我创建了一个帐户设置页面,其中包含通过片段打开的多个选项。但是,我似乎面临着一个我无法解决的问题。我尝试了多种解决方案,但似乎会出错。感谢您的帮助!
单击该按钮以打开AccountSettingsActivity时,应用程序崩溃。错误如下。
这是我的代码:
AccountSettingsActivity.java
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);
}
});
}
}
AccountSettingsAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.archive.pod.R;
import java.util.ArrayList;
public class AccountSettingsAdapter extends BaseAdapter {
ArrayList < Object > list;
private static final int OPTIONS = 0;
private static final int HEADER = 1;
private LayoutInflater inflater;
public AccountSettingsAdapter(Context context, ArrayList < Object > list) {
this.list = list;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getItemViewType(int position) {
if (list.get(position) instanceof AccountSettingsOptions) {
return OPTIONS;
} else {
return HEADER;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
/** This distinguishes between section title and list view options,
then using switch it beings to populate the options in the list
view with titles and descriptions */
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
switch (getItemViewType(i)) {
case OPTIONS:
view = inflater.inflate(R.layout.activity_accountsettings, viewGroup, false);
break;
case HEADER:
view = inflater.inflate(R.layout.listview_section_header_accountsettings, viewGroup, false);
break;
}
}
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;
}
return view;
}
}
AccountSettingsOptions.java
public class AccountSettingsOptions {
private String name;
private String description;
public AccountSettingsOptions(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
activity_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>
错误:
E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.archive.pod, PID: 22146
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)
at android.widget.AbsListView.obtainView(AbsListView.java: 2405)
at android.widget.ListView.makeAndAddView(ListView.java: 2071)
at android.widget.ListView.fillDown(ListView.java: 797)
at android.widget.ListView.fillFromTop(ListView.java: 859)
at android.widget.ListView.layoutChildren(ListView.java: 1842)
at android.widget.AbsListView.onLayout(AbsListView.java: 2202)
at android.view.View.layout(View.java: 22466)
at android.view.ViewGroup.layout(ViewGroup.java: 6304)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java: 1103)
at android.view.View.layout(View.java: 22466)
at android.view.ViewGroup.layout(ViewGroup.java: 6304)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java: 1103)
at android.view.View.layout(View.java: 22466)
at android.view.ViewGroup.layout(ViewGroup.java: 6304)
at androidx.coordinatorlayout.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java: 1183)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java: 870)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java: 889)
at android.view.View.layout(View.java: 22466)
at android.view.ViewGroup.layout(ViewGroup.java: 6304)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java: 332)
at android.widget.FrameLayout.onLayout(FrameLayout.java: 270)
at android.view.View.layout(View.java: 22466)
at android.view.ViewGroup.layout(ViewGroup.java: 6304)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java: 1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java: 1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java: 1582)
at android.view.View.layout(View.java: 22466)
at android.view.ViewGroup.layout(ViewGroup.java: 6304)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java: 332)
at android.widget.FrameLayout.onLayout(FrameLayout.java: 270)
at android.view.View.layout(View.java: 22466)
at android.view.ViewGroup.layout(ViewGroup.java: 6304)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java: 1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java: 1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java: 1582)
at android.view.View.layout(View.java: 22466)
at android.view.ViewGroup.layout(ViewGroup.java: 6304)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java: 332)
at android.widget.FrameLayout.onLayout(FrameLayout.java: 270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java: 779)
at android.view.View.layout(View.java: 22466)
at android.view.ViewGroup.layout(ViewGroup.java: 6304)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java: 3157)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java: 2614)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java: 1745)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java: 7768)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java: 967)
at android.view.Choreographer.doCallbacks(Choreographer.java: 791)
at android.view.Choreographer.doFrame(Choreographer.java: 726)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java: 952)
at android.os.Handler.handleCallback(Handler.java: 883)
at android.os.Handler.dispatchMessage(Handler.java: 100)
at android.os.Looper.loop(Looper.java: 214)
at android.app.ActivityThread.main(ActivityThread.java: 7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java: 492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 930)
我已经包含了所有代码,请随时创建您自己的项目!
最佳答案
listView.setOnItemClickListener ... onItemClick(AdapterView < parent上有语法错误。通配符前还有一个
不知道这是否是错误的原因。