- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试制作一个提供已安装应用程序列表的应用程序,当单击某个项目时,将启动另一个 Activity ,该 Activity 说明已安装应用程序所需的权限。我得到了已安装的应用程序列表,但是当我单击任何应用程序而不是启动新 Activity 时,应用程序强制关闭。
主要 Activity
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;
}
}
list 文件
<?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>
最佳答案
编辑:
It seems you have not registered AppData in the Manifest
尝试添加
android:name=".AppData"
在 list 的应用程序标签中。
即这个
<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();
关于android - 已安装应用列表强制关闭 : java. lang.ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15757590/
在 Tomcat 6/Ubuntu 12.04 上启动 Grails 2.1.0 应用程序时出现以下错误。 Error 500 - Internal Server Error. groovy.lang
在运行 Storm 拓扑时,我收到此错误。拓扑完美运行 5 分钟,没有任何错误,然后失败。我正在使用 Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS as 300 sec i
我有一个 jsp 代码在其中一台机器上运行良好。但是当我复制到另一台机器时,我得到了这个 no such method found 异常。我是 Spring 的新手。有人可以解释我错过了什么吗? 以下
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我的代码在下面给出了一个错误; Exception in thread "main" java.lang.NoSuchMethodError: com/myApp/Client.cypherCBC(L
我正在尝试一个 Restful web 服务示例,所以当我要访问 url 时,我遇到了异常 java.lang.NoSuchMethodError: jersey.repackaged.com.goo
我正在将一个 Spring web 项目转换为一个 Maven 项目,但我收到了这个错误: java.lang.NoSuchMethodError: org.jboss.logging.Logger.
在我的项目中,我有一个像这样的枚举: public enum MyEnum { FIRST(1), SECOND(2); private int value; private MyEnum(int v
我创建了这个简单的示例,用于读取 Linux 正常运行时间: public String getMachineUptime() throws IOException { String[] di
我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,
我正在运行以下查询。它显示一条错误消息。如何解决这个错误? ListrouteList=null; List companyList = session.createS
我有以下模型类: @Entity @Table(name="user_content") @org.hibernate.annotations.NamedQueries({ @org.
我有那个错误。这是我的代码: GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME
实际上我在执行我的java程序时遇到了下面提到的错误 Exception in thread "pool-1-thread-1" java.lang.ClassCastException: jav
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String 我在以下代码中遇到此异常: Strin
我正在尝试从 linkedhashset 中检索随机元素。下面是我的代码,但它每次都给我异常。 private static void generateRandomUserId(Set userIds
我已经完成了 Android 中的代码: List spinnerArray = new ArrayList(); for (int i = 0; i item = (LinkedTreeMap)
这个问题已经有答案了: Explanation of ClassCastException in Java (12 个回答) 已关闭 6 年前。 我已经编写了 java 到 Json 的代码,同时从页
这个问题在这里已经有了答案: ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn (4 个答案) 关闭 6 年前
我在运行时遇到问题来编译这段代码,这给我一个错误,java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮助我更正此代码,我将非常高兴 double x; pu
我是一名优秀的程序员,十分优秀!