尝试调用虚方法' void android.widget.GridView.setAdapter(android.widget.ListAdapter)'在null对象引用上

时间:2016-03-20 22:39:53

标签: java android gridview adapter

我试图从api创建一个电影应用程序,但首先我做了一些假数据,以确保我的适配器工作正常所以我使用gridview制作我自己的适配器和片段中的一些数据我搜索了很多但我无法解决这是我自己的代码,提前谢谢。

MainActivity.java

    package besho.hallofmovies;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MovieFragment movieFragment = new MovieFragment();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(R.id.main_activity,movieFragment,"movieFragment");
    transaction.commit();
    }

    }

MovieFragment.java

    package besho.hallofmovies;

    import android.app.Fragment;
    import android.app.ListFragment;
    import android.content.Context;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import java.lang.reflect.Array;
    import java.util.ArrayList;

public class MovieFragment extends Fragment {

GridView myGridView ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.movie_fragment,menu);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_movie,container,false);
    myGridView = (GridView) getActivity().findViewById(R.id.gridView);
    myGridView.setAdapter(new MoviesAdapter(getActivity()));

    return view ;
}
}

class Movie
{
String name;
int movieID;

Movie (String name, int movieID)
{
    this.movieID=movieID;
    this.name=name;
}
}

class MoviesAdapter extends BaseAdapter
{
ArrayList<Movie> movies;
Context context;
MoviesAdapter (Context c)
{
    this.context=c;
    movies = new ArrayList<Movie>();
    Resources res = c.getResources();
    String[] moviesNames = res.getStringArray(R.array.values);
    int[] moviesImages = {R.drawable.pic_1,R.drawable.pic_2,R.drawable.pic_3,R.drawable.pic_4};

    for (int i=0;i<moviesNames.length;i++)
    {
        Movie tempMovie = new Movie(moviesNames[i],moviesImages[i]);
        movies.add(tempMovie);
    }
}

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

@Override
public Object getItem(int position) {
    return movies.get(position);
}

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

class ImageViewHolder
{
    ImageView myImage;
    ImageViewHolder(View v)
    {
        myImage = (ImageView) v.findViewById(R.id.imageView);
    }
}

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

    View item=convertView;
    ImageViewHolder myHolder;
    if (item == null)
    {
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       item = inflater.inflate(R.layout.item_layout,parent,false);
       myHolder = new ImageViewHolder(item);
       item.setTag(myHolder);
    }

    else
    {
        myHolder = (ImageViewHolder) item.getTag();
    }

    Movie temp = movies.get(position);
    myHolder.myImage.setImageResource(temp.movieID);
    return item;
}
}

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="besho.hallofmovies.MainActivity"
android:id="@+id/main_activity">



</RelativeLayout>

fragment_movie.xml

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<GridView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/gridView"
    android:layout_gravity="left|top"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:columnWidth="190dp"
    />

</FrameLayout>

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/movie_item_layout">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:scaleType="centerCrop"/>
</RelativeLayout>

logcat.sceenshot

1 个答案:

答案 0 :(得分:5)

您正在进入主机活动以找到gridView:

View view = inflater.inflate(R.layout.fragment_movie,container,false);
myGridView = (GridView) getActivity().findViewById(R.id.gridView);

这对片段没有意义。您想要查看膨胀的视图本身:

myGridView = view.findViewById(R.id.gridView);