setAdapter()上的NullPointer也会触发InflateException

时间:2015-08-14 21:46:31

标签: android listview android-fragments layout-inflater custom-adapter

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

问题:首先,当我尝试在列表中调用setAdapter时,我收到NullPointerException。我谷歌废除了这个问题,但没有修复它。但与此同时,我得到了InflateException。 如果我注释掉setAdapter电话,那么就没有崩溃。但如果我离开它,两个原因都会显示在日志中。

有人可以说明问题可能是什么吗? 这是我的代码:

包含片段的活动

它只有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.access$800(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.access$800(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.access$800(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)

1 个答案:

答案 0 :(得分:0)

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

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

@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);
}