使用片段

时间:2015-10-13 00:32:39

标签: android gridview nullpointerexception android-arrayadapter picasso

我正在从Udacity课程创建一个热门电影应用程序,我正在使用片段加载Picasso的图像。适配器似乎在getView内部遍历,然后崩溃并出现空指针异常。我已经阅读了很多其他帖子,但似乎没有人帮我解决我的问题。我不确定导致异常的原因。我已经检查并确保数组列表已填充。当我调试应用程序时,调试器进入适配器一次然后在return语句后崩溃。

我的适配器:MovieArrayAdapter.java

public class MovieArrayAdapter extends ArrayAdapter<String> {

private final String LOG_TAG = MovieArrayAdapter.class.getSimpleName();

private final Context mContext;
private ArrayList<String> mValues = new ArrayList<String>();

public MovieArrayAdapter(Context context,int layoutResId, ArrayList<String> values){
    super(context,layoutResId, values);
    this.mContext = context;
    this.mValues = values;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View rowView= convertView;

   if(rowView == null){

       LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.movie_list_detail, parent, false);
    }
    ImageView imageView = (ImageView) rowView.findViewById(R.id.movie_imageView_left);

    String url = getItem(position);
    Log.i(LOG_TAG, "url string: " + url);

    Picasso.with(getContext())
            .load(url)
            .fit()
            .into(imageView);

    return convertView;
}

@Override
public int getCount() {
    return mValues.size();
}

@Override
public String getItem(int position) {
    return mValues.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

}

My Fragment:DiscoveryActivityFragment.java

公共类DiscoveryActivityFragment扩展了Fragment {

private final String LOG_TAG = DiscoveryActivityFragment.class.getSimpleName();

private ArrayAdapter<String> mMovieAdapter;
private ArrayList<String> movies = new ArrayList<String>();

public DiscoveryActivityFragment() {
}

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


    GridView movieListView = (GridView) rootView.findViewById(R.id.moviesGridView);
    createMoviesArray();
    mMovieAdapter = new MovieArrayAdapter(
            getContext(),
            R.layout.movie_list_detail,
            movies
    );
    movieListView.setAdapter(mMovieAdapter);

    return rootView;
}

private void createMoviesArray() {

    movies.add("http://image.tmdb.org/t/p/w92//vlTPQANjLYTebzFJM1G4KeON0cb.jpg");
    movies.add("http://image.tmdb.org/t/p/w92//AjbENYG3b8lhYSkdrWwlhVLRPKR.jpg");
    movies.add("http://image.tmdb.org/t/p/w92//vlTPQANjLYTebzFJM1G4KeON0cb.jpg");
    movies.add("http://image.tmdb.org/t/p/w92//5BGjNYVHWynVNIbo3eF6XhPeB1J.jpg");
    movies.add("http://image.tmdb.org/t/p/w92//slobKil2T1ASQbItglLdGfAHJqC.jpg");

   /* movies.add("apples");
    movies.add("oranges");*/
}

}

我的主要活动:DiscoveryActivity.java

public class DiscoveryActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.discovery_main);
   if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new DiscoveryActivityFragment())
                .commit();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_discovery, menu);
    return true;
}

}

fragment_main.xml

<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.popularmovies.DiscoveryActivityFragment" >

<GridView
    android:id="@+id/moviesGridView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numColumns="auto_fit"
    android:gravity="center"/>

movie_list_detail.xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/movie_imageView_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

日志

10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:FATAL EXCEPTION:main 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:java.lang.NullPointerException 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:在android.widget.AbsListView.obtainView(AbsListView.java:2310) 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:at android.widget.GridView.onMeasure(GridView.java:1061) 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:at android.view.View.measure(View.java:15696) 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851) 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:at android.view.View.measure(View.java:15696) 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851) 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:at android.view.View.measure(View.java:15696) 10-12 19:59:21.264 10526-10526 / com.example.android.popularmovies E / AndroidRuntime:at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)

1 个答案:

答案 0 :(得分:0)

我需要在ArrayAdapter的getView方法中返回rowView而不是contextView。我正在rowView中完成所有工作,然后返回仍然为null的contextView。