2015-07-19 41 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。希望这有帮助 –