我在listview的onitemclick方法中遇到ClassCast异常。在项目点击它应该转到下一个活动,但在项目点击应用程序强制关闭,它给出了类强制转换异常:
AppData appData =(AppData)getApplicationContext();
这是代码
使用活动:
public class UsageActivity extends Activity
implements OnItemClickListener {
PackageManager packageManager;
ListView apkList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_usage);
packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
apkList = (ListView) findViewById(R.id.applist);
apkList.setAdapter(new ApkAdapter(this, packageList, packageManager));
apkList.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long row) {
// TODO Auto-generated method stub
PackageInfo packageInfo = (PackageInfo) parent
.getItemAtPosition(position);
AppData appData = (AppData) getApplicationContext();
appData.setPackageInfo(packageInfo);
Intent appInfo = new Intent(getApplicationContext(), ApkInfo.class);
startActivity(appInfo);
}
应用程序数据:
package com.example.ui_work;
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;
}
}
请帮忙。