- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名初学者,正在编写包含两个 Activity 的代码。两者都有自己的Fragment
。在第一个 Activity (在其 fragment 中)中,用户输入一些字段。结果将显示在第二个 Activity 上(在其列表 fragment 中)。
我尝试使用 bundle 手动获取状态。现在,我使用回调来做到这一点。但是,我收到错误消息 java.lang.ClassCastException: Activity 必须实现 fragment 的回调
。
第一个 Activity 是:
public class AssetRegistrationInfoPage extends Page {
public static final String ASSET_TYPE_NAME_KEY = "assetTypeName";
public static final String ASSET_ID_DATA_KEY = "assetID";
public static final String PROJECT_CODE_DATA_KEY = "projectCode";
public static final String REMARK_DATA_KEY = "remark";
public AssetRegistrationInfoPage(ModelCallbacks callbacks, String title) {
super(callbacks, title);
}
@Override
public Fragment createFragment() {
return AssetRegistrationInfoFragment.create(getKey());
}
@Override
public void getReviewItems(ArrayList<ReviewItem> dest) {
dest.add(new ReviewItem("Nama Asset", mData.getString(ASSET_TYPE_NAME_KEY), getKey(), 0, null));
dest.add(new ReviewItem("Kode Asset", mData. getString(ASSET_ID_DATA_KEY), getKey(), 0, null));
dest.add(new ReviewItem("Kode Project", mData.getString(PROJECT_CODE_DATA_KEY), getKey(),0,null));
dest.add(new ReviewItem("Kondisi Asset", mData.getString(REMARK_DATA_KEY), getKey(), 0, null));
}
@Override
public boolean isCompleted() {
return (!TextUtils.isEmpty(mData.getString(ASSET_TYPE_NAME_KEY)) &&
!TextUtils.isEmpty(mData.getString(ASSET_ID_DATA_KEY)) &&
!TextUtils.isEmpty(mData.getString(PROJECT_CODE_DATA_KEY)) &&
!TextUtils.isEmpty(mData.getString(REMARK_DATA_KEY)));
}
}
第一个 fragment :
public class AssetRegistrationInfoFragment extends Fragment {
private static final String ARG_KEY = "key";
private PageFragmentCallbacks mCallbacks;
private String mKey;
private AssetRegistrationInfoPage mPage;
private TextView mAssetID;
private AutoCompleteTextView mAssetTypeName, mProjectCode;
private TextView mRemark;
private Button mTakePicture;
static final int REQUEST_PICTURE_CAPTURE = 1;
private String pictureFilePath;
Bitmap bitmap;
ImageView mImgView;
ArrayList<String> projectCodes = new ArrayList<String>();
ArrayList<String> assetTypeNames = new ArrayList<String>();
public static AssetRegistrationInfoFragment create(String key) {
Bundle args = new Bundle();
args.putString(ARG_KEY, key);
AssetRegistrationInfoFragment fragment = new AssetRegistrationInfoFragment();
fragment.setArguments(args);
return fragment;
}
public AssetRegistrationInfoFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
mKey = args.getString(ARG_KEY);
mPage = (AssetRegistrationInfoPage) mCallbacks.onGetPage(mKey);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
boolean cancel = false;
mAssetTypeName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1,
int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
mPage.getData().putString(AssetRegistrationInfoPage.ASSET_TYPE_NAME_KEY,
(editable != null) ? editable.toString() : null);
mPage.notifyDataChanged();
}
});
mAssetID.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1,
int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
mPage.getData().putString(AssetRegistrationInfoPage.ASSET_ID_DATA_KEY,
(editable != null) ? editable.toString() : null);
mPage.notifyDataChanged();
}
});
mProjectCode.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1,
int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
mPage.getData().putString(AssetRegistrationInfoPage.PROJECT_CODE_DATA_KEY,
(editable != null) ? editable.toString() : null);
mPage.notifyDataChanged();
}
});
mRemark.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1,
int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
mPage.getData().putString(AssetRegistrationInfoPage.REMARK_DATA_KEY,
(editable != null) ? editable.toString() : null);
mPage.notifyDataChanged();
}
});
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page_asset_registration_info, container, false);
((TextView) view.findViewById(android.R.id.title)).setText(mPage.getTitle());
getAssetType();
mAssetTypeName = ((AutoCompleteTextView) view.findViewById(R.id.assetTypeName));
mAssetTypeName.setText(mPage.getData().getString(AssetRegistrationInfoPage.ASSET_TYPE_NAME_KEY));
ArrayAdapter<String> assetTypeNameAdapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.simple_dropdown_item_1line, assetTypeNames);
mAssetTypeName.setAdapter(assetTypeNameAdapter);
mAssetID = ((TextView) view.findViewById(R.id.assetID));
mAssetID.setText(mPage.getData().getString(AssetRegistrationInfoPage.ASSET_ID_DATA_KEY));
getProjects();
mProjectCode = ((AutoCompleteTextView) view.findViewById(R.id.projectCode));
mProjectCode.setText(mPage.getData().getString(AssetRegistrationInfoPage.PROJECT_CODE_DATA_KEY));
mProjectCode.setThreshold(1);
ArrayAdapter<String> projectCodeAdapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.simple_dropdown_item_1line, projectCodes);
mProjectCode.setAdapter(projectCodeAdapter);
mRemark = ((TextView) view.findViewById(R.id.remark));
mRemark.setText(mPage.getData().getString(AssetRegistrationInfoPage.REMARK_DATA_KEY));
return view;
}
private void getProjects() {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<List<Project>> call = apiService.getProjects("");
call.enqueue(new Callback<List<Project>>() {
@Override
public void onResponse(Call<List<Project>> call, Response<List<Project>> response) {
for (Project project : response.body()) {
projectCodes.add(project.getProjectCode());
}
}
@Override
public void onFailure(Call<List<Project>> call, Throwable t) {
Toast.makeText(getActivity(), "Unable to fetch Data " , Toast.LENGTH_LONG).show();
}
});
}
private void getAssetType() {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<List<AssetType>> call = apiService.getAssetTypes("");
call.enqueue(new Callback<List<AssetType>>() {
@Override
public void onResponse(Call<List<AssetType>> call, Response<List<AssetType>> response) {
for (AssetType assetType : response.body()) {
assetTypeNames.add(assetType.getName());
}
}
@Override
public void onFailure(Call<List<AssetType>> call, Throwable t) {
Toast.makeText(getActivity(), "Unable to fetch Data ", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof PageFragmentCallbacks)) {
throw new ClassCastException("Activity must implement PageFragmentCallbacks");
}
mCallbacks = (PageFragmentCallbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
@Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
// In a future update to the support library, this should override setUserVisibleHint
// instead of setMenuVisibility.
if ( mAssetTypeName != null && mAssetID != null && mProjectCode != null && mRemark != null){
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (!menuVisible) {
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}
}
}
}
第二页
public class AssetRegConfirmationPage extends Page {
public static final String ASSET_TYPE_NAME_KEY = "assetTypeName";
public AssetRegConfirmationPage(ModelCallbacks callbacks, String title) {
super(callbacks, title);
}
@Override
public Fragment createFragment() {
return AssetRegConfirmationFragment.create(getKey());
}
@Override
public void getReviewItems(ArrayList<ReviewItem> dest) {
dest.add(new ReviewItem("Nama Asset", mData.getString(ASSET_TYPE_NAME_KEY), getKey(), 0, null));
}
}
第二个 fragment
public class AssetRegConfirmationFragment extends ListFragment implements ModelCallbacks {
private static final String ARG_KEY = "key";
private Callbacks mCallbacks;
private AbstractWizardModel mWizardModel;
private List<ReviewItem> mCurrentReviewItems;
private ReviewAdapter mReviewAdapter;
public AssetRegConfirmationFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReviewAdapter = new ReviewAdapter();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_page, container, false);
TextView titleView = (TextView) rootView.findViewById(android.R.id.title);
titleView.setText(R.string.review);
titleView.setTextColor(getResources().getColor(R.color.colorPrimaryOld));
ListView listView = (ListView) rootView.findViewById(android.R.id.list);
setListAdapter(mReviewAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof Callbacks)) {
throw new ClassCastException("Activity must implement fragment's callbacks");
}
mCallbacks = (Callbacks) activity;
mWizardModel = mCallbacks.onGetModel();
mWizardModel.registerListener(this);
onPageTreeChanged();
}
@Override
public void onPageTreeChanged() {
onPageDataChanged(null);
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
mWizardModel.unregisterListener(this);
}
@Override
public void onPageDataChanged(Page changedPage) {
ArrayList<ReviewItem> reviewItems = new ArrayList<ReviewItem>();
for (Page page : mWizardModel.getCurrentPageSequence()) {
page.getReviewItems(reviewItems);
}
Collections.sort(reviewItems, new Comparator<ReviewItem>() {
@Override
public int compare(ReviewItem a, ReviewItem b) {
return a.getWeight() > b.getWeight() ? +1 : a.getWeight() < b.getWeight() ? -1 : 0;
}
});
mCurrentReviewItems = reviewItems;
if (mReviewAdapter != null) {
mReviewAdapter.notifyDataSetInvalidated();
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCallbacks.onEditScreenAfterReview(mCurrentReviewItems.get(position).getPageKey());
}
public interface Callbacks {
AbstractWizardModel onGetModel();
void onEditScreenAfterReview(String pageKey);
}
private class ReviewAdapter extends BaseAdapter {
@Override
public boolean hasStableIds() {
return true;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public Object getItem(int position) {
return mCurrentReviewItems.get(position);
}
@Override
public long getItemId(int position) {
return mCurrentReviewItems.get(position).hashCode();
}
@Override
public View getView(int position, View view, ViewGroup container) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View rootView = inflater.inflate(R.layout.list_item_review, container, false);
ReviewItem reviewItem = mCurrentReviewItems.get(position);
String value = reviewItem.getDisplayValue();
if (TextUtils.isEmpty(value)) {
value = "(None)";
}
((TextView) rootView.findViewById(android.R.id.text1)).setText(reviewItem.getTitle());
((TextView) rootView.findViewById(android.R.id.text2)).setText(value);
return rootView;
}
@Override
public int getCount() {
return mCurrentReviewItems.size();
}
}
public static AssetRegConfirmationFragment create(String key) {
Bundle args = new Bundle();
args.putString(ARG_KEY, key);
AssetRegConfirmationFragment fragment = new AssetRegConfirmationFragment();
fragment.setArguments(args);
return fragment;
}
}
我不知道我应该做什么才能完成这项工作。
这是页面源代码:
public abstract class Page implements PageTreeNode {
/**
* The key into {@link #getData()} used for wizards with simple (single) values.
*/
public static final String SIMPLE_DATA_KEY = "_";
protected ModelCallbacks mCallbacks;
/**
* Current wizard values/selections.
*/
protected Bundle mData = new Bundle();
protected String mTitle;
protected boolean mRequired = false;
protected String mParentKey;
protected Page(ModelCallbacks callbacks, String title) {
mCallbacks = callbacks;
mTitle = title;
}
public Bundle getData() {
return mData;
}
public String getTitle() {
return mTitle;
}
public boolean isRequired() {
return mRequired;
}
void setParentKey(String parentKey) {
mParentKey = parentKey;
}
@Override
public Page findByKey(String key) {
return getKey().equals(key) ? this : null;
}
@Override
public void flattenCurrentPageSequence(ArrayList<Page> dest) {
dest.add(this);
}
public abstract Fragment createFragment();
public String getKey() {
return (mParentKey != null) ? mParentKey + ":" + mTitle : mTitle;
}
public abstract void getReviewItems(ArrayList<ReviewItem> dest);
public boolean isCompleted() {
return true;
}
public Bitmap getPicture() {
return mData.getParcelable(getKey() + "_" + getTitle().trim());
}
public void resetData(Bundle data) {
mData = data;
notifyDataChanged();
}
public void notifyDataChanged() {
mCallbacks.onPageDataChanged(this);
}
public Page setRequired(boolean required) {
mRequired = required;
return this;
}
}
最佳答案
查看第二个 fragment
中的代码:
if (!(activity instanceof Callbacks)) {
throw new ClassCastException("Activity must implement fragment's callbacks");
}
使用第二个 fragment
的 Activity
应该实现Callbacks
接口(interface),例如:
class YourActivity implements Callbacks {
}
关于java - 如何修复 'java.lang.ClassCastException: Activity must implement fragment' s 回调',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288083/
我正在研究 learnyounode 的 HTTP 客户端作业。 我想知道为什么控制台记录来自response.on(“end”,callback)的数据仅输出预期输出的最后一部分,而控制台记录来自r
我正在尝试创建一个对象列表(在我的示例中为 List),我在其中使用 json 将对象添加到此列表,但该列表仍为空。这是我的代码: public List readCardsFromJson() {
我有一个 JavaScript 函数“print_something”,它在大约 300 个 jsp 帮助页面中实现。我发现这个“print_something”函数必须被纠正。所以我正在寻找一个不更
有 2 个 HTML 下拉列表,一个用于 12 小时时间,一个用于每小时 5 分钟的时间间隔。 .. 1 .. 12 .. 0 .. 55 .. 一直在尝试使用 if/
我有一个 A 类,我打算在它与设备驱动程序交互时将其放入共享库中。 我有一个 B 类,将来可能是 C、D、E...,它将使用共享库中的 A 类。 我想要在类 A 中设置回调函数的功能,以便当特定事件发
我需要能够在处理完 Observable.next() 之后执行回调。 我有一个组件“A”,它有一个主题使用 Subject.next() 发送通知。我有一个组件“B”,它订阅了 Subject.as
我有一张在顶部和底部单元格下方带有阴影的表格(此处使用 Matt Gallagher 的解决方案:http://cocoawithlove.com/2009/08/adding-shadow-effe
有人可以向我解释一下为什么这段代码有效 renderSquare(i) { return ( this.handleClick(i)} /> ); } 但
我可以让两个不同的客户端监听相同的 WCF 回调并让它们都接收相同的数据而不必进行两次处理吗? 最佳答案 不是真的 - 至少不是直接的。你所描述的听起来很像发布/订阅模式。 WCF 服务基本上在任何给
我是 SignalR 的新手,如果这个问题太明显,我深表歉意,但我在文档中找不到任何答案。 这是我的代码。 /*1*/ actions.client.doActionA = function (r
我有这个应用程序,您可以在其中输入一些文本并按下一个按钮,将此文本添加到自定义小部件中。这是代码: import 'dart:core'; import 'package:flutter/materi
我读到当您还想使用模型回调时不能使用 Keras 进行交叉验证,但是 this post表明这毕竟是可能的。但是,我很难将其纳入我的上下文。 为了更详细地探讨这个问题,我正在关注 machinelea
我尝试在重力表单中提交表单失败后运行一些 jQuery 代码,也就是验证发现错误时。 我尝试使用 Ajax:complete 回调,但它根本不触发。 我尝试运行的代码基本上将监听器添加到选择下拉列表中
我有一个 $image,我 .fadeIn 和 .fadeOut,然后 .remove .fadeOut 完成。这是我的代码: $image .fadeIn() .fadeOut(func
我正在处理一个自定义文件路径类,它应该始终执行一个函数 写入相应的系统文件及其文件对象后 关闭。该函数将文件路径的内容上传到远程位置。 我希望上传功能完全在用户的幕后发生 透视,即用户可以像使用其他任
这里是 javascript 新手,所以回调在我的大脑中仍然有点不确定。 我想做的是:给定一个“菜单”,它是一个 objectId 数组,查询与该 objectId 相对应的每个 foodItem,获
我正在学习回调,我编写了以下代码: var http = require('http'); var str = ""; var count = 2; function jugglingAsync(ca
这是我的困境,我有一系列被调用的函数,我正在使用回调函数在它们完成时执行函数。回调返回一个值并且效果也很好,我的问题是当我向回调添加参数时我无法再访问返回值。这是一个有效的例子: function m
This question already has answers here: Explanation of function pointers (4个答案) 上个月关闭。 如何将函数指针作为参数传递
我无法让以下代码工作。假设 ajax 调用有效,并且 msg['username'] 预设为 'john'。我想我对如何将变量传递给回调感到困惑。编辑:我认为我的主要困惑是如何从 Ajax 中获取“m
我是一名优秀的程序员,十分优秀!