2016-11-26 78 views
0

我有一個活動(說B)與從父活動(說A)獲取對象的片段。該片段有一個列表視圖,我想在適配器的幫助下填充列表視圖。雖然我成功從父級活動獲取數據,但該應用崩潰,說沒有發現視圖。 下面的代碼:沒有發現錯誤的片段與ListView與自定義適配器

活性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和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" /> 

和,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

我很困惑,爲什麼'(名單)movies'會工作。 –

+0

你可以發佈你的logcat異常嗎?如果不是,請在發生異常的位置發帖。 – jonathanrz

+0

用Logcat更新了問題。此外,@ cricket_007我正在使用(列表)電影,因爲沒有投射我無法使用超級。 –

回答

0

的問題是在佈局被充氣時,適配器。編寫的代碼是:

convertView = inflater.inflate(R.layout.activity_movie_details, parent, false); 但它會導致NullPointerException錯誤而在ImageView的和TextView的爲Android的設置文字/圖片找不到,從那裏可以開始任何佈局。總而言之,佈局文件膨脹爲:activity_movie_details,它只包含一個框架佈局,並且沒有圖像/文本視圖。 不是代碼應該是:

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

相關問題