3

我正在尝试制作一个应用程序,它提供已安装应用程序的列表,当单击一个项目时,启动另一个活动,说明已安装应用程序所需的权限。我得到了已安装的应用程序列表,但是当我单击任何应用程序而不是开始新活动时,应用程序强制关闭。

主要活动

package com.example.appslist;

import java.util.List;
import com.example.appslist.adapter.ApkAdapter;
import com.example.appslist.app.AppData;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;


public class ApkListActivity extends Activity implements OnItemClickListener {

    PackageManager packageManager;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        packageManager = getPackageManager();
        List<PackageInfo> packageList = packageManager
                .getInstalledPackages(PackageManager.GET_PERMISSIONS);

        ListView mylistview= (ListView) findViewById(android.R.id.list);
        mylistview.setAdapter(new ApkAdapter(this, packageList, packageManager));



        mylistview.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long row) {
        PackageInfo packageInfo = (PackageInfo) parent
                .getItemAtPosition(position);
        AppData appData = (AppData) getApplicationContext();
        appData.setPackageInfo(packageInfo);

        Intent appInfo = new Intent(getApplicationContext(), ApkInfo.class);
        startActivity(appInfo);
    }
}

日志猫

04-02 11:16:04.536: E/asset(2777): MAS: getAppPckgAndVerCode package: com.example.appslist === version 1
04-02 11:16:04.559: D/dalvikvm(2777): Trying to load lib lib_glossary.so 0x0
04-02 11:16:04.559: I/dalvikvm(2777): Unable to dlopen(lib_glossary.so): Cannot load library: load_library[1083]: Library 'lib_glossary.so' not found
04-02 11:16:04.567: E/MBGlossaryManager(2777): lib_glossary.so can't be loaded
04-02 11:16:04.700: E/asset(2777): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-02 11:16:04.708: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-02 11:16:04.708: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-02 11:16:04.708: E/asset(2777): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-02 11:16:04.715: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-02 11:16:04.723: E/asset(2777): MAS: getAppPckgAndVerCode package: com.arcsoft.photoworkshop === version 5
04-02 11:16:04.778: D/dalvikvm(2777): GC_EXTERNAL_ALLOC freed 822 objects / 60704 bytes in 56ms
04-02 11:16:04.786: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.phone === version 8
04-02 11:16:04.794: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.calculator2 === version 8
04-02 11:16:04.794: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.htmlviewer === version 8
04-02 11:16:04.801: E/asset(2777): MAS: getAppPckgAndVerCode package: com.example.myfirstapp === version 1
04-02 11:16:04.801: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.providers.calendar === version 8
04-02 11:16:04.809: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.bluetooth === version 8
04-02 11:16:04.809: E/asset(2777): MAS: getAppPckgAndVerCode package: com.example.sessionmanagement === version 1
04-02 11:16:04.817: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.calendar === version 8
04-02 11:16:04.817: E/asset(2777): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-02 11:16:04.817: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-02 11:16:04.825: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-02 11:16:04.825: E/asset(2777): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-02 11:16:04.872: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-02 11:16:06.536: D/AndroidRuntime(2777): Shutting down VM
04-02 11:16:06.536: W/dalvikvm(2777): threadid=1: thread exiting with uncaught exception (group=0x400207e0)
04-02 11:16:06.551: E/AndroidRuntime(2777): FATAL EXCEPTION: main
04-02 11:16:06.551: E/AndroidRuntime(2777): java.lang.ClassCastException: android.app.Application
04-02 11:16:06.551: E/AndroidRuntime(2777):     at com.example.appslist.ApkListActivity.onItemClick(ApkListActivity.java:44)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at android.widget.ListView.performItemClick(ListView.java:3444)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at android.os.Handler.handleCallback(Handler.java:587)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at android.os.Looper.loop(Looper.java:143)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at android.app.ActivityThread.main(ActivityThread.java:4717)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at java.lang.reflect.Method.invokeNative(Native Method)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at java.lang.reflect.Method.invoke(Method.java:521)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-02 11:16:06.551: E/AndroidRuntime(2777):     at dalvik.system.NativeStart.main(Native Method)
04-02 11:16:08.137: I/Process(2777): Sending signal. PID: 2777 SIG: 9

