我正在完成Google Android教程的一部分(准确地说是this part)而且我遇到了麻烦。我正在尝试将ListView的适配器设置为自定义ArrayAdapter。不幸的是,findViewById似乎返回null(这意味着我无法设置适配器)。
以下是相关代码:
MainActivityFragment
private final String LOG_TAG = MainActivityFragment.class.getSimpleName();
private ArtistItemAdapter artistsAdapter;
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
artistsAdapter = new ArtistItemAdapter(getActivity(), R.layout.list_item_artist, new Pager<Artist>().items);
ListView artistsList = (ListView) rootView.findViewById(R.id.listViewArtists);
artistsList.setAdapter(artistsAdapter);
return rootView;
}
@Override
public void onStart() {
EditText searchBar = (EditText) getView().findViewById(R.id.searchEditText);
searchBar.setOnEditorActionListener(new TextView.OnEditorActionListener() {
ArtistsPager artists;
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
FetchArtistsTask artistsTaskCall = new FetchArtistsTask();
artistsTaskCall.execute(String.valueOf(v.getText()));
handled = true;
}
return handled;
}
});
}
MainActivity.onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
fragment_main.xml
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/searchEditText"
android:inputType="textFilter"
android:imeOptions="actionSearch"
android:drawableLeft="@android:drawable/ic_menu_search"
android:hint="Search Artist"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listViewArtists"
android:layout_below="@+id/searchEditText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
activity_main.xml中
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.example.android.spotifyproject.MainActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
LogCat: http://pastebin.com/CUNF05Af
答案 0 :(得分:0)
ListView不是null,它是您在创建时发送到适配器的数据,因此当您尝试在ListView上设置适配器时,它调用List.size()关于数据(null),以及错误所在的位置。