我创建了此代码以显示手机文件夹中的文件。现在,我的意图是点击其中一个文件(apk文件)在文件夹中安装了apk选中。这是代码
private List<String> fileList = new ArrayList<String>();
ApplicationInfo ai;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File root = new File(Environment.getExternalStorageDirectory().toString()+File.separator+"MyFolder");
ListDir(root);
}
@SuppressWarnings("deprecation")
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
String appPos = fileList.get(position);
Uri packageUri = Uri.parse("package:"+getApplicationContext().getPackageName());
Intent installIntent = new Intent(Intent.ACTION_VIEW, packageUri);
try {
startActivity(installIntent);
}
catch (ActivityNotFoundException ex) {
final AlertDialog alertDialog1 = new AlertDialog.Builder(RestoreApp.this).create();
alertDialog1.setTitle("Error");
alertDialog1.setMessage("Unable open the selected activity");
alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog1.dismiss();
}
});
alertDialog1.show();
}
}
void ListDir(File f){
File[] files = f.listFiles();
fileList.clear();
for (File file : files){
fileList.add(file.getName());
}
ArrayAdapter<String> directoryList
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}
}
当我点击一个项目时显示对话框,这意味着进入ActivityNotFoundException。为什么?怎么解决?
答案 0 :(得分:0)
在super.onCreate()之后的onCreate方法中使用setContentView(),这应解决问题。