0

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

主要活动

 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

请帮忙。如果问题含糊不清,非常抱歉。我想不通,我尝试了很长时间。

提前致谢 :)

4

1 回答 1

2

这是因为您正在使用列表活动。当您使用 Activity 时,您的列表视图必须具有 id

在您的 xml 中设置 listview id

android:id="@android:id/list

像这样得到它

ListView yorulistview= (ListView) findViewById(android.R.id.list);
于 2013-04-01T12:05:02.383 回答