gpt4 book ai didi

android - 通过 Intent 将编辑文本的文本移动到另一个 Activity 会引发异常

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

我正在尝试在我的 Android 应用程序中实现搜索功能,我想从 Activity Discover 获取用户在搜索编辑文本中编写的内容,并打开一个名为 Activity search 的新 Activity,其中可以显示结果,发布object 是我实现的一个类,我在其中从后端获取数据每次我尝试都会抛出异常这是我这部分的 Activity 发现代码 `

search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
// setContentView(R.layout.search_grid);
// gridView2 = (GridView) findViewById(R.id.search_grid);
RequestParams rp = new RequestParams();
final String url;
if (people.isChecked()) {
rp.add("user", search.getText().toString());
rp.add("counter", "");
url = "search_users";
} else if (tags.isChecked()) {
rp.add("tag", search.getText().toString());
rp.add("counter", "");
url = "search_exact_hashtag";
} else {
rp.add("location", search.getText().toString());
rp.add("counter", "");
url = "search_location";
}
final ArrayList<PostObject> addyExtras = new ArrayList<>();
//Toast.makeText(getApplicationContext()," something " + , Toast.LENGTH_SHORT).show();
PabloClient.post(url, rp, new JSONHandler(getApplicationContext(), new JSONHandler.OnFinishedListener() {
@Override
public void OnFinished(int statusCode, Header[] headers, JSONObject response, int result) {
try {
if (result == 1) {
// Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();

JSONArray posts = response.getJSONArray("data");
setContentView(R.layout.search_grid);
//////////////////////////////////////////////


//////////////////////////////////////////////////////////////////
for (int i = 0; i < posts.length(); i++) {
searchPosts.add(new PostObject(posts.getJSONObject(i)));


addyExtras.add(searchPosts.get(i));

}




}
} catch (JSONException e) {
e.printStackTrace();

}
Intent intent = new Intent(ActivityDiscover.this, ActivitySearch.class);
intent.putParcelableArrayListExtra("mylist", addyExtras);
startActivity(intent);

}
}));
}

});

`

这里是我如何获得搜索 Activity 的 Intent

searchPosts2 = getIntent().getParcelableArrayExtra("mylist");

最佳答案

数组应该在 Bundle 的帮助下传递

发送数组

首先从发送方需要在包中添加数组列表

Bundle data = new Bundle();
data.putParcelableArrayList("mylist", addyExtras);

然后将 bundle 添加到 Intent

 Intent intent = new Intent(ActivityDiscover.this, ActivitySearch.class);
intent .putExtra("content", data);
startActivity(yourIntent);

检索数组

在接收端从 Intent 中获取包

Bundle data =  getIntent().getBundleExtra("content");

然后从包中获取数组列表

searchPosts2  =  data.getParcelableArrayList("mylist");

关于android - 通过 Intent 将编辑文本的文本移动到另一个 Activity 会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37023997/

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