2017-05-04 89 views
0

我不知道爲什麼,但我不斷收到此錯誤,另一個問題是,在AsyncTask之後listView不可見,我必須再次手動刷新到notifyDataSetChanged 。請幫忙!提前致謝! MainActivity.java編舞:跳過53幀!該應用程序可能會在其主線程上做太多的工作

public class MainActivity extends AppCompatActivityimplements NavigationView.OnNavigationItemSelectedListener { 

ListView listView; 
CustomAdapter customAdapter; 
public static Boolean tapped = false; 

SwipeRefreshLayout refreshLayout; 

public static ArrayList<String> titles; 
public static ArrayList<String> newsSources; 
public static ArrayList<String> imageUrl; 
public static ArrayList<String> urls; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

    //Main 

    ListView listView = (ListView) findViewById(R.id.listView); 

    refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh); 

    refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 
      customAdapter.notifyDataSetChanged(); 
      refreshLayout.setRefreshing(false); 
     } 
    }); 

    titles = new ArrayList<>(); 
    urls = new ArrayList<>(); 
    newsSources = new ArrayList<>(); 
    imageUrl = new ArrayList<>(); 

    customAdapter = new CustomAdapter(MainActivity.this, titles, newsSources, imageUrl); 

    DownloadTask task = new DownloadTask(); 
    try { 

     task.execute("https://newsapi.org/v1/articles?source=the-verge&sortBy=top&apiKey=357539b5b6dd401689aa2f400ce1a03d"); 
     listView.setAdapter(customAdapter); 
     customAdapter.notifyDataSetChanged(); 


    } catch (Exception e) { 

     e.printStackTrace(); 

    } 


} 

public class DownloadTask extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... strings) { 

     String result = ""; 
     URL url; 
     HttpsURLConnection urlConnection = null; 

     try { 

      url = new URL(strings[0]); 
      urlConnection = (HttpsURLConnection) url.openConnection(); 

      InputStream in = urlConnection.getInputStream(); 
      InputStreamReader reader = new InputStreamReader(in); 

      int data = reader.read(); 

      while (data != -1) { 

       char current = (char) data; 
       result += current; 
       data = reader.read(); 

      } 

      Log.i("URLContent", result); 

      JSONObject jsonObject = new JSONObject(result); 
      JSONArray jsonArray = jsonObject.getJSONArray("articles"); 

      for (int i = 0; i < jsonArray.length(); i++) { 

       JSONObject jsonObject1 = jsonArray.getJSONObject(i); 

       String articleTitle = jsonObject1.getString("title"); 
       String articleURL = jsonObject1.getString("url"); 
       String articleThumbnail = jsonObject1.getString("urlToImage"); 
       String articleSource = jsonObject1.getString("author"); 

       Log.i("Content", articleTitle); 

       titles.add(articleTitle); 
       imageUrl.add(articleThumbnail); 
       newsSources.add(articleSource); 
       urls.add(articleURL); 

      } 


     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 


} 



@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_camera) { 
     // Handle the camera action 
    } else if (id == R.id.nav_gallery) { 

    } else if (id == R.id.nav_slideshow) { 

    } else if (id == R.id.nav_manage) { 

    } else if (id == R.id.nav_share) { 

    } else if (id == R.id.nav_send) { 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

} `

+1

''customAdapter'在你調用'setAdapter()'的時候不會有任何東西,因爲'newsSources'正在後臺線程中填充。正確使用'AsyncTask';在'onPostExecute()'中填充你的'ListView'。或者,不是使用'AsyncTask'和所有這些手動HTTP I/O和JSON解析,而是使用現有的庫,例如Retrofit。 – CommonsWare

回答

1

不要叫你的線程在主線程。當活動完成UI的工作時也調用線程。像onStart()中的地方AsyncTasks一樣,並使用onStop()方法銷燬它們。

+0

非常感謝您的幫助! –

相關問題