Java 编舞:跳过了53帧!应用程序可能在其主线程上做了太多工作
我不知道为什么,但我经常遇到这个错误,另一个问题是,在Java 编舞:跳过了53帧!应用程序可能在其主线程上做了太多工作,java,android,performance,listview,Java,Android,Performance,Listview,我不知道为什么,但我经常遇到这个错误,另一个问题是,在AsyncTask之后,listView不可见,我必须再次手动刷新到notifyDataSetChanged。请帮忙!提前谢谢! MainActivity.java public class MainActivity extends AppCompatActivityimplements NavigationView.OnNavigationItemSelectedListener { ListView listView; CustomA
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;
}
public class MainActivity扩展了AppCompativeActivityImplements NavigationView.OnNavigationItemSelectedListener{
列表视图列表视图;
自定义适配器自定义适配器;
公共静态布尔值=false;
SwipeRefreshLayout刷新布局;
公共静态数组列表标题;
公共静态ArrayList新闻源;
公共静态ArrayList imageUrl;
公共静态ArrayList URL;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
ActionBarDrawerToggle切换=新建ActionBarDrawerToggle(
这,抽屉,工具栏,R.string.navigation\u drawer\u open,R.string.navigation\u drawer\u close);
抽屉。设置抽屉定位器(开关);
toggle.syncState();
NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图);
navigationView.setNavigationItemSelectedListener(此);
//主要
ListView ListView=(ListView)findViewById(R.id.ListView);
refreshLayout=(SwipeRefreshLayout)findViewById(R.id.refresh);
refreshLayout.setOnRefreshListener(新的SwipeRefreshLayout.OnRefreshListener(){
@凌驾
公共void onRefresh(){
customAdapter.notifyDataSetChanged();
refreshLayout.setRefreshing(false);
}
});
titles=新的ArrayList();
URL=新的ArrayList();
newsSources=newArrayList();
imageUrl=新的ArrayList();
customAdapter=新的customAdapter(MainActivity.this、标题、新闻源、imageUrl);
DownloadTask任务=新建DownloadTask();
试一试{
任务。执行(“https://newsapi.org/v1/articles?source=the-verge&sortBy=top&apiKey=357539b5b6dd401689aa2f400ce1a03d”);
setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();
}捕获(例外e){
e、 printStackTrace();
}
}
公共类DownloadTask扩展了AsyncTask{
@凌驾
受保护的字符串背景(字符串…字符串){
字符串结果=”;
网址;
HttpsURLConnection urlConnection=null;
试一试{
url=新url(字符串[0]);
urlConnection=(HttpsURLConnection)url.openConnection();
InputStream in=urlConnection.getInputStream();
InputStreamReader reader=新的InputStreamReader(in);
int data=reader.read();
while(数据!=-1){
当前字符=(字符)数据;
结果+=电流;
data=reader.read();
}
Log.i(“URLContent”,结果);
JSONObject JSONObject=新JSONObject(结果);
JSONArray JSONArray=jsonObject.getJSONArray(“文章”);
for(int i=0;i
}
`不要在主线程中调用线程。当活动完成对UI的操作时也调用线程。类似于将异步任务放置在
onStart()
中,并在onStop()
方法中销毁它们。customAdapter
在调用setAdapter()
时不会包含任何内容,因为新闻源