listCountry = new ArrayList<String>();
listFlag = new ArrayList<String>();
listCountry.add("name");
listFlag.add("https://www.freepngimg.com/thumb/cartoon/4-2-cartoon-transparent.png");
listCountry.add("one");
listFlag.add("https://www.freepngimg.com/thumb/cartoon/4-2-cartoon-transparent.png");
mAdapter = new GridviewAdapter(getActivity(),listCountry, listFlag);
gridView.setAdapter(mAdapter);
上面的代码在
这是网格视图适配器.java代码
public class GridviewAdapter extends BaseAdapter {
private ArrayList<String> listCountry;
private ArrayList<String> listFlag;
private Activity activity;
public GridviewAdapter(Activity activity, ArrayList<String> listCountry, ArrayList<String> listFlag) {
super();
this.listCountry = listCountry;
this.listFlag = listFlag;
this.activity = activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listCountry.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listCountry.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.simple_list, null);
view.txtViewTitle = convertView.findViewById(R.id.textView1);
view.imgViewFlag = convertView.findViewById(R.id.imageView1);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(listCountry.get(position));
Picasso.get().load(listFlag.get(position)).resize(100,100).into(view.imgViewFlag);
return convertView;
}
}
适配器传递数据时应用程序崩溃 我也尝试过上下文。请检查并为我提供解决方案。当适配器将数据从片段传递到适配器时。然后应用程序崩溃,所以请为我提供一个解决方案,以便应用程序不会崩溃。
日志
2019-07-22 15:50:23.394 2238-2268/system_process E/memtrack: Couldn't load memtrack module
2019-07-22 15:50:25.313 21653-21787/com.android.chrome:sandboxed_process0 E/chromium: [ERROR:adm_helpers.cc(73)] Failed to query stereo recording.
2019-07-22 15:50:25.919 21550-21550/com.allinone E/chromium: [ERROR:web_contents_delegate.cc(175)] WebContentsDelegate::CheckMediaAccessPermission: Not supported.
2019-07-22 15:50:25.920 21550-21550/com.allinone E/chromium: [ERROR:web_contents_delegate.cc(175)] WebContentsDelegate::CheckMediaAccessPermission: Not supported.
2019-07-22 15:50:26.001 21550-21550/com.allinone E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.allinone, PID: 21550
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.allinone.Shopping.prepareList(Shopping.java:68)
at com.allinone.Shopping.onCreateView(Shopping.java:37)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:168)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1244)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:669)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:631)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:612)
at com.allinone.MainActivity$1.onTabSelected(MainActivity.java:94)
at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1608)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1601)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1569)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1874)
at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:2059)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
--------- beginning of system
2019-07-22 15:50:26.189 2238-2371/system_process E/InputDispatcher: channel 'f48eada com.allinone/com.allinone.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-07-22 15:50:26.202 21653-21668/com.android.chrome:sandboxed_process0 E/chromium: [ERROR:broker_posix.cc(106)] Error sending sync broker message: Broken pipe (32)
2019-07-22 15:50:26.202 21653-21668/com.android.chrome:sandboxed_process0 E/chromium: [ERROR:broker_posix.cc(106)] Error sending sync broker message: Broken pipe (32)
2019-07-22 15:50:26.384 2238-2272/system_process E/libprocessgroup: Error encountered killing process cgroup uid 99040 pid 21653: No such file or directory
2019-07-22 15:50:26.902 3498-3498/com.google.android.googlequicksearchbox:search E/OverlaySController: Calling with an invalid UID, the interface will not work
2019-07-22 15:50:26.971 1877-1882/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2019-07-22 15:50:27.148 1877-2113/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2019-07-22 15:50:28.332 3498-21345/com.google.android.googlequicksearchbox:search E/EntrySyncManager: Cannot determine account name: drop request
2019-07-22 15:50:28.332 3498-21345/com.google.android.googlequicksearchbox:search E/NowController: Failed to access data from EntryProvider. ExecutionException.
java.util.concurrent.ExecutionException: com.google.android.apps.gsa.sidekick.main.h.n: Could not complete scheduled request to refresh entries. ClientErrorCode: 3
at com.google.common.util.concurrent.d.eA(SourceFile:85)
at com.google.common.util.concurrent.d.get(SourceFile:23)
at com.google.common.util.concurrent.l.get(SourceFile:2)
at com.google.android.apps.gsa.staticplugins.nowstream.b.a.be.cbB(SourceFile:49)
at com.google.android.apps.gsa.staticplugins.nowstream.b.a.be.cbA(SourceFile:181)
at com.google.android.apps.gsa.staticplugins.nowstream.b.a.bh.run(Unknown Source:2)
at com.google.android.apps.gsa.shared.util.concurrent.at.run(SourceFile:4)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.google.android.apps.gsa.shared.util.concurrent.b.g.run(Unknown Source:4)
at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
at com.google.android.apps.gsa.shared.util.concurrent.b.i.run(SourceFile:6)
Caused by: com.google.android.apps.gsa.sidekick.main.h.n: Could not complete scheduled request to refresh entries. ClientErrorCode: 3
at com.google.android.apps.gsa.staticplugins.nowstream.b.a.ar.az(Unknown Source:4)
at com.google.common.util.concurrent.q.ap(SourceFile:7)
at com.google.common.util.concurrent.p.run(SourceFile:32)
at com.google.common.util.concurrent.bt.execute(SourceFile:3)
at com.google.common.util.concurrent.d.b(SourceFile:275)
at com.google.common.util.concurrent.d.addListener(SourceFile:135)
at com.google.common.util.concurrent.p.b(SourceFile:3)
at com.google.android.apps.gsa.shared.util.concurrent.h.a(SourceFile:16)
at com.google.android.apps.gsa.shared.util.concurrent.h.a(SourceFile:13)
at com.google.android.apps.gsa.staticplugins.nowstream.b.a.be.cbB(SourceFile:47)
at com.google.android.apps.gsa.staticplugins.nowstream.b.a.be.cbA(SourceFile:181)
at com.google.android.apps.gsa.staticplugins.nowstream.b.a.bh.run(Unknown Source:2)
at com.google.android.apps.gsa.shared.util.concurrent.at.run(SourceFile:4)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.google.android.apps.gsa.shared.util.concurrent.b.g.run(Unknown Source:4)
at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
at com.google.android.apps.gsa.shared.util.concurrent.b.aw.run(SourceFile:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
at com.google.android.apps.gsa.shared.util.concurrent.b.i.run(SourceFile:6)
2019-07-22 15:50:29.045 1873-3005/? E/AudioPolicyIntefaceImpl: getInputForAttr permission denied: recording not allowed for uid 10039 pid 3498
2019-07-22 15:50:29.045 1873-3005/? E/AudioFlinger: createRecord() checkRecordThread_l failed
2019-07-22 15:50:29.045 3498-21833/com.google.android.googlequicksearchbox:search E/IAudioFlinger: createRecord returned error -22
2019-07-22 15:50:29.045 3498-21833/com.google.android.googlequicksearchbox:search E/AudioRecord: AudioFlinger could not create record track, status: -22
2019-07-22 15:50:29.053 3498-21833/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -22.
2019-07-22 15:50:29.055 3498-21833/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
2019-07-22 15:50:29.057 3498-21833/com.google.android.googlequicksearchbox:search E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
2019-07-22 15:50:29.238 2872-21263/com.google.android.gms.persistent E/ctxmgr: [ProducerStatusImpl]updateStateForNewContextData: inactive, contextName=7 [CONTEXT service_id=47 ]
2019-07-22 15:50:29.431 2872-2872/com.google.android.gms.persistent E/BeaconBle: Missing BluetoothAdapter
2019-07-22 15:50:29.521 2872-2872/com.google.android.gms.persistent E/BeaconBle: Scan couldn't start for Places
2019-07-22 15:50:29.960 21604-21627/com.google.android.apps.nexuslauncher E/ActivityThread: Failed to find provider info for com.google.android.as.allapps.actionsettingprovider
2019-07-22 15:50:29.960 21604-21627/com.google.android.apps.nexuslauncher E/ActionsController: write setting failed
java.lang.IllegalArgumentException: Unknown URL content://com.google.android.as.allapps.actionsettingprovider
at android.content.ContentResolver.insert(ContentResolver.java:1583)
at com.google.android.apps.nexuslauncher.allapps.d.handleMessage(SourceFile:268)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:65)
2019-07-22 15:50:30.055 21604-21604/com.google.android.apps.nexuslauncher E/SchedPolicy: set_timerslack_ns write failed: Operation not permitted
这是因为在对
gridView
对象调用方法之前setAdapter()
尚未初始化该对象。首先,您必须使用 gridView = res.findViewById(R.id.your_grid_view_id_in_your_xml_file)
或 gridView = new GridView(context)
初始化它,然后调用 setAdapter()
方法。