gpt4 book ai didi

java - 简单的应用程序,显示智能手机中所有应用程序的列表及其图标

转载 作者:行者123 更新时间:2023-11-30 10:59:50 25 4
gpt4 key购买 nike

ListView 正确显示。但是,当我单击该项目时,它会抛出 NullPointerException。可能是什么原因?谢谢。ListView 显示正确。但是,当我单击该项目时,它会抛出 NullPointerException。可能是什么原因?谢谢。

package com.example.nerdlauncher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class NerdLauncherFragment extends ListFragment
{
private static final String TAG = "NerdLauncherFragment";
public ArrayList<TheApplication> icons = new ArrayList<TheApplication>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent startupIntent = new Intent(Intent.ACTION_MAIN);
startupIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager pm = getActivity().getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(startupIntent, 0);
Log.i(TAG, "I've found " + activities.size() + " activities.");

Collections.sort(activities, new Comparator<ResolveInfo>()
{
public int compare(ResolveInfo a, ResolveInfo b)
{
PackageManager pm = getActivity().getPackageManager();
return String.CASE_INSENSITIVE_ORDER.compare(a.loadLabel(pm).toString(), b.loadLabel(pm).toString());
}
});
for(int i = 0; i < activities.size(); i++)
{
icons.add(new TheApplication(activities.get(i).loadLabel(pm).toString(),activities.get(i).loadIcon(pm), activities.get(i)));
}
IconAdapter adapter = new IconAdapter(icons);
setListAdapter(adapter);




}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
TheApplication app = (TheApplication) getListAdapter().getItem(position);

ResolveInfo resolveInfo = app.getResolveInfo();
ActivityInfo activityInfo = resolveInfo.activityInfo; // exception was thrown from this line

if(activityInfo == null) return;

Intent i = new Intent();
i.setClassName(activityInfo.applicationInfo.packageName, activityInfo.name);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);


}
private class IconAdapter extends ArrayAdapter<TheApplication>
{
public IconAdapter(ArrayList<TheApplication> icons)
{
super(getActivity(), android.R.layout.simple_list_item_1, icons);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// if we weren't given a view, inflate one
if (null == convertView) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.list_item_app, null);
}

// configure the view for this Crime
TheApplication theApp = getItem(position);

TextView theTextView = (TextView)convertView.findViewById(R.id.text);
theTextView.setText(theApp.getName());

ImageView img = (ImageView) convertView.findViewById(R.id.icon);
img.setImageDrawable(theApp.getIcon());

return convertView;
}

}

}

应用类:

package com.example.nerdlauncher;

import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;

public class TheApplication {
private String mName;
private Drawable mIcon;
private ResolveInfo ri;
public TheApplication(String name, Drawable icon, ResolveInfo ri)
{
mName = name;
mIcon = icon;
}
public Drawable getIcon() {
return mIcon;
}
public String getName () {
return mName;
}
public ResolveInfo getResolveInfo()
{
return ri;
}
}

最佳答案

在构造函数中进行修改

public TheApplication(String name, Drawable icon, ResolveInfo ri)
{
this.mName = name;
this.mIcon = icon;
this.ri=ri;
}

关于java - 简单的应用程序,显示智能手机中所有应用程序的列表及其图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31774659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com