android gridview 添加view,用TabLayout和ViewPager关联后,想把GridView添加到ViewPager里

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

用TabLayout和ViewPager关联后,想把GridView添加到ViewPager里面。报空指针异常。为什么

04-12 08:39:37.363 25278-25278/? E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.chenshenglei.tablayouttest, PID: 25278

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chenshenglei.tablayouttest/com.example.chenshenglei.tablayouttest.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:2646)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)

at android.app.ActivityThread.-wrap12(ActivityThread.java)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6077)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference

at com.example.chenshenglei.tablayouttest.MainActivity.onCreate(MainActivity.java:46)

at android.app.Activity.performCreate(Activity.java:6662)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)

下面贴代码MyGridViewAdapter.java

```

import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.widget.SimpleAdapter;

import java.util.List;

import java.util.Map;

/**

* Created by chenshenglei on 18-4-12.

*/

public class MyGridViewAdapter extends SimpleAdapter {

private Context mContext;

public MyGridViewAdapter(Context context, List> dataLists, int layout, String[] key, int[] item){

super(context, dataLists,layout, key, item);

this.mContext = context;

}

@Override

public int getCount() {

return 0;

}

@Override

public Object getItem(int position) {

return null;

}

@Override

public long getItemId(int position) {

return 0;

}

@Override

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

convertView = super.getView(position, convertView, parent);

return convertView;

}

}

```

代码TabPagerAdapter.java

```

package com.example.chenshenglei.tablayouttest;

import android.content.Context;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup;

import android.widget.GridView;

import android.widget.TextView;

import java.security.PublicKey;

import java.util.List;

/**

* Created by chenshenglei on 18-4-12.

*/

//自定义ViewPager页面适配器

public class TabPagerAdapter extends PagerAdapter {

private Context mContext;

private String[] mTitleArray;

private List mList;

public TabPagerAdapter (Context context, String[] title) {

super();

this.mContext = context;

this.mTitleArray = title;

}

public TabPagerAdapter (Context context, String[] title, List array) {

super();

this.mContext = context;

this.mTitleArray = title;

this.mList = array;

}

@Override

public int getCount() {

return mTitleArray.length;

}

@Override

public boolean isViewFromObject(View view, Object object) {

return view == object;

}

@Override

public Object instantiateItem(ViewGroup container, int position) {

View view = mList.get(position);

container.addView(view);

return view;

}

@Override

public void destroyItem(ViewGroup container, int position, Object object) {

container.removeView((View) object);

}

@Override

public CharSequence getPageTitle(int position) {

return mTitleArray[position];

}

}

```

代码 MainActivity.java

import android.support.design.widget.TabLayout;

import android.support.v4.view.ViewPager;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.GridView;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

private TabLayout mTabLayout;

private ViewPager mViewPager;

private String[] mTitleArray = {"酒标","葡萄品种"};

private GridView gridView1;

private GridView gridView2;

private MyGridViewAdapter myGridViewAdapter;

private List mList;

private List> dataList;

private List> dataList2;

private int[] img = {R.mipmap.ic_launcher_round,R.mipmap.ic_launcher};

private int[] img2 = {R.mipmap.ic_launcher_round,R.mipmap.ic_launcher};

private String[] imgName = {"1","2"};

private String[] imgName2 = {"3","4"};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_weibo_timeline);

gridView1 = (GridView)findViewById(R.id.grid_test);

dataList = new ArrayList>();

myGridViewAdapter = new MyGridViewAdapter(this,

getData(), R.layout.grid_item,

new String[] {"img", "txt"},

new int[] {R.id.img_item,R.id.txt_item});

gridView1.setAdapter(myGridViewAdapter);

gridView2 = (GridView)findViewById(R.id.grid_test);

dataList2 = new ArrayList>();

myGridViewAdapter = new MyGridViewAdapter(this,

getData2(), R.layout.grid_item,

new String[] {"img", "txt"},

new int[] {R.id.img_item,R.id.txt_item});

gridView2.setAdapter(myGridViewAdapter);

mList = new ArrayList();

mList.add(gridView1);

mList.add(gridView2);

mViewPager = (ViewPager) findViewById(R.id.view);

TabPagerAdapter adapter = new TabPagerAdapter(getApplicationContext(),mTitleArray,mList);

mViewPager.setAdapter(adapter);

mTabLayout = (TabLayout) findViewById(R.id.tab);

mTabLayout.setupWithViewPager(mViewPager);

}

private List> getData() {

for (int i=0; i

Map map = new HashMap();

map.put("img", img[i]);

map.put("txt", imgName[i]);

dataList.add(map);

}

return dataList;

}

private List> getData2() {

for (int i=0; i

Map map = new HashMap();

map.put("img", img2[i]);

map.put("txt", imgName2[i]);

dataList2.add(map);

}

return dataList2;

}

}

```

```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值