我正在构建Android Launcher,并在尝试将已安装的应用程序添加到GridView时不断崩溃。
我正在尝试在gridview中添加已安装应用的网格。
我的gridview代码是
private void loadGridView() {
grid = (GridView) getActivity().findViewById(R.id.content);
ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(getActivity(), android.R.layout.simple_list_item_1, apps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.drawer_item, null);
}
ImageView appIcon = (ImageView) convertView.findViewById(R.id.icon_image);
appIcon.setImageDrawable(apps.get(position).icon);
TextView appLabel = (TextView) convertView.findViewById(R.id.icon_text);
appLabel.setText(apps.get(position).label);
return convertView;
}
};
grid.setAdapter(adapter);
}
由于
编辑1 - 继承我的崩溃日志
Process: com.jenovaprojects.ubuntutouchlauncher, PID: 19156
Theme: themes:{}
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a
null object reference
at
com.jenovaprojects.ubuntutouchlauncher.homescreenfragments
.AppsScope.load
GridView(AppsScope.java:156)
at
com.jenovaprojects.ubuntutouchlauncher.homescreenfragments
.AppsScope.onCr
eateView(AppsScope.java:109)
at
android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at
android.support.v4.app.FragmentManagerImpl.moveToState
(FragmentManager.ja
va:1299)
at
android.support.v4.app.FragmentManagerImpl.
moveFragmentToExpectedState(Fr
agmentManager.java:1528)
at
android.support.v4.app.FragmentManagerImpl.
moveToState(FragmentManager.ja
va:1595)
at
android.support.v4.app.BackStackRecord.executeOps
(BackStackRecord.java:75
8)
at
android.support.v4.app.FragmentManagerImpl.executeOps
(FragmentManager.jav
a:2363)
at
android.support.v4.app.FragmentManagerImpl.executeOpsTogether
(FragmentMan
ager.java:2149)
at
android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps
(Fragment
Manager.java:2103)
at
android.support.v4.app.FragmentManagerImpl.execSingleAction
(FragmentManag
er.java:1984)
在 android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss (BackSta ckRecord.java:626)
at
android.support.v4.app.FragmentPagerAdapter.finishUpdate
(FragmentPagerAda
pter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:18799)
at
android.support.constraint.ConstraintLayout.internalMeasureChildren
(Const
raintLayout.java:934)
at
android.support.constraint.ConstraintLayout.onMeasure
(ConstraintLayout.ja
va:973)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins
(ViewGroup.java:5951)
at
android.support.design.widget.CoordinatorLayout.
onMeasureChild(Coordinato
rLayout.java:714)
at
android.support.design.widget.HeaderScrollingViewBehavior.
onMeasureChild(
HeaderScrollingViewBehavior.java:90)
at
android.support.design.widget.
AppBarLayout$ScrollingViewBehavior.onMeasur
eChild(AppBarLayout.java:1391)
at
android.support.design.widget.CoordinatorLayout.
onMeasure(CoordinatorLayo
ut.java:784)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at
android.support.v7.widget.ContentFrameLayout.onMeasure
(ContentFrameLayout
.java:139)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at
android.widget.LinearLayout.measureChildBeforeLayout
(LinearLayout.java:14
58)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at
android.widget.LinearLayout.measureChildBeforeLayout
(LinearLayout.java:14
58)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at
com.android.internal.policy.PhoneWindow$DecorView.onMeasure
(PhoneWindow.j
ava:2899)
at android.view.View.measure(View.java:18799)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.ja
答案 0 :(得分:1)
问题是(GridView) getActivity().findViewById(R.id.content)
返回null。这会导致崩溃:
grid.setAdapter(adapter);
要解决检查你的网格ID是什么,并将其放入:
grid = (GridView) getActivity().findViewById(R.id.your_grid_view);