2013-11-14 42 views
0

我已經創建了此代碼來顯示手機文件夾中的文件。現在,我的意圖是,點擊文件夾中的其中一個文件(apk文件),安裝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

在super.onCreate()之後的onCreate方法中使用setContentView(),這應該可以解決問題。

+0

我還沒有這個ListActivity的佈局 – user2976665

相關問題