gpt4 book ai didi

java - 为什么我的 clickListener 仅在我第二次调用它时才使我的应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 22:37:30 25 4
gpt4 key购买 nike

我在编写一个简单的 Android 应用程序时遇到了一个小问题。在我的主要 Activity 中,有一个按钮和与之关联的回调在 AsynchTask 中获取用户的 json 文件。获取文件后,我在 textView 中显示其中一个文件的名称。目前一切。然后我旋转我的设备(即使我不旋转它,问题仍然存在)并再次单击该按钮。应用程序崩溃了,我不知道你为什么能帮我?

这是崩溃日志:

2019-12-16 08:47:45.258 16758-16863/com.example.labo4bis E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.labo4bis, PID: 16758
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$4.done(AsyncTask.java:399)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
at android.os.Looper.prepare(Looper.java:108)
at android.os.Looper.prepare(Looper.java:103)
at com.example.labo4bis.MainActivity$LoadUsersTask.doInBackground(MainActivity.java:80)
at com.example.labo4bis.MainActivity$LoadUsersTask.doInBackground(MainActivity.java:71)
at android.os.AsyncTask$3.call(AsyncTask.java:378)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
at java.lang.Thread.run(Thread.java:919) 

这是我的代码:主要 Activity :

public class MainActivity extends AppCompatActivity {
private UserDAO dao = new UserDAO();
private LoadUsersTask task;
@BindView(R.id.button2)
Button actionButton;
@BindView(R.id.hello)
TextView t;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Log.i("lab4","create");


actionButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
task = new LoadUsersTask();
task.execute();
}
});

if(savedInstanceState != null) {
t.setText(savedInstanceState.getString("name"));
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("name",t.getText().toString());
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.i("lab4","destroy");
if(task != null) {
task.cancel(true);
}
}

private class LoadUsersTask extends AsyncTask<Void,Void,List<User>> {
@Override
protected void onPostExecute(List<User> users) {
super.onPostExecute(users);
t.setText(users.get(0).getName());
}

@Override
protected List<User> doInBackground(Void... voids) {
Looper.prepare();
return dao.getAllUsers();
}

@Override
protected void onCancelled() {
super.onCancelled();
Log.i("lab4","asynchdestroy");
}
}
}

DAO 类:

public class UserDAO {
public UserDAO() {
}

public List<User> getAllUsers() {
UserService service = new Retrofit.Builder()
.baseUrl(UserService.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build().create(UserService.class);

List<User> repoList = null;
try {
repoList = service.getUsers().execute().body();
} catch (Exception e) {
e.printStackTrace();
}
return repoList;
}
}

最佳答案

doInBackground() 方法中的这一行是罪魁祸首:

Looper.prepare();
Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
at android.os.Looper.prepare(Looper.java:108)
at android.os.Looper.prepare(Looper.java:103)
at com.example.labo4bis.MainActivity$LoadUsersTask.doInBackground(MainActivity.java:80)

为什么叫这个?我在您的代码中没有看到任何与 Handler 相关的内容。如果您只能删除它,那就这样做吧。

如果您出于我们看不到的原因需要保留它,那么您应该检查一下 looper 是否已经准备好:

if (Looper.myLooper() == null) {
Looper.prepare();
}

关于java - 为什么我的 clickListener 仅在我第二次调用它时才使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59362470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com