android空指针异常,求大神指教,已纠结好几天了。。。

package com.rong.shop.myshop.home;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import com.rong.shop.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
Created by Administrator on 2016/8/13.
*/
public class MyFragment_home extends Fragment {
//ViewPager
private ViewPager vpager_one;
private ArrayList aList;
private Adapter_homeViewPager mAdapter;
//GridView
private GridView gridview;
private ImageView imageview;
private Context mcontext = this.getActivity();
private List> listitem;
private SimpleAdapter gAdapter;
private int[] imgIds;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fg_home, container, false);
//ViewPager
vpager_one = (ViewPager) view.findViewById(R.id.vpager);
aList = new ArrayList();
LayoutInflater li = getLayoutInflater(null);
aList.add(li.inflate(R.layout.fg_home_viewpager_view_1, null, false));
aList.add(li.inflate(R.layout.fg_home_viewpager_view_2, null, false));
aList.add(li.inflate(R.layout.fg_home_viewpager_view_3, null, false));
aList.add(li.inflate(R.layout.fg_home_viewpager_view_4, null, false));
aList.add(li.inflate(R.layout.fg_home_viewpager_view_5, null, false));
mAdapter = new Adapter_homeViewPager(aList);
vpager_one.setAdapter(mAdapter);
//GridView
gridview = (GridView)view.findViewById(R.id.gridview);
listitem= new ArrayList<>();
imgIds = new int[]{
R.drawable.fg_home_gridview_1, R.drawable.fg_home_gridview_2,
R.drawable.fg_home_gridview_3, R.drawable.fg_home_gridview_4,
R.drawable.fg_home_gridview_5, R.drawable.fg_home_gridview_6,
R.drawable.fg_home_gridview_7, R.drawable.fg_home_gridview_8,
};
for (int i = 0; i < imgIds.length; i++) {
Map showitem = new HashMap();
showitem.put("image", imgIds[i]);
listitem.add(showitem);
}
gAdapter = new SimpleAdapter(mcontext, listitem, R.layout.fg_home_gridview_item,
new String[] { "image" }, new int[]{R.id.gridview_image});
gridview.setAdapter(gAdapter);这里是第75行报错的代码
return view;
}
}
报错信息是:
09-01 08:49:51.260 22924-22924/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rong.shop, PID: 22924
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rong.shop/com.rong.shop.myshop.home.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.widget.SimpleAdapter.(SimpleAdapter.java:85)
at com.rong.shop.myshop.home.MyFragment_home.onCreateView(MyFragment_home.java:75)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5241)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)

6个回答

at com.rong.shop.myshop.home.MyFragment_home.onCreateView(MyFragment_home.java:75) 看你Fragment第75行

前面的说的对 mcontext可能没初始化

去掉mcontext变量,直接在引用的地方换成getActivity()

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rong.shop/com.rong.shop.myshop.home.MainActivity}: java.lang.NullPointerException 你看看MainActivity有没有错误

u010165147
_pinnacle_ Context mcontext = this.getActivity();// 这句有问题 getActivity()需要在onActivityCreated()中或者之后调用,不然返回的就是Null
接近 4 年之前 回复
dragonrong_
dragonrong8 avtivity没有错误,就是在fragment里加了一个gridview就出错了
接近 4 年之前 回复

private Context mcontext = this.getActivity();

在onCreateView 中获取看看

第75行错误时哪一行

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问