You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android Fragment联系人列表调用setAdapter时崩溃(空指针)求助

Fixing the NullPointerException in Your Contacts Fragment

Hey there! Let's get that crash sorted out—you're hitting a NullPointerException because your contactsList variable is null when you try to call setAdapter() on it. Here's exactly what's going wrong and how to fix it:

What's Causing the Crash?

Two key issues are combining to break your code:

  1. Wrong ID lookup: Your Fragment's layout (contact_list.xml) defines the ListView with ID contact_list_view, but you're trying to find it using R.id.contact_list—that's a mismatch!
  2. Incorrect view context: You're using getActivity().findViewById() to look for the ListView, but that searches the Activity's layout, not your Fragment's independent layout. The Fragment's views only exist within the view you inflated in onCreateView.

Step-by-Step Fix

1. Update Your Fragment to Use Its Own View

Instead of relying on the Activity to find your Fragment's views, use the view you inflated in onCreateView. The cleanest way to do this is to use onViewCreated() (a lifecycle method designed specifically for post-view-creation setup) instead of onActivityCreated():

Modify your ContactsFragment.java code like this:

public class ContactsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener {
    // ... keep your existing member variables ...
    ListView contactsList;
    SimpleCursorAdapter cursorAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Just inflate and return the view—setup goes in onViewCreated
        return inflater.inflate(R.layout.contact_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // Use the Fragment's own view to find the ListView (matches the ID in contact_list.xml)
        contactsList = view.findViewById(R.id.contact_list_view);
        
        // Initialize your adapter and set it on the ListView
        cursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.contact_list_item, null, FROM_COLUMNS, TO_IDS, 0);
        contactsList.setAdapter(cursorAdapter);
        contactsList.setOnItemClickListener(this);
    }

    // ... keep your other existing methods (onCreate, loader callbacks, onItemClick) ...
}

2. Double-Check Layout IDs

Make sure your contact_list.xml still has the correct ID for the ListView (which it does, based on your code):

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contact_list_view" <!-- This matches the ID we're using in onViewCreated -->
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>

Why This Works

  • onViewCreated() is called right after your Fragment's view is fully inflated, so you know the ListView exists at this point.
  • Using view.findViewById() (where view is the Fragment's own inflated layout) ensures you're searching the correct set of views, not the Activity's layout.
  • We've fixed the ID mismatch between the layout and the Java code, so the ListView is found successfully.

Once you make these changes, the NullPointerException should disappear, and your Fragment should load the ListView properly. After that, you can move on to implementing the search functionality without layout-related crashes.

内容的提问来源于stack exchange,提问作者humble_barnacle

火山引擎 最新活动