片段中的GridView崩溃了

时间:2016-01-21 13:36:21

标签: android android-fragments gridview

我跟着this tutorial并创建了一个包含3个片段的布局,并使用ViewPager来切换它们,它运行得很好。然后我尝试将GridView添加到中间片段中,以便它可以作为图像库执行,就像它在this tutorial中显示的那样,但我不断得到“不幸的是,**已停止”消息。在一开始我认为可能是因为图像的大小太大而无法运行,所以我使用了第二个教程中的示例图像,但它没有用。

请帮助我离开,因为它困扰了我好几天。先感谢您。 :d

这是我的logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{my.com.impressor.demo2/my.com.impressor.demo2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
at my.com.impressor.demo2.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
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)

这是我的中间fragment.java,包含GridView:

public class Fragment2 extends Fragment {

private GridView gridView; //added regarding to the answer by Atual in <http://stackoverflow.com/questions/22613064/using-gridview-image-in-fragment>

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment2, container, false);
    GridView gridView = (GridView) view.findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));
    return view;
}

}

这是相关的XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    />

这是MainActivity:

public class MainActivity extends AppCompatActivity {

//ImageView im;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Toast.makeText(MainActivity.this, "" + position,
                    Toast.LENGTH_SHORT).show();
        }
    });

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Service"));
    tabLayout.addTab(tabLayout.newTab().setText("Gallery"));
    tabLayout.addTab(tabLayout.newTab().setText("Contact Us"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

/*public void biggerView(View v)
{
    im=(ImageView)findViewById(R.id.selected);

    switch (v.getId())
    {
        case R.id.image1: im.setImageResource(R.drawable.p1);
            break;
        case R.id.image2: im.setImageResource(R.drawable.p2);
            break;
        case R.id.image3: im.setImageResource(R.drawable.p3);
            break;
        case R.id.image4: im.setImageResource(R.drawable.p4);
            break;
        case R.id.image5: im.setImageResource(R.drawable.p5);
            break;
        case R.id.image6: im.setImageResource(R.drawable.p6);
            break;
        case R.id.image7: im.setImageResource(R.drawable.p7);
            break;
    }
}*/


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

这是ImageAdapter:

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        /*R.drawable.p1, R.drawable.p2,
        R.drawable.p3, R.drawable.p4,
        R.drawable.p5, R.drawable.p6,
        R.drawable.p7, R.drawable.p8,
        R.drawable.p9, R.drawable.p10,
        R.drawable.p11, R.drawable.p12,
        R.drawable.p13, R.drawable.p14,
        R.drawable.p15, R.drawable.p16,
        R.drawable.p17, R.drawable.p18,
        R.drawable.p19, R.drawable.p20,
        R.drawable.p21, R.drawable.p22,
        R.drawable.p23*/
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};
}

2 个答案:

答案 0 :(得分:1)

您的GridView R.id.gridview似乎不在布局R.layout.activity_main中。将其添加到布局中,或者如果要使用片段,则加载片段,GridView应位于Fragment2布局R.layout.fragment2内。

解决方案1 ​​

如果您选择使用Fragment2,请从MainActivity删除以下代码并在Fragment2内添加

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {
        Toast.makeText(MainActivity.this, "" + position,
                Toast.LENGTH_SHORT).show();
    }
});

解决方案2

如果您选择不使用Fragment2,请将以下代码添加到R.layout.activity_main

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>

其他建议

同时更改

public Object getItem(int position) {
    return null;
}

public Object getItem(int position) {
    return mThumbIds[position];
}

答案 1 :(得分:0)

据我所知,你的GridView位于你的片段中(fragment2布局而不是activity_main)。所以你应该在Fragment中移动代码来处理它,因为在你的Activity中GridView不可用。