setAdapter() 上的 NullPointer 也会触发 InflateException

NullPointer on setAdapter() also triggers InflateException

所以,让我先解释一下场景。我有一个 activity 在其中获取一个字符串。我通过 intent extra 将该字符串传递给另一个 activity。第二个 activity 将承载一个使用该字符串执行搜索的片段,然后我在自定义 ListView 上显示我的结果。

问题:首先,当我尝试调用列表中的 setAdapter 时遇到 NullPointerException。我 google 解决了这个问题,但没有解决它。但同时我得到了一个 InflateException。 如果我注释掉 setAdapter 调用,则不会发生崩溃。但是如果我离开它,这两个原因都会显示在日志中。

有人可以阐明问题所在吗? 这是我的代码:

Activity 包含片段

它真的只有 onCreate 方法...

public class TracksContainerActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tracks_container);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

这里是 activity_tracks_container 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/include" />

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_artist_results"
    android:name="com.spadatech.spotifystreamer.fragments.ArtistListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.spadatech.spotifystreamer.fragments.ArtistListFragment"
    tools:layout="@android:layout/list_content" />

现在,片段

public class ArtistListFragment extends Fragment {
private static final String LOG_TAG = ArtistListFragment.class.getSimpleName();

ListView albumsList;
ArrayList<ArtistResults> mArtistResults;
String query;
ArtistArrayAdapter adapter;

public ArtistListFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    query = getActivity().getIntent().getStringExtra("query");
    adapter = new ArtistArrayAdapter(getActivity(), mArtistResults);

    if (savedInstanceState == null || !savedInstanceState.containsKey("artistResults")) {
        mArtistResults = new ArrayList<>();
        performSearch(query);
    } else {
        mArtistResults = savedInstanceState.getParcelableArrayList("artistResults");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = getActivity().getLayoutInflater().inflate(R.layout.fragment_artist_list, null);
    albumsList = (ListView) v.findViewById(R.id.lvAlbums);
    albumsList.setAdapter(adapter);
    return v;
}

}

这里是片段的布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.spadatech.spotifystreamer.fragments.ArtistListFragment">

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lvAlbums"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

我认为这可能有帮助,所以这是 日志

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spadatech.spotifystreamer/com.spadatech.spotifystreamer.activities.TracksContainerActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.spadatech.spotifystreamer.activities.TracksContainerActivity.onCreate(TracksContainerActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
            at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
            at android.widget.ListView.setAdapter(ListView.java:487)
            at com.spadatech.spotifystreamer.fragments.ArtistListFragment.onCreateView(ArtistListFragment.java:72)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:924)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116)
            at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1218)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2170)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
            at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:838)
            at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:34)
            at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:826)
            at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.spadatech.spotifystreamer.activities.TracksContainerActivity.onCreate(TracksContainerActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

您正在将一个空对象列表传递给您的适配器。

只需对您的代码进行以下更改。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    query = getActivity().getIntent().getStringExtra("query");
    if (savedInstanceState == null || !savedInstanceState.containsKey("artistResults")) {
        mArtistResults = new ArrayList<>();
        performSearch(query);
    } else {
        mArtistResults = savedInstanceState.getParcelableArrayList("artistResults");
    }
    adapter = new ArtistArrayAdapter(getActivity(), mArtistResults);
}