【问题标题】:No view found error for a fragment with a ListView with a custom Adapter带有自定义适配器的 ListView 的片段未找到视图错误
【发布时间】:2016-11-26 18:53:29
【问题描述】:

我有一个活动(比如 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 和一个ImageView。

注意:我知道我搞砸了适配器,但不知道如何正确处理这种情况。

更新: 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" />

【问题讨论】:

  • 我很困惑为什么(List&lt;Movie&gt;) movies 会起作用。
  • 你能发布你的 logcat 吗?如果没有,请在发生异常的行中发布。
  • 用 Logcat 更新了问题。另外,@cricket_007 我正在使用 (List) 电影,因为没有演员表我无法使用 super。
  • 除非单数 Movie 对象实际上是一个列表,否则您不能执行该演员表。
  • 请添加您的activity_movie_detail.xml。该错误是不言自明的。您在该布局中没有 container1 id

标签: android listview android-fragments


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2013-05-21
    相关资源
    最近更新 更多