Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 即使在使用AsyncTask之后,编舞也会跳过帧(最多400!)_Java_Android_Android Asynctask - Fatal编程技术网

Java 即使在使用AsyncTask之后,编舞也会跳过帧(最多400!)

Java 即使在使用AsyncTask之后,编舞也会跳过帧(最多400!),java,android,android-asynctask,Java,Android,Android Asynctask,我正在尝试从远程服务器获取数据,尝试在SQLite中创建数据库并更新我的共享首选项。我将前面提到的所有操作都放在一个AsyncTask中,从理论上讲,它必须在UI(主)线程以外的单独线程上运行。此外,我正在实际的Android设备上运行应用程序,而不是在模拟器上运行 我在日志中获得以下信息: I/Choreographer: Skipped 229 frames! The application may be doing too much work on its main thread. 源

我正在尝试从远程服务器获取数据,尝试在SQLite中创建数据库并更新我的共享首选项。我将前面提到的所有操作都放在一个AsyncTask中,从理论上讲,它必须在UI(主)线程以外的单独线程上运行。此外,我正在实际的Android设备上运行应用程序,而不是在模拟器上运行

我在日志中获得以下信息:

I/Choreographer: Skipped 229 frames!  The application may be doing too much work on its main thread.
源代码:

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

    TextView errorMessage;
    Button retryAgainButton;

    SharedPreferences sharedPrefs;

    @SuppressLint("StaticFieldLeak")
    public class FirstRunDBCreator extends AsyncTask<Void, Void, String> {
        String result = "";
        private Exception e = null;

        @Override
        protected void onPreExecute() {
            sharedPrefs = getApplicationContext().getSharedPreferences("android.content.SharedPreference", Context.MODE_PRIVATE);
            if(!sharedPrefs.contains("firstRun")) {
                sharedPrefs.edit().putBoolean("firstRun", true).apply();
            }
        }

        @Override
        protected String doInBackground(Void... voids) {
            String result = "";
            // All the heavy operations here
            // Fetching the data from the server here and creating/ storing data in my SQLite database
            // I have also used transactions and SQLite preparedStatement to increase the insert speed
            return result;
        }

        @SuppressLint("SetTextI18n")
        @Override
        protected void onPostExecute(String s) {
            // Closing the database connections and handling any exceptions here
            // Updating the UI elements such as diplaying the complete message or the visibility of a textview
        }
    }

    public void initializeTheUIComponents() {
        errorMessage = findViewById(R.id.errorMessage);
        retryAgainButton = findViewById(R.id.retryAgainButton);
        retryAgainButton.setClickable(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeTheUIComponents();
        // Running the jumpToNextActivity after the screen and the UI elements have been rendered
        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                try {
                    new FirstRunDBCreator().execute().get();
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteStatement;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.os.AsyncTask;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.util.concurrent.ExecutionException;
公共类MainActivity扩展了AppCompatActivity{
文本视图错误消息;
按钮复位按钮;
SharedReferences SharedRefers;
@SuppressLint(“StaticFieldLeak”)
公共类FirstRunDBCreator扩展异步任务{
字符串结果=”;
私有异常e=null;
@凌驾
受保护的void onPreExecute(){
SharedRefers=getApplicationContext().GetSharedReferences(“android.content.SharedReference”,Context.MODE\u PRIVATE);
如果(!sharedPrefs.contains(“firstRun”)){
sharedPrefs.edit().putBoolean(“firstRun”,true.apply();
}
}
@凌驾
受保护的字符串背景(无效…无效){
字符串结果=”;
//这里所有繁重的行动
//在此处从服务器获取数据并在我的SQLite数据库中创建/存储数据
//我还使用了事务和SQLite preparedStatement来提高插入速度
返回结果;
}
@SuppressLint(“SetTextI18n”)
@凌驾
受保护的void onPostExecute(字符串s){
//关闭数据库连接并在此处处理任何异常
//更新UI元素,例如显示完整消息或文本视图的可见性
}
}
public void初始化UIComponents(){
errorMessage=findViewById(R.id.errorMessage);
retryAgainButton=findViewById(R.id.retryAgainButton);
retryAgainButton.setClickable(错误);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
初始化UIComponents();
//在呈现屏幕和UI元素后运行jumpToNextActivity
getWindow().getDecorView().post(新的Runnable()){
@凌驾
公开募捐{
试一试{
新的FirstRunDBCreator().execute().get();
}捕获(中断异常|执行异常e){
e、 printStackTrace();
}
}
});
}
}

尽管我没有任何高分辨率的图像或任何沉重的前端组件,除了两个文本视图,而且所有沉重的处理都是在后台线程中进行的,但是什么导致编舞跳过这些帧呢?是因为我在
onCreate()
方法中调用AsyncTask,还是因为我在
onPreExecute()
onPostExecute()中有一些代码?
是因为
Runnable()
还是其他原因?

当调用.get()方法时,它将在当前线程(本例中为UI线程)上运行,因此尝试在调用.get()方法时不使用
get

执行,它将在当前线程(本例中为UI线程)上运行,因此尝试在不使用
get
的情况下执行,建议对异步任务使用weakReference上下文,如下所示:

 /.../in your activity
  WeakReference<Context> baseContext = new WeakReference<>(MainActivity.this);

建议对asyncTask使用weakReference上下文,如下所示:

 /.../in your activity
  WeakReference<Context> baseContext = new WeakReference<>(MainActivity.this);

这立即解决了跳帧问题。尽管如此,你能解释一下为什么
get()
要对此负责吗?是否因为在后台运行任务后,在收到结果后,它再次在UI(主)线程中开始工作?本质上是同步还是阻塞?最后,我们应该在什么时候实际使用
get()
@RishabParmar,因为当您调用get()方法时,它会再次在主线程上开始工作,并等待工作完成。这就是为什么它需要时间。这立即解决了跳帧问题。尽管如此,你能解释一下为什么
get()
要对此负责吗?是否因为在后台运行任务后,在收到结果后,它再次在UI(主)线程中开始工作?本质上是同步还是阻塞?最后,我们应该在什么时候实际使用
get()
@RishabParmar,因为当您调用get()方法时,它会再次在主线程上开始工作,并等待工作完成。这就是为什么它需要时间。
  /.../
  new FirstRunDBCreator(baseContext).execute();