Android跳过了44帧!应用程序可能在其主线程上做了太多的工作。

huangapple go评论88阅读模式
英文:

android Skipped 44 frames! The application may be doing too much work on its main thread

问题

我正在使用Java和SQLite开发一款图书应用,我想在列表视图中显示内容列表。我已经解决了一些打开数据库的错误,但现在数据库已经打开,但是列表视图中没有显示任何内容,我收到了以下错误。

以下是我的活动代码。此活动在主活动中点击按钮后启动:

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class LVSwitches extends AppCompatActivity {
    public static final String LOGTAG = "book";
    private BookDataSource dataSource;
    private List<season> seasons;
    private ListView listView;
    private ArrayAdapter<season> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_l_v_switches);
        listView = (ListView) findViewById(R.id.season_list);
        dataSource = new BookDataSource(getBaseContext());
        this.seasons = this.dataSource.getAllSeasons();
        
        if (seasons.size() == 0) {
            this.dataSource.CopyDbfile();
            this.seasons = this.dataSource.getAllSeasons();
        }
    }
}

以下是数据源的代码:

public class BookDataSource {
    // ... 其他部分未翻译 ...
    
    public List<season> getAllSeasons() {
        List<season> items = new ArrayList<season>();
        openDb();
        String[] columns = {bookDBHelper.KEY_ID, bookDBHelper.KEY_CONTENT, bookDBHelper.KEY_ISFAV};
        Cursor cursor = database.query(bookDBHelper.TABLE_LVS, columns, bookDBHelper.KEY_TYPE + " = \"" + bookDBHelper.TYPE_INDX + "\"",
                null, null, null, bookDBHelper.KEY_ID);
        if (cursor.moveToFirst()) {
            do {
                season season = new season();
                season.setId(cursor.getLong(cursor.getColumnIndex(bookDBHelper.KEY_ID)));
                season.setStitle(cursor.getString(cursor.getColumnIndex(bookDBHelper.KEY_CONTENT)));
                season.setFav(cursor.getInt(cursor.getColumnIndex(bookDBHelper.KEY_ISFAV)) > 0);
                items.add(season);
            } while (cursor.moveToNext());
        }
        return items;
    }
    
    // ... 其他部分未翻译 ...
}

season 类:

public class season {
    // ... 其他部分未翻译 ...
}

请注意,因为你只需要翻译特定的代码部分,所以我已经排除了注释和其他无关的内容。

英文:

I am developing a book app using Java and SQLite and I want to show the list of the contents in a list view. I have already solved a few errors for opening the database but now the database opens but nothing is showing in the list view and I receive that error.

The code for my activity. This activity starts after clicking on a button in the main activity:

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class LVSwitches extends AppCompatActivity {
public static final String LOGTAG=&quot;book&quot;;
private BookDataSource dataSource;
private List&lt;season&gt; seasons;
private ListView listView;
private ArrayAdapter&lt;season&gt; adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_l_v_switches);
    listView = (ListView) findViewById(R.id.season_list);
    dataSource=new BookDataSource(getBaseContext());
    this.seasons = this.dataSource.getAllSeasons();
    //dataSource.getAllSeasons();
    if (seasons.size()==0){
        this.dataSource.CopyDbfile();

        this.seasons = this.dataSource.getAllSeasons();
    }

}

}

Here is the code for the data source

