java - Choreographer : Skipped 53 frames! 应用程序可能在其主线程上做了太多工作

标签 java android performance listview

我不知道为什么,但我不断收到此错误,另一个问题是在AsyncTask之后listView不可见,我必须再次手动刷新以notifyDataSetChanged。请帮忙!提前致谢! MainActivity.java

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;
}

} `

最佳答案

不要在主线程中调用您的线程。当 Activity 完成对 UI 的处理时,也会调用线程。就像将 AsyncTasks 放在 onStart() 中并在 onStop() 方法中销毁它们。

关于java - Choreographer : Skipped 53 frames! 应用程序可能在其主线程上做了太多工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43791986/

相关文章:

java - 为什么 Java 没有真正的多维数组?

java - 字符数组与字符串 : which is better for storing a set of letters

ruby - 找到给定数字的因子的最快方法

java - 为什么一个线程的执行速度比多个线程快得多?

java - 静态变量设置为 null 可用于 GC?

android - 如何使用 CSS 媒体查询检测 Android 设备的默认字体大小?

java - 从android发送序列化数据到java

java - Checkbox-未选中当前位置

java - 如何通过 pom.xml 告诉编译器使用 openjdk 11

java - 无法在intellij中将Spring mvc部署到tomcat