mcpixel
Newbie
Stack trace:
UploadWorker.java:
MainActivity.java:
I am writing on android 4.1.1 and it seems this error applies to my code, but I did not understand how to fix it. I will be glad to any help
Java:
08-01 19:19:43.254 5291-5291/com.example.app1 E/APP: RUNNING
08-01 19:19:43.254 5291-5291/com.example.app1 E/APP: onChanged
08-01 19:19:43.259 5291-5320/com.example.app1 E/WM-WorkerWrapper: Work [ id=495d1e86-0f1f-42a5-9997-3a3c01b878d3, tags={ *****, com.example.app1.UploadWorker } ] failed because it threw an exception/error
java.util.concurrent.ExecutionException: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:516)
at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:284)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.app.Activity.<init>(Activity.java:749)
at androidx.core.app.ComponentActivity.<init>(ComponentActivity.java:39)
at androidx.activity.ComponentActivity.<init>(ComponentActivity.java:84)
at androidx.fragment.app.FragmentActivity.<init>(FragmentActivity.java:127)
at androidx.appcompat.app.AppCompatActivity.<init>(AppCompatActivity.java:77)
at com.example.app1.MainActivity.<init>(MainActivity.java:19)
at com.example.app1.UploadWorker.doWork(UploadWorker.java:20)
at androidx.work.Worker$1.run(Worker.java:85)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Java:
package com.example.app1;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
public class UploadWorker extends Worker {
public UploadWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
String key = new MainActivity().KEY_NAME;
Data ret = new Data.Builder().putLong(key, System.currentTimeMillis()).build();
Log.e("APP", "doWork");
return Result.success(ret);
}
}
Java:
package com.example.app1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.work.BackoffPolicy;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
public final String KEY_NAME = "string1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("START", "Start");
Constraints constraints = new Constraints.Builder().setRequiresCharging(true).build();
Data data = new Data.Builder().putString(KEY_NAME, "Mike").build();
OneTimeWorkRequest uploadWordRequest = new OneTimeWorkRequest.Builder(UploadWorker.class)
.setConstraints(constraints)
.setInitialDelay(10, TimeUnit.SECONDS)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 5, TimeUnit.SECONDS)
.setInputData(data)
.addTag("tag")
.build();
WorkManager w = WorkManager.getInstance(getApplicationContext());
w.enqueue(uploadWordRequest);
LiveData<List<WorkInfo>> l = w.getWorkInfosByTagLiveData("tag");
l.observe(this, new Observer<List<WorkInfo>>() {
@Override
public void onChanged(List<WorkInfo> workInfos) {
if(workInfos.get(0).getState() == WorkInfo.State.RUNNING)
Log.e("APP", "RUNNING");
Log.e("APP", "onChanged");
}
});
}
}