public class BookDataSource {
private Context context;
private SQLiteDatabase database;
private SQLiteOpenHelper dbhelper;
private static String DB_PATH = &quot;&quot;;
private static String DB_NAME = &quot;lvs&quot;;

public BookDataSource(Context context){
    this.context=context;
    this.dbhelper=new bookDBHelper(context);
    DB_PATH = &quot;/data/data/&quot; + context.getPackageName() + &quot;/databases/&quot;;
}
public void openDb(){
    database=dbhelper.getWritableDatabase();
    String myPath = DB_PATH + DB_NAME;
    database = SQLiteDatabase.openDatabase
            (myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE);
    Log.i(LVSwitches.LOGTAG,&quot;database opened&quot;);
}
public void closeDb(){
    if (database!=null){
        database.close();
        //Log.i(LVSwitches.LOGTAG,&quot;database closed&quot;);
    }else {
       Log.i(LVSwitches.LOGTAG,&quot;db=null&quot;);
    }
}
public List&lt;season&gt; getAllSeasons(){
    List&lt;season&gt; items=new ArrayList&lt;season&gt;();
    openDb();
    String[] colums= {bookDBHelper.KEY_ID,bookDBHelper.KEY_CONTENT,bookDBHelper.KEY_ISFAV};
    Cursor cursor=database.query(bookDBHelper.TABLE_LVS,colums,bookDBHelper.KEY_TYPE + &quot; = \&quot;&quot; +bookDBHelper.TYPE_INDX + &quot;\&quot;&quot;,
            null,null,null,bookDBHelper.KEY_ID);
    if (cursor.moveToFirst()){
        do {
            season season = new season();
            season.setId(cursor.getLong(cursor.getColumnIndex(bookDBHelper.KEY_ID)));
            season.setStitle(cursor.getString(cursor.getColumnIndex(bookDBHelper.KEY_CONTENT)));
            season.setFav(cursor.getInt(cursor.getColumnIndex(bookDBHelper.KEY_ISFAV)) &gt; 0);
            items.add(season);
        }while (cursor.moveToNext());

        }
    return items;
    }
    public void CopyDbfile()
    {
        try {
            InputStream in=this.context.getAssets().open(&quot;lvs.db&quot;);
            File dir=new File(&quot;/data/data/&quot;+this.context.getPackageName()+&quot;/databases&quot;);
            dir.mkdirs();
            FileOutputStream out=new FileOutputStream(new File(dir,&quot;lvs.db&quot;));
            byte[] buffer=new byte[1024];
            int len=0;
            while ((len=in.read(buffer))&gt;0) {
                out.write(buffer, 0, len);

            }
            in.close();
            out.close();
            Log.i(LVSwitches.LOGTAG,&quot;database copied&quot;);

        }
        catch (Exception e){
            Log.i(&quot;Story&quot;, &quot;copy() : exception -&gt; &quot; + e.getMessage());
        }
    }



}

season class:

    public class season {

    private String stitle=&quot;&quot;;
    private String pic=&quot;&quot;;
    private long id=0;
    private boolean isFav=false;
    public season(String stitle){
        this.id= id;
        this.stitle= stitle;
        this.isFav= isFav;
    }

    public String getStitle() {
        return stitle;
    }

    public void setStitle(String stitle) {
        this.stitle = stitle;
    }

    public season()
    {
        String stitle=&quot;&quot;;
        String pic=&quot;&quot;;
        long id=0;
        boolean isFav=false;
    }


    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public boolean isFav() {
        return isFav;
    }

    public void setFav(boolean fav) {
        isFav = fav;
    }
    public String toString(){
        return stitle;

    }
 }

答案1

得分: 0

你需要告诉ListView使用你的数据源。你可以通过以下方式告诉它:

listView.setAdapter(YOUR_ADAPTER);

只需查阅Android文档。编写适配器有详细的说明。

此外,你提到了应用程序在主线程上执行了过多的工作而引发的错误。这与你的ListView问题无关。这更像是一个警告,提示你在另一个线程上执行繁重的任务。主线程用于渲染你的应用程序。因此,如果你在主线程上做太多的工作,你的UI将被阻塞。我建议你在另一个线程上处理数据库操作。此外,你应该记住,只有主线程才能对UI进行更改,因此当你想在UI线程上处理并行线程计算结果时,你可能会发现你的activity对象中的runOnUiThread()方法很有帮助。

英文:

You need to tell the ListView to use your data source. You can tell it to do so by writing

listView.setAdapter(YOUR_ADAPTER);

Just have a look at the Android documentation. Writing an adapter is well explained.

Furthermore, you mentioned the error that the application does too much work on its main thread. It does not have something to do with your ListView issue. It's much more a warning telling you to do heavy tasks on another Thread. The main thread is used to render your application. So if you do too much work on it your UI is blocked. I'd recommend you to do the database stuff on another thread. Moreover you should keep in mind that only the main thread can apply changes to the UI, so you might find the runOnUiThread() method of your activity object helpful when you want to do something with the result of your parallel thread's calculation on the UI thread.

huangapple
  • 本文由 发表于 2020年8月16日 02:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63429687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定