2011-04-28 44 views
0

我想下載完成後更新我的列表視圖,但它給了我空指針異常我的代碼是按照下空指針異常刷新

 
try { 
        URL url = new URL(GlobalVariable.Getstr()); 
        HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
        c.setRequestMethod("GET"); 
        c.setDoOutput(true); 
        c.connect(); 
         int lenghtOfFile = c.getContentLength(); 
         Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 
        String PATH = Environment.getExternalStorageDirectory() 
          + "/download/"; 
        Log.v(LOG_TAG, "PATH: " + PATH); 
        File file = new File(PATH); 
        file.mkdirs(); 

        // String fileName = "workTest.mp3"; 
        String fileName = GlobalVariable.Getstrpath().toString(); 

        File outputFile = new File(file, fileName); 
        FileOutputStream fos = new FileOutputStream(outputFile); 

        InputStream is = c.getInputStream(); 

        byte[] buffer = new byte[1024]; 
//     int len1 = 0; 
        long total = 0; 
//     while ((len1 = is.read(buffer)) != -1) { 
//      total += len1; 
//      publishProgress(""+(int)((total*100)/lenghtOfFile)); 
//      fos.write(buffer, 0, len1); 
//     
        while ((count = is.read(buffer)) != -1) { 
          total += count; 
          publishProgress(""+(int)((total*100)/lenghtOfFile)); 
          fos.write(buffer, 0, len1); 

        } 
        fos.close(); 
        is.close(); 
//     ArrayAdapter aa=new ArrayAdapter(FindFilesByType.this, android.R.layout.test_list_item,Ringtones); 
//     aa.setNotifyOnChange(true); 
//     lv1.setAdapter(aa); 
        try{ 

         ((ArrayAdapter) lv1.getAdapter()).notifyDataSetChanged(); 

       //  adapter.notifyDataSetChanged(); 




        } 
        catch (Exception e) { 
         Log.i(LOG_TAG, "Error: " + e.toString()); 
        } 



我收到

04-28 15:56:17.198: INFO/app(624): Error: java.lang.NullPointerException

我在做什麼錯?

在此先感謝

+0

這是創建方法ArrayAdapter 適配器=新ArrayAdapter (FindFilesByType.this,android.R.layout.test_list_item我的陣列適配器,鈴聲); \t \t \t \t this.lv1.setAdapter(adapter); – Android 2011-04-28 10:31:37

回答

1

在哪一行異常來了。

and use notifyDataSetChanged();方法適配器而不是lvl.getAdapter()。 notifyDataSetChanged();

編輯:notifydatasetChange看到這個

希望它可以幫助..

+0

你試過aa.notifyDataSetChanged();直。 – Udaykiran 2011-04-28 10:42:48

+0

lvl.setAdapter(aa)它顯示列表..?並且數據添加不爲空 – Udaykiran 2011-04-28 10:44:40

+0

檢查上面編輯的鏈接... – Udaykiran 2011-04-28 10:51:51