[英]Execute AsyncTask in Fragment from another Activity class and update UI inside it
I'm trying to execute AsyncTask
from a fragment class (eg MyFragment.java)
from another Activity class (eg MyActivity.java)
.我正在尝试从另一个活动 class
(eg MyActivity.java)
的片段 class (eg MyFragment.java)
执行AsyncTask
。
the fragment class MyFragment.java
is updating UI on it's elements:片段 class
MyFragment.java
正在更新其元素的 UI:
public class MyFragment extends Fragment {
private SwipeRefreshLayout myRefreshLayout;
private ArrayAdapter myListAdapter;
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myListAdapter = new myCustomAdapter(getContext(), myDataArray);
myRefreshLayout = myView.findViewById(R.id.myRefreshLayout);
}
public class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// UI...
myRefreshLayout.setRefreshing(true);
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// UI...
myRefreshLayout.setRefreshing(false);
myListAdapter.notifyDataSetChanged();
}
@Override
protected Void doInBackground(Void... params) {
// code...
return null;
}
}
}
I'm trying to Execute AsyncTask from MyActivity.java
我正在尝试从
MyActivity.java
执行 AsyncTask
MyFragment myfragment = new MyFragment();
myfragment.new MyTask.execute();
The Application crashes at onPreExecute
:应用程序在
onPreExecute
崩溃:
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.swiperefreshlayout.widget.SwipeRefreshLayout.
java.lang.NullPointerException:尝试调用虚方法'void androidx.swiperefreshlayout.widget.SwipeRefreshLayout。 setRefreshing(boolean) ' on a null object reference.
setRefreshing(boolean) ' 在 null object 参考上。
I'm assuming this is because it couldn't access myRefreshLayout
to perform setRefreshing(true)
on it, because i'm running the task from another thread MyActivity.java
我假设这是因为它无法访问
myRefreshLayout
以对其执行setRefreshing(true)
,因为我正在从另一个线程MyActivity.java
运行任务
How can i execute the AsyncTask on the Fragment .. from the Activity and change the UI?我如何从Activity执行Fragment上的AsyncTask .. 并更改 UI?
Thanks.谢谢。
Idk if this would be a nice solution.不知道这是否是一个很好的解决方案。 But I think you can make MainActivity implements this custom interface.
但我认为你可以让 MainActivity 实现这个自定义接口。 It'd be a callback that should be execute after the swipe action done.
这将是一个回调,应该在滑动操作完成后执行。
public class MainActivity extends AppCompatActivity implements MyAsyncCallback {
....
....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFragment myfragment = new MyFragment();
myfragment.new MyTask(this).execute();
}
@Override
public void onPreExecute(Boolean isRefreshing) {
if(isRefreshing){
// UPDATE MAIN UI
}
}
@Override
public void onPostExecute(Boolean isRefreshing) {
if(isRefreshing){
// UPDATE MAIN UI
}
}
}
interface MyAsyncCallback {
void onPreExecute(Boolean isRefreshing);
void onPostExecute(Boolean isRefreshing);
}
in your fragment在你的片段中
public class MyTask extends AsyncTask<Void, Void, Void> {
WeakReference<MyAsyncCallback> myListener;
public MyTask(MyAsyncCallback listener){
this.myListener = listener
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// don't forget to return view
}
@Override
protected void onPreExecute() {
super.onPreExecute();
MyAsyncCallback myListener = this.myListener.get();
if (myListener != null) {
myListener.onPreExecute(true);
}
myRefreshLayout.setRefreshing(true);
}
//same for onPostExecute()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.