Android应用程序列表

时间:2012-07-01 14:13:56

标签: android android-layout android-intent

我创建了一个应用程序,我想将所有已安装的应用程序列出到自定义列表视图。不幸的是,活动在开始时崩溃了!任何人都可以帮我这个!我找不到错误,因为它没有给出任何错误!

AppActivity.java

package com.ex.layout;

import java.util.List;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class AppActivity extends Activity implements OnItemClickListener{

String[] name = getaAppName();
String[] packs = getPackName();


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView li = (ListView) findViewById(R.id.applist);
    ListviewAdapet list = new ListviewAdapet(this, name, packs);
    li.setAdapter(list);
    li.setOnItemClickListener(this);
}

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {  
    // TODO Auto-generated method stub  
    Toast.makeText(this,"Title => "+name[position]+"=> n Description"+packs[position], Toast.LENGTH_SHORT).show();  
}  

public String[] getaAppName() {
    List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
    String appname[] = new String[apps.size()];
    for (int number = 0; number < apps.size(); number++) {
        PackageInfo pack = apps.get(number);
        appname[number] = pack.applicationInfo.loadLabel(getPackageManager()).toString();
    }
    return appname;
}

public String[] getPackName(){
    List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
    String packnames[] = new String[apps.size()];
    for (int i = 0; i < apps.size(); i++) {
        PackageInfo pack = apps.get(i);
        packnames[i] = pack.packageName;
    }
    return packnames;
}

}

这是下一堂课!它扩展了baseadapter以获得输出。 listviewadapet.java

package com.ex.layout;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListviewAdapet extends BaseAdapter{

Activity context;
String[] appname , packname;

public ListviewAdapet(Activity Context, String[] appname , String[] packname) {
    // TODO Auto-generated constructor stub
    super();
    this.appname = appname;
    this.packname = packname;
    this.context = Context;

}

public class Viewholder{
    TextView appname;
    TextView packname;
}

public int getCount(String[] array) {
    // TODO Auto-generated method stub
    int count = array.length;
    return count;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Viewholder view;
    LayoutInflater inflator = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflator.inflate(R.layout.listitem, null);
        view = new Viewholder();
        view.appname = (TextView) convertView.findViewById(R.id.appname);
        view.packname= (TextView) convertView.findViewById(R.id.packagename);
        convertView.setTag(view);
    } else {
        view = (Viewholder) convertView.getTag();
    }

    view.appname.setText(appname[position]);
    view.packname.setText(packname[position]);
    return convertView;
}

public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

}

请帮帮我吧! 谢谢。

1 个答案:

答案 0 :(得分:1)

您在代码中遇到了两个严重错误。

错误1

String[] name = getaAppName();
String[] packs = getPackName();

在onCreate之前调用这两个方法.REMEMBER getPackageManager需要Activity上下文。如果你在onCreate之前定义它,你会得到该死的NullPointerException

所以改变你的代码

public class AppActivity extends Activity implements OnItemClickListener{

String[] name = getaAppName();
String[] packs = getPackName();


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

public class AppActivity extends Activity implements OnItemClickListener{

    String[] name;
    String[] packs;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        name = getaAppName();
        packs = getPackName();

错误2

public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

如果你在这里返回0,你将获得空列表。

所以将其改为

public int getCount() {
        return packname.length;
    }