親アクティビティ(Aなど)からオブジェクトを取得するフラグメントを持つアクティビティ(たとえばB)があります。フラグメントにはリストビューがあり、アダプターの助けを借りてリストビューを作成したい。私は親の活動から正常にデータを取得しますが、アプリケーションは、ビューが見つからないとクラッシュします。 は、ここでは、コードです:カスタムアダプタを使用したListViewを持つフラグメントの表示エラーが見つかりません
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;
}
}
アダプタ:は
今を更新している活動Bは、MovieDetailsActivityと呼ばれます
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は、リストビューとitem_movie_detail.xmlとのLinearLayoutを有しのTextViewとanImageViewとのLinearLayoutを有しています。
注:私はアダプターを台無しにしていますが、ケースを正しく処理する方法を理解していません。
更新:(アダプタ更新後) 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" />
と、
<?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" />
'(一覧)movies'が働くだろう、なぜ私は混乱しています。 –
あなたはlogcatを例外で掲示できますか?そうでない場合は、例外が発生した行を投稿してください。 – jonathanrz
Logcatで質問を更新しました。また、@ cricket_007私は(キャストなしで私はスーパーを使用することができないので、リスト)映画を使用しています。 –