我正在尝试创建一个应用程序,该应用程序提供已安装应用程序的列表,并在单击项目时启动另一个活动,该活动指出已安装应用程序所需的权限。我获得了已安装的应用程序列表,但是当我单击任何应用程序而不是启动新活动时,应用程序强制关闭。
MainActivity
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) getApplication();
appData.setPackageInfo(packageInfo);
Intent appInfo = new Intent(ApkListActivity.this, ApkInfo.class);
startActivity(appInfo);
}
}
ApkInfo.java
package com.example.appslist;
import android.os.Bundle;
import android.app.Activity;
import com.example.appslist.app.AppData;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageInfo;
import android.widget.TextView;
public class ApkInfo extends Activity {
TextView appLabel, packageName, version, features;
TextView permissions, andVersion,path;
PackageInfo packageInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apkinfo);
findViewsById();
AppData appData = (AppData) getApplicationContext();
packageInfo = appData.getPackageInfo();
setValues();
}
private void findViewsById() {
appLabel = (TextView) findViewById(R.id.applabel);
packageName = (TextView) findViewById(R.id.package_name);
version = (TextView) findViewById(R.id.version_name);
features = (TextView) findViewById(R.id.req_feature);
permissions = (TextView) findViewById(R.id.req_permission);
andVersion = (TextView) findViewById(R.id.andversion);
path = (TextView) findViewById(R.id.path);
}
private void setValues() {
// APP name
appLabel.setText(getPackageManager().getApplicationLabel(
packageInfo.applicationInfo));
// package name
packageName.setText(packageInfo.packageName);
// version name
version.setText(packageInfo.versionName);
// target version
andVersion.setText(Integer
.toString(packageInfo.applicationInfo.targetSdkVersion));
// path
path.setText(packageInfo.applicationInfo.sourceDir);
// features
if (packageInfo.reqFeatures != null)
features.setText(getFeatures(packageInfo.reqFeatures));
else
features.setText("-");
// uses-permission
if (packageInfo.requestedPermissions != null)
permissions
.setText(getPermissions(packageInfo.requestedPermissions));
else
permissions.setText("-");
}
// Convert string array to comma separated string
private String getPermissions(String[] requestedPermissions) {
String permission = "";
for (int i = 0; i < requestedPermissions.length; i++) {
permission = permission + requestedPermissions[i] + ",\n";
}
return permission;
}
// Convert string array to comma separated string
private String getFeatures(FeatureInfo[] reqFeatures) {
String features = "";
for (int i = 0; i < reqFeatures.length; i++) {
features = features + reqFeatures[i] + ",\n";
}
return features;
}
}
AppData类
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;
}
}
我已经尝试在清单中注册AppData,尝试使用bundle,强制关闭。请帮忙 。
清单文件
<?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>
</manifest>
这是LogCat
04-09 12:17:47.305: E/asset(5752): MAS: getAppPckgAndVerCode package: com.example.appslist === version 1
04-09 12:17:47.329: D/dalvikvm(5752): Trying to load lib lib_glossary.so 0x0
04-09 12:17:47.329: I/dalvikvm(5752): Unable to dlopen(lib_glossary.so): Cannot load library: load_library[1083]: Library 'lib_glossary.so' not found
04-09 12:17:47.329: E/MBGlossaryManager(5752): lib_glossary.so can't be loaded
04-09 12:17:47.469: E/asset(5752): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-09 12:17:47.469: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-09 12:17:47.477: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-09 12:17:47.477: E/asset(5752): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-09 12:17:47.485: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-09 12:17:47.493: E/asset(5752): MAS: getAppPckgAndVerCode package: com.example.backgroundapp === version 1
04-09 12:17:47.493: E/asset(5752): MAS: getAppPckgAndVerCode package: com.arcsoft.photoworkshop === version 5
04-09 12:17:47.548: D/dalvikvm(5752): GC_EXTERNAL_ALLOC freed 934 objects / 65512 bytes in 49ms
04-09 12:17:47.555: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.phone === version 8
04-09 12:17:47.563: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.calculator2 === version 8
04-09 12:17:47.563: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.htmlviewer === version 8
04-09 12:17:47.563: E/asset(5752): MAS: getAppPckgAndVerCode package: com.example.myfirstapp === version 1
04-09 12:17:47.571: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.providers.calendar === version 8
04-09 12:17:47.571: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.bluetooth === version 8
04-09 12:17:47.571: E/asset(5752): MAS: getAppPckgAndVerCode package: com.example.sessionmanagement === version 1
04-09 12:17:47.579: E/asset(5752): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-09 12:17:47.579: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-09 12:17:47.587: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-09 12:17:47.587: E/asset(5752): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-09 12:17:47.594: E/asset(5752): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-09 12:17:47.641: E/asset(5752): MAS: getAppPckgAndVerCode package: com.example.backgroundapp === version 1
04-09 12:17:47.782: W/IInputConnectionWrapper(5752): showStatusIcon on inactive InputConnection
04-09 12:17:47.782: W/IInputConnectionWrapper(5752): clearMetaKeyStates on inactive InputConnection
04-09 12:17:49.704: D/AndroidRuntime(5752): Shutting down VM
04-09 12:17:49.704: W/dalvikvm(5752): threadid=1: thread exiting with uncaught exception (group=0x400207e0)
04-09 12:17:49.719: E/AndroidRuntime(5752): FATAL EXCEPTION: main
04-09 12:17:49.719: E/AndroidRuntime(5752): java.lang.ClassCastException: android.app.Application
04-09 12:17:49.719: E/AndroidRuntime(5752): at com.example.appslist.ApkListActivity.onItemClick(ApkListActivity.java:42)
04-09 12:17:49.719: E/AndroidRuntime(5752): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-09 12:17:49.719: E/AndroidRuntime(5752): at android.widget.ListView.performItemClick(ListView.java:3444)
04-09 12:17:49.719: E/AndroidRuntime(5752): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
04-09 12:17:49.719: E/AndroidRuntime(5752): at android.os.Handler.handleCallback(Handler.java:587)
04-09 12:17:49.719: E/AndroidRuntime(5752): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 12:17:49.719: E/AndroidRuntime(5752): at android.os.Looper.loop(Looper.java:143)
04-09 12:17:49.719: E/AndroidRuntime(5752): at android.app.ActivityThread.main(ActivityThread.java:4717)
04-09 12:17:49.719: E/AndroidRuntime(5752): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 12:17:49.719: E/AndroidRuntime(5752): at java.lang.reflect.Method.invoke(Method.java:521)
04-09 12:17:49.719: E/AndroidRuntime(5752): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-09 12:17:49.719: E/AndroidRuntime(5752): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-09 12:17:49.719: E/AndroidRuntime(5752): at dalvik.system.NativeStart.main(Native Method)
04-09 12:17:51.118: I/Process(5752): Sending signal. PID: 5752 SIG: 9
04-09 12:24:48.087: E/asset(5827): MAS: getAppPckgAndVerCode package: com.example.appslist === version 1
04-09 12:24:48.102: D/dalvikvm(5827): Trying to load lib lib_glossary.so 0x0
04-09 12:24:48.110: I/dalvikvm(5827): Unable to dlopen(lib_glossary.so): Cannot load library: load_library[1083]: Library 'lib_glossary.so' not found
04-09 12:24:48.110: E/MBGlossaryManager(5827): lib_glossary.so can't be loaded
04-09 12:24:48.212: E/asset(5827): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-09 12:24:48.219: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-09 12:24:48.219: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-09 12:24:48.219: E/asset(5827): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-09 12:24:48.227: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-09 12:24:48.235: E/asset(5827): MAS: getAppPckgAndVerCode package: com.example.backgroundapp === version 1
04-09 12:24:48.235: E/asset(5827): MAS: getAppPckgAndVerCode package: com.arcsoft.photoworkshop === version 5
04-09 12:24:48.298: D/dalvikvm(5827): GC_EXTERNAL_ALLOC freed 971 objects / 66752 bytes in 58ms
04-09 12:24:48.305: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.phone === version 8
04-09 12:24:48.313: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.calculator2 === version 8
04-09 12:24:48.313: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.htmlviewer === version 8
04-09 12:24:48.313: E/asset(5827): MAS: getAppPckgAndVerCode package: com.example.myfirstapp === version 1
04-09 12:24:48.321: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.providers.calendar === version 8
04-09 12:24:48.321: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.bluetooth === version 8
04-09 12:24:48.329: E/asset(5827): MAS: getAppPckgAndVerCode package: com.example.sessionmanagement === version 1
04-09 12:24:48.329: E/asset(5827): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-09 12:24:48.329: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-09 12:24:48.337: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-09 12:24:48.337: E/asset(5827): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-09 12:24:48.344: E/asset(5827): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-09 12:24:48.391: E/asset(5827): MAS: getAppPckgAndVerCode package: com.example.backgroundapp === version 1
04-09 12:24:49.805: D/AndroidRuntime(5827): Shutting down VM
04-09 12:24:49.805: W/dalvikvm(5827): threadid=1: thread exiting with uncaught exception (group=0x400207e0)
04-09 12:24:49.821: E/AndroidRuntime(5827): FATAL EXCEPTION: main
04-09 12:24:49.821: E/AndroidRuntime(5827): java.lang.ClassCastException: android.app.Application
04-09 12:24:49.821: E/AndroidRuntime(5827): at com.example.appslist.ApkListActivity.onItemClick(ApkListActivity.java:42)
04-09 12:24:49.821: E/AndroidRuntime(5827): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-09 12:24:49.821: E/AndroidRuntime(5827): at android.widget.ListView.performItemClick(ListView.java:3444)
04-09 12:24:49.821: E/AndroidRuntime(5827): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
04-09 12:24:49.821: E/AndroidRuntime(5827): at android.os.Handler.handleCallback(Handler.java:587)
04-09 12:24:49.821: E/AndroidRuntime(5827): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 12:24:49.821: E/AndroidRuntime(5827): at android.os.Looper.loop(Looper.java:143)
04-09 12:24:49.821: E/AndroidRuntime(5827): at android.app.ActivityThread.main(ActivityThread.java:4717)
04-09 12:24:49.821: E/AndroidRuntime(5827): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 12:24:49.821: E/AndroidRuntime(5827): at java.lang.reflect.Method.invoke(Method.java:521)
04-09 12:24:49.821: E/AndroidRuntime(5827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-09 12:24:49.821: E/AndroidRuntime(5827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-09 12:24:49.821: E/AndroidRuntime(5827): at dalvik.system.NativeStart.main(Native Method)
04-09 12:24:51.688: I/Process(5827): Sending signal. PID: 5827 SIG: 9
04-09 12:24:55.165: E/asset(5835): MAS: getAppPckgAndVerCode package: com.example.appslist === version 1
04-09 12:24:55.188: D/dalvikvm(5835): Trying to load lib lib_glossary.so 0x0
04-09 12:24:55.188: I/dalvikvm(5835): Unable to dlopen(lib_glossary.so): Cannot load library: load_library[1083]: Library 'lib_glossary.so' not found
04-09 12:24:55.188: E/MBGlossaryManager(5835): lib_glossary.so can't be loaded
04-09 12:24:55.313: E/asset(5835): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-09 12:24:55.321: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-09 12:24:55.329: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-09 12:24:55.329: E/asset(5835): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-09 12:24:55.337: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-09 12:24:55.337: E/asset(5835): MAS: getAppPckgAndVerCode package: com.example.backgroundapp === version 1
04-09 12:24:55.344: E/asset(5835): MAS: getAppPckgAndVerCode package: com.arcsoft.photoworkshop === version 5
04-09 12:24:55.391: D/dalvikvm(5835): GC_EXTERNAL_ALLOC freed 993 objects / 67608 bytes in 49ms
04-09 12:24:55.407: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.phone === version 8
04-09 12:24:55.415: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.calculator2 === version 8
04-09 12:24:55.415: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.htmlviewer === version 8
04-09 12:24:55.415: E/asset(5835): MAS: getAppPckgAndVerCode package: com.example.myfirstapp === version 1
04-09 12:24:55.423: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.providers.calendar === version 8
04-09 12:24:55.423: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.bluetooth === version 8
04-09 12:24:55.423: E/asset(5835): MAS: getAppPckgAndVerCode package: com.example.sessionmanagement === version 1
04-09 12:24:55.430: E/asset(5835): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-09 12:24:55.430: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-09 12:24:55.438: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-09 12:24:55.438: E/asset(5835): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-09 12:24:55.446: E/asset(5835): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-09 12:24:55.493: E/asset(5835): MAS: getAppPckgAndVerCode package: com.example.backgroundapp === version 1
04-09 12:24:56.641: D/AndroidRuntime(5835): Shutting down VM
04-09 12:24:56.641: W/dalvikvm(5835): threadid=1: thread exiting with uncaught exception (group=0x400207e0)
04-09 12:24:56.657: E/AndroidRuntime(5835): FATAL EXCEPTION: main
04-09 12:24:56.657: E/AndroidRuntime(5835): java.lang.ClassCastException: android.app.Application
04-09 12:24:56.657: E/AndroidRuntime(5835): at com.example.appslist.ApkListActivity.onItemClick(ApkListActivity.java:42)
04-09 12:24:56.657: E/AndroidRuntime(5835): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-09 12:24:56.657: E/AndroidRuntime(5835): at android.widget.ListView.performItemClick(ListView.java:3444)
04-09 12:24:56.657: E/AndroidRuntime(5835): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
04-09 12:24:56.657: E/AndroidRuntime(5835): at android.os.Handler.handleCallback(Handler.java:587)
04-09 12:24:56.657: E/AndroidRuntime(5835): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 12:24:56.657: E/AndroidRuntime(5835): at android.os.Looper.loop(Looper.java:143)
04-09 12:24:56.657: E/AndroidRuntime(5835): at android.app.ActivityThread.main(ActivityThread.java:4717)
04-09 12:24:56.657: E/AndroidRuntime(5835): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 12:24:56.657: E/AndroidRuntime(5835): at java.lang.reflect.Method.invoke(Method.java:521)
04-09 12:24:56.657: E/AndroidRuntime(5835): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-09 12:24:56.657: E/AndroidRuntime(5835): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-09 12:24:56.657: E/AndroidRuntime(5835): at dalvik.system.NativeStart.main(Native Method)
04-09 12:24:58.040: I/Process(5835): Sending signal. PID: 5835 SIG: 9
04-09 12:39:54.556: I/Process(5930): Sending signal. PID: 5930 SIG: 9
04-09 12:43:51.244: E/asset(5972): MAS: getAppPckgAndVerCode package: com.example.appslist === version 1
04-09 12:43:51.283: D/dalvikvm(5972): Trying to load lib lib_glossary.so 0x0
04-09 12:43:51.291: I/dalvikvm(5972): Unable to dlopen(lib_glossary.so): Cannot load library: load_library[1083]: Library 'lib_glossary.so' not found
04-09 12:43:51.291: E/MBGlossaryManager(5972): lib_glossary.so can't be loaded
04-09 12:43:51.494: E/asset(5972): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-09 12:43:51.494: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-09 12:43:51.502: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-09 12:43:51.509: E/asset(5972): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-09 12:43:51.509: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-09 12:43:51.517: E/asset(5972): MAS: getAppPckgAndVerCode package: com.example.backgroundapp === version 1
04-09 12:43:51.517: E/asset(5972): MAS: getAppPckgAndVerCode package: com.arcsoft.photoworkshop === version 5
04-09 12:43:51.580: D/dalvikvm(5972): GC_EXTERNAL_ALLOC freed 1059 objects / 70176 bytes in 57ms
04-09 12:43:51.588: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.phone === version 8
04-09 12:43:51.595: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.calculator2 === version 8
04-09 12:43:51.595: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.htmlviewer === version 8
04-09 12:43:51.595: E/asset(5972): MAS: getAppPckgAndVerCode package: com.example.myfirstapp === version 1
04-09 12:43:51.603: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.providers.calendar === version 8
04-09 12:43:51.603: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.bluetooth === version 8
04-09 12:43:51.611: E/asset(5972): MAS: getAppPckgAndVerCode package: com.example.sessionmanagement === version 1
04-09 12:43:51.611: E/asset(5972): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-09 12:43:51.619: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-09 12:43:51.619: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-09 12:43:51.619: E/asset(5972): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-09 12:43:51.627: E/asset(5972): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-09 12:43:51.673: E/asset(5972): MAS: getAppPckgAndVerCode package: com.example.backgroundapp === version 1
04-09 12:43:56.166: D/AndroidRuntime(5972): Shutting down VM
04-09 12:43:56.166: W/dalvikvm(5972): threadid=1: thread exiting with uncaught exception (group=0x400207e0)
04-09 12:43:56.189: E/AndroidRuntime(5972): FATAL EXCEPTION: main
04-09 12:43:56.189: E/AndroidRuntime(5972): java.lang.ClassCastException: android.app.Application
04-09 12:43:56.189: E/AndroidRuntime(5972): at com.example.appslist.ApkListActivity.onItemClick(ApkListActivity.java:43)
04-09 12:43:56.189: E/AndroidRuntime(5972): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-09 12:43:56.189: E/AndroidRuntime(5972): at android.widget.ListView.performItemClick(ListView.java:3444)
04-09 12:43:56.189: E/AndroidRuntime(5972): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
04-09 12:43:56.189: E/AndroidRuntime(5972): at android.os.Handler.handleCallback(Handler.java:587)
04-09 12:43:56.189: E/AndroidRuntime(5972): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 12:43:56.189: E/AndroidRuntime(5972): at android.os.Looper.loop(Looper.java:143)
04-09 12:43:56.189: E/AndroidRuntime(5972): at android.app.ActivityThread.main(ActivityThread.java:4717)
04-09 12:43:56.189: E/AndroidRuntime(5972): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 12:43:56.189: E/AndroidRuntime(5972): at java.lang.reflect.Method.invoke(Method.java:521)
04-09 12:43:56.189: E/AndroidRuntime(5972): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-09 12:43:56.189: E/AndroidRuntime(5972): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-09 12:43:56.189: E/AndroidRuntime(5972): at dalvik.system.NativeStart.main(Native Method)
04-09 12:43:57.486: I/Process(5972): Sending signal. PID: 5972 SIG: 9
答案 0 :(得分:2)
如果您仔细阅读错误日志,您将看到问题所在。它在线
AppData appData = (AppData) getApplication();
您的onItemClick
方法的。 getApplication
方法返回com.android.Application
,您尝试将其转换为AppData
类,但它不是AppData
的实例。
虽然AppData
的每个实例也是Application
的实例,但反之亦然:Application
的实例不会自动成为其子类的实例。
答案 1 :(得分:1)
如果你扩展应用程序类,你应该修改你的manifest [add android:name]。在您的情况下,您应该添加到清单:
<application
android:name=".app.AppData"