我无法理解强制关闭的原因。请帮忙

应用程序数据.java

package com.example.appslist.app;

import android.app.Application;
import android.content.pm.PackageInfo;

public class AppData extends Application {

    PackageInfo packageInfo;

    public PackageInfo getPackageInfo() {
        return packageInfo;
    }

    public void setPackageInfo(PackageInfo packageInfo) {
        this.packageInfo = packageInfo;
    }
}

ApkAdapter.java

package com.example.appslist.adapter;

import java.util.List;
import com.example.appslist.R;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ApkAdapter extends BaseAdapter {
    List<PackageInfo> packageList;
    Activity context;
    PackageManager packageManager;

    public ApkAdapter(Activity context, List<PackageInfo> packageList,
            PackageManager packageManager) {
        super();
        this.context = context;
        this.packageList = packageList;
        this.packageManager = packageManager;
    }

    private class ViewHolder {
        TextView apkName;
    }

    public int getCount() {
        return packageList.size();
    }

    public Object getItem(int position) {
        return packageList.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.apklist_item, null);
            holder = new ViewHolder();

            holder.apkName = (TextView) convertView.findViewById(R.id.appname);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PackageInfo packageInfo = (PackageInfo) getItem(position);
        Drawable appIcon = packageManager
                .getApplicationIcon(packageInfo.applicationInfo);
        String appName = packageManager.getApplicationLabel(
                packageInfo.applicationInfo).toString();
        appIcon.setBounds(0, 0, 40, 40);
        holder.apkName.setCompoundDrawables(appIcon, null, null, null);
        holder.apkName.setCompoundDrawablePadding(15);
        holder.apkName.setText(appName);

        return convertView;
    }

}

清单文件

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.appslist"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
     >

    <activity
        android:name="com.example.appslist.ApkListActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ApkInfo"
        android:label="@string/title_activity_apk_info" >
    </activity>
</application>

4

4 回答 4

1

编辑:

看来您还没有在 Manifest 中注册 AppData

尝试添加

  android:name=".AppData"

在清单的应用程序标签中。

即这个

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".AppData"
         >

错误在这一行

 AppData appData = (AppData) getApplicationContext();

将其更改为

AppData appData = (AppData) getApplication();
于 2013-04-02T06:56:23.537 回答
1

您定义了一个名为 AppData 的新类,那么如何将 getApplicationContext() 返回的不是 AppData 实例的对象转换为 AppData?

例如,

class A{}

class B extends from A{}

class C extends from A{}

A foo(){
    return new C();
}

B b = (B)foo();

这肯定会导致类转换异常,因为类型为 C 的对象不是 B 的实例。

我认为您只需要向 ApkInfo.class 发送一些信息,那么为什么不使用 bundle 呢?

Bundle bundle = new Bundle(); 
bundle.putString("key", "data");   
bundle.putString("key2", "data2"); 
intent.setClass(...)
intent.putExtras(bundle); 
startActivity(intent);

更新:

然后就可以获取到你存储在目标activity中的信息了:

@Override
public void onCreate(...){
    Bundle bundle = getIntent().getExtras();
    bundle.getString("key");
}
于 2013-04-02T07:41:21.060 回答
0

这导致了问题:

PackageInfo packageInfo = (PackageInfo) parent.getItemAtPosition(position);

您正在尝试强制转换一个不同的对象来PackageInfo检查PackageInfo作为返回类型返回的不同方法以避免这种情况。

编辑:

尝试,

PackageInfo packageInfo = (PackageInfo) parent.getItemAtPosition(position).getClass().getPackage();

相反,我只是检查了它的视图对象。

于 2013-04-02T06:34:28.970 回答
0

问题在于您的清单应用程序文件。

请将您的以下行替换为

 android:name="com.ibc.android.demo.appslist.app.AppData"

下一行

android:name="com.ibc.android.demo.appslist.activity.AppData"

希望这会帮助你。

于 2013-04-02T07:29:32.023 回答