从 asynctask 下载的 ArrayList 在完成方法执行后变为 null



我正在开发一个应用程序,其中某些内容将使用AsyncTasks下载到ArrayList中。

问题是,在完成相关方法的执行后,这些 ArrayLists 的值返回到 null,因此当我使用 Intent 将其传递给另一个活动时;它不会发送以前下载的值,而是出现 null 指针异常(我在启动 Intent 之前检查了 ArrayLists 的内容,发现它们都变为 null)。

通过 AsyncTask 获取值的代码:

public class Fragment1 extends Fragment
implements UpdateComponents.AsyncContentsResponse, UpdateComponents.AsyncOtherContentsResponse {
private static ArrayList<String> names;
private static ArrayList<Drawable> images;
private static ArrayList<String> someContents;
private static ArrayList<String> anotherContents;
UpdateComponents.getJsonContentsTextTask contentsTextTask =
new UpdateComponents.getJsonContentsTextTask();
UpdateComponents.getJsonOtherContentsTextTask otherContentsTextTask =
new UpdateComponents.getJsonOtherContentsTextTask();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

contentsTextTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
"https://someurl.com");
otherContentsTextTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
"https://anotherurl.com");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView recycler = (RecyclerView) inflater.inflate(R.layout.fragment,
container, false);
CaptionedImagesAdapter adapter = new CaptionedImagesAdapter(names, images);
recycler.setAdapter(adapter);
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
recycler.setLayoutManager(layoutManager);
adapter.setListener(new CaptionedImagesAdapter.CardViewListener() {
@Override
public void onClick(int position) {
Intent intent = new Intent(getActivity(), AnotherActivity.class);
intent.putExtra(AnotherActivity.CONTENT, someContents.get(position));
intent.putExtra(AnotherActivity.ANOTHER_CONTENT, anotherContents.get(position));
getActivity().startActivity(intent);
}
});
return recycler;
}

@Override
public void passContentsResult(ArrayList<String> result) {
someContents = new ArrayList<>(result);
}

@Override
public void passAnotherContentsResult(ArrayList<String> result) {
anotherContents = new ArrayList<>(result);
}
}

用于从其他活动中的意图获取值的代码:

public class AnotherActivity extends AppCompatActivity {
public static final String CONTENT = "content";
public static final String ANOTHER_CONTENT = "anotherContent";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
TextView contents = findViewById(R.id.contents);
TextView anotherContents = findViewById(R.id.anotherContents);
contents.setText((CharSequence) getIntent().getExtras().get(CONTENT));
anotherContent.setText((CharSequence) getIntent().getExtras().get(ANOTHER_CONTENT));
}
}

如何保留从 AsyncTask 下载的 ArrayLists,以便我可以将其传递给其他活动? 有没有办法保留这些 ArrayLists,这样如果我从其他活动返回,我就不需要再次重新下载它?

提前感谢您的支持。

更新使用简单的代码,我发现发生了同样的问题:

public class MainActivity extends AppCompatActivity {
private static final String TAG = "DownloadData";
TextView tv;
ImageView imageView;
String text;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.TestTxt);
String text = "sometext to text";
Log.d(TAG, "onCreate: " + text);
}
public void onClick(View v){
Log.d(TAG, "onClick: " + text);
tv.setText(text);
}

在onCreate中创建后记录值时,没关系,一旦单击按钮启动onClick方法,我发现它的值为null。

我找到了问题原因,我忘记将 AsyncTask 接口变量分配给它的片段实例:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentsTextTask.contentsResponse = this;
contentsTextTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
"https://someurl.com");
otherContentsTextTask.otherContentsResponse = this;
otherContentsTextTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
"https://anotherurl.com");
}

特别感谢0X0nosugar的贡献和帮助。

最新更新