2015-07-19 40 views
0

我想從我動態添加Strings的列表中填充我的android應用(android studio)中的listviewAndroid從popl列表中填充listview錯誤

首先,名單被宣佈作爲活動的一個屬性:

private List<String> your_array_list = new ArrayList<String>(); 

以後,我就在那裏該方法populatelist()被稱爲類。還有其他的方法調用它,但他們都做工精細,populatelist()是給我一個錯誤的唯一方法:

private class SendMessage extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 
      try {    

        parse(message); 
        populatelist(); 
       } 



      } catch (UnknownHostException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

    } 

這是方法本身:

public void populatelist() { 
    // String[] myitems = {"World Weather Online","Open Weather Map","Weather"}; 
     Log.d("Matcher", "Populate! Test 1"); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.items,R.id.textting,your_array_list); 
     Log.d("Matcher", "Populate! Test 2"); 
     ListView list; 
     Log.d("Matcher", "Populate! Test 3"); 
     list = (ListView) findViewById(R.id.services); 
     Log.d("Matcher", "Populate! Test 4"); 
     list.setAdapter(adapter); 
     Log.d("Matcher", "Populate! Test 5"); 

    } 

所有日誌打印除了最後一個「測試5」。當我運行我的應用程序時,出現以下錯誤:

07-19 21:13:32.399 1575-1591/com.example.othmane.servicefinder E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2 
    Process: com.example.othmane.servicefinder, PID: 1575 
    java.lang.RuntimeException: An error occured while executing doInBackground() 
      at android.os.AsyncTask$3.done(AsyncTask.java:300) 
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
      at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
      at java.lang.Thread.run(Thread.java:818) 
    Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
      at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6247) 
      at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2855) 
      at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:679) 
      at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:679) 
      at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:679) 
      at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:679) 
      at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:679) 
      at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:679) 
      at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:679) 
      at android.view.View.setFlags(View.java:9603) 
      at android.view.View.setFocusableInTouchMode(View.java:6741) 
      at android.widget.AdapterView.checkFocus(AdapterView.java:722) 
      at android.widget.ListView.setAdapter(ListView.java:488) 
      at com.example.othmane.servicefinder.MainActivity.populatelist(MainActivity.java:277) 
      at com.example.othmane.servicefinder.MainActivity$SendMessage.doInBackground(MainActivity.java:100) 
      at com.example.othmane.servicefinder.MainActivity$SendMessage.doInBackground(MainActivity.java:67) 
      at android.os.AsyncTask$2.call(AsyncTask.java:288) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
            at java.lang.Thread.run(Thread.java:818) 

有沒有人看到我如何嘗試填充列表的錯誤?我跟着一個教程去做,但我不知道爲什麼它不起作用。

回答

0

閱讀logcat的,你會看到答案:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at 

錯誤是由您要填充查看後臺線程。爲了在主線程中執行工作,您需要調用onPostExecute方法。

這裏是我的應用程序的一個例子:

public class FetchArtistData extends AsyncTask<String,Void, List<Artist>> { 
    private static final String TAG="FetchArtistData"; 
    @Override 
    protected List<Artist> doInBackground(String... params) { 
     SpotifyApi api=new SpotifyApi(); 
     SpotifyService spotify=api.getService(); 
     ArtistsPager results=spotify.searchArtists(params[0]); 
     List<Artist> list=results.artists.items; 
     Log.d(TAG, list.toString()); 
     return list; 
    } 

    @Override 
    protected void onPostExecute(List<Artist> artists) { 
     mArtistList=(ArrayList<Artist>)artists; 
     Log.d(TAG,Integer.toString(mArtistList.size())); 
     updateAdapter(mArtistList); 
    } 
} 

注意到我更新onPostExecute視圖。

+0

謝謝!我沒有錯誤了,但列表仍然是空的 – user2446260

+0

你也可以發佈your_array_list和解析(消息)的代碼。在我看來,你可能已經錯誤地實現了它們,結果your_array_list是空的。通常,我會調用adapter.notifyDataSetChanged或在onPostExecute方法中編寫自己的updateAdapter(ArrayList)以使用新數據重新填充listview。 –

+0

讓我們嘗試創建ArrayAdapter onCreateView,更新your_array_list以反映新的數據集並在onPostExecute上調用adapter.notifyDataSetChanged。希望這有幫助 –