【发布时间】:2025-12-17 02:35:01
【问题描述】:
我正在尝试制作一个类似于Action Launcher 的应用程序,但我遇到了一些困难。我似乎无法让已安装的应用程序填充到片段中。
它要么崩溃,要么只是没有填充应用程序(应用程序视图)。我目前已将其设置为将所有应用程序加载到一个 ArrayList/List 中,该 ArrayList/List 安装了 90 个应用程序,因此我并不担心。它显示了我最难使用的实际应用程序。我正在尝试动态执行此操作,但它没有按我想要的方式工作。
/*
* Assume that
* GridView gv;
* Config cnf = new Config(); //personal class
* PackageManager pm;
* pm = getPackageManager();
*
*/
public void SelectItem(int possition) {
switch(possition){
case 2:
gv = (GridView) findViewById(R.id.gridView1);
// ArrayList<Apps> apps = cnf.getApps();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> apps = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
GridLayout gl = new GridLayout(returnContext());
gl.setOrientation(GridLayout.VERTICAL);
for (ResolveInfo appInfo : apps) {
String label = (String) appInfo.loadLabel(pm);
TextView name2 = new TextView(returnContext());
name2.setText(label);
gl.addView(name2);
ImageView img = new ImageView(returnContext());
img.setImageDrawable(appInfo.loadIcon(pm));
gl.addView(img);
}
gv.addView(gl); // I am getting a null reference exception here
}
}
以及上述代码的 Base XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="40dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
<ImageView
android:id="@+id/appicon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="2dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="6dip"
android:layout_marginTop="2dip"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/apptitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="20dip" />
</GridView>
</RelativeLayout>
我假设我的问题在于显而易见的问题(我没有正确设置值。)而不是更大的问题,即我不知道我在做什么并且我搞砸了一些事情(我不知道并且可能有)
我将我的来源上传到GitHub :)
干杯。
【问题讨论】:
标签: java android xml android-layout android-fragments