使用带自定义适配器的

时间:2016-11-26 18:53:29

标签: android listview android-fragments

我有一个活动(比方说B),一个片段从父活动中获取一个对象(比如说A)。该片段有一个列表视图,我想在适配器的帮助下填充列表视图。虽然我成功从父活动中获取数据,但应用程序崩溃说没有找到查看。 这是代码:

活动B,名为 MovieDetailsActivity:

public class MovieDetailsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_movie_detail);
    Bundle movieDetails = new Bundle();
    /**get the Movie's Object from the parent Activity**/
    Movie movie = getIntent().getParcelableExtra("movie");
    movieDetails.putParcelable("movie", movie);

    if (savedInstanceState == null) {
        MovieDetailsFragment defaultMovieFragment = new MovieDetailsFragment();
        defaultMovieFragment.setArguments(movieDetails);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container1, defaultMovieFragment)
                .commit();

    }
}
}

片段:

public class MovieDetailsFragment extends Fragment {

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_movie_detail, container, false);

    Bundle bundle = getArguments();

    if ((bundle != null)) {
        Movie movie = getArguments().getParcelable("movie");
        ArrayList<Movie> movieArrayList = new ArrayList<>();
        movieArrayList.add(movie);

        // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
        // adapter knows how to create list items for each item in the list.
        MovieDetailsAdapter adapter = new MovieDetailsAdapter(getActivity(), movieArrayList);

        // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
        // There should be a {@link ListView} with the view ID called list, which is declared in the
        // word_list.xml layout file.
        ListView listView = (ListView) rootView.findViewById(R.id.list_item);

        // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
        // {@link ListView} will display list items for each {@link Word} in the list.
        listView.setAdapter(adapter);
    }

    return rootView;
}
}

适配器: 已更新现在

public class MovieDetailsAdapter extends ArrayAdapter<Movie> {
// Store a member variable for the movies
// private static Movie mMovie;
// Store the context for easy access
private Context mContext;

// Pass in the movies array into the constructor
public MovieDetailsAdapter(Context context, ArrayList<Movie> movies) {
    super(context, 0, movies);
    //mMovie = movies;

    mContext = context;
}

// View lookup cache
private static class ViewHolder {
    TextView textView;
    ImageView imageView;

}

public View getView(int position, View convertView, ViewGroup parent) {
    Movie mMovie = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag
    if (convertView == null) {
        /* If there's no view to re-use, inflate a brand new view for row */
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(R.layout.activity_movie_details, parent, false);
        /*Find the TextView and ImageView and set them on the VIewHolder*/
        viewHolder.textView = (TextView) convertView.findViewById(R.id.movie_detail_title_text_view);
        viewHolder.imageView = (ImageView) convertView.findViewById(R.id.movie_detail_title_image_view);

        // Cache the viewHolder object inside the fresh view
        convertView.setTag(viewHolder);
    } else {
        // View is being recycled, retrieve the viewHolder object from tag
        viewHolder = (ViewHolder) convertView.getTag();
    }
    if (mMovie != null) {
        // Populate the data into the template view using the data object
        viewHolder.textView.setText(mMovie.getMovieTitle().toString());

        String url = "https://image.tmdb.org/t/p/w500/" + mMovie.getMoviePosterPath().toString();

        Picasso.with(mContext)
                .load(url)
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);
    }
    return convertView;
}
}

activity_movie_details.xml 有一个FrameLayout, fragment_movie_detail.xml 有一个带ListView的LinearLayout, item_movie_detail.xml 有一个带有TextView的LinearLayout和anImageView。

注意:我知道我搞砸了适配器,但不明白如何正确处理案例。

更新 Logcat消息(适配器更新后):

FATAL EXCEPTION: main
                                                                       Process: me.abhishekraj.showmyshow, PID: 17718
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                           at me.abhishekraj.showmyshow.MovieDetailsAdapter.getView(MovieDetailsAdapter.java:61)
                                                                           at android.widget.AbsListView.obtainView(AbsListView.java:2346)
                                                                           at android.widget.ListView.makeAndAddView(ListView.java:1875)
                                                                           at android.widget.ListView.fillDown(ListView.java:702)
                                                                           at android.widget.ListView.fillFromTop(ListView.java:763)
                                                                           at android.widget.ListView.layoutChildren(ListView.java:1684)
                                                                           at android.widget.AbsListView.onLayout(AbsListView.java:2148)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                           at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                           at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                           at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                           at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                           at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                                           at android.view.View.layout(View.java:16630)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                                                                           at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                                                                           at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                           at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                           at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                           at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                           at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                           at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

更新2: activity_movie_detail.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MovieDetailsActivity"
tools:ignore="MergeRootFrame" />

item_movie_detail.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="me.abhishekraj.showmyshow.MovieDetailsActivity"
tools:showIn="@layout/activity_movie_detail">

<ImageView
    android:id="@+id/movie_detail_title_image_view"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:scaleType="centerCrop"

    android:src="@mipmap/ic_launcher"
    app:layout_collapseMode="parallax"
    app:layout_collapseParallaxMultiplier="0.7" />

<TextView
    android:id="@+id/movie_detail_title_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:text="@string/large_text" />

1 个答案:

答案 0 :(得分:1)

问题出在布局膨胀的适配器中。编写的代码是:

convertView = inflater.inflate(R.layout.activity_movie_details, parent, false); 但是在ImageView和TextView上设置文本/图像时会导致NullPointerException错误,因为android无法找到可以启动它的任何布局。 总而言之,夸大的布局文件是:activity_movie_details,它只包含一个框架布局,没有图像/文本视图。 代码应该是:

convertView = inflater.inflate(R.layout.item_movie_detail, parent, false);