这很简单,我看不到,但我检查了与此相关的所有类似问题,但看不到问题。希望一些新鲜的眼睛可以看到发生了什么。
我有一个包含一个活动和6个片段的应用程序,所有片段都通过与主要活动的接口相互交谈。我将第六个片段添加到我的onNewIntent方法中,尽管它什么也没做,但是我得到了带有其他片段之一的ClassCastExption。他们都使用getSupportFragment管理器,并使用android.support.v4.app.Fragment。
当NFC标签与设备接触并根据可见的片段执行一些代码时,会调用OnNewIntent。
当可见读取片段并调用onNewIntent时发生错误。
如果有人可以阐明正在发生的事情,我将非常感激,如果需要更多信息,我会提供。
这是Logcat中的错误。
java.lang.ClassCastException: com.appsolutedevelopment.labourstaff.UI.Fragments.ReadFragment cannot be cast to com.appsolutedevelopment.labourstaff.UI.Fragments.VisitorReadFragment
at com.appsolutedevelopment.labourstaff.Main.onNewIntent(Main.java:253)
at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1228)
at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1240)
at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2946)
at android.app.ActivityThread.performNewIntents(ActivityThread.java:2958)
at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2967)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1648)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
这是onNewIntent方法
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
WriteFragment writeFragment = (WriteFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.fragment_write));
ReadFragment readFragment = (ReadFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.fragment_read));
DelegateRegistrationFragment delegateRegistrationFragment = (DelegateRegistrationFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.fragment_delegate_registration));
VisitorRegistrationFragment visitorRegistrationFragment = (VisitorRegistrationFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.fragment_visitor_registration));
UpdateDelegateFragment updateDelegateFragment = (UpdateDelegateFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.fragment_update_delegate));
VisitorReadFragment visitorReadFragment = (VisitorReadFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.fragment_visitor_read));
if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
//Toast.makeText(this, "NfcIntent!", Toast.LENGTH_SHORT).show();
if(readFragment != null && readFragment.isVisible())
{
Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if(parcelables != null && parcelables.length > 0)
{
readDataFromMessage((NdefMessage) parcelables[0]);
}else{
Toast.makeText(this, "No NDEF messages found!", Toast.LENGTH_SHORT).show();
}
}
}
}
以及导致错误的两个片段 读取片段
public class ReadFragment extends Fragment {
//widgets
TextView mMemberID;
TextView mFirstName;
TextView mLastName;
Context context;
ImageLoader imageLoader;
//vars
private IMainActivity mIMainActivity;
private String tagContent;
static String MEMBER_ID = "member_id";
static String FIRST_NAME = "first_name";
static String LAST_NAME = "last_name";
static String PHOTO = "photo";
private String authToken = null;
private String bearerToken;
private AccountManager mAccountManager;
private Bundle bundle;
public ReadFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIMainActivity.setToolbarTitle(getTag());
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_read, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mMemberID = view.findViewById(R.id.memberID);
mFirstName = view.findViewById(R.id.firstName);
mLastName = view.findViewById(R.id.lastName);
bundle = this.getArguments();
if(bundle != null) {
MEMBER_ID = bundle.getString("member_id");
FIRST_NAME = bundle.getString("first_name");
LAST_NAME = bundle.getString("last_name");
setUI();
}
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mIMainActivity = (Main) getActivity();
}
public void getSingleMember(){
UserClient userClient = ServiceGenerator.createService(UserClient.class);
Call<Member> memberCall = userClient.getMember(tagContent);
memberCall.enqueue(new Callback<Member>() {
@Override
public void onResponse(Call<Member> call, Response<Member> response) {
int statusCode = response.code();
int code = response.code();
Log.d("test", "Code is " + code);
if (code == 200) {
mIMainActivity.setVisitorData(MEMBER_ID, FIRST_NAME, LAST_NAME);
if(response.body() instanceof Member){
Member memberResponse = response.body();
MEMBER_ID = memberResponse.getMember_id();
FIRST_NAME = memberResponse.getFirst_name();
LAST_NAME = memberResponse.getLast_name();
}
}
setUI();
Log.d("Server", "onResponse" + statusCode);
}
@Override
public void onFailure(Call<Member> call, Throwable t) {
Log.d("Server", "Error with something" + t.getMessage() );
}
});
}
public void setUI(){
mMemberID.setText(MEMBER_ID);
mFirstName.setText(FIRST_NAME);
mLastName.setText(LAST_NAME);
String confimation = "Access Granted";
Intent i = new Intent(getActivity(), TagWrittenPopup.class);
i.putExtra("confirmation", confimation);
startActivity(i);
}
public void readDataFromMessage(NdefMessage ndefMessage) {
NdefRecord[] ndefRecords = ndefMessage.getRecords();
if(ndefRecords != null && ndefRecords.length>0){
NdefRecord ndefTextRecord = ndefRecords[0];
tagContent = getTextFromNdefRecord(ndefTextRecord);
getSingleMember();
}else
{
Toast.makeText(getActivity(), "No NDEF records found!", Toast.LENGTH_SHORT).show();
}
}
public String getTextFromNdefRecord(NdefRecord ndefRecord)
{
String tagContent = null;
try {
byte[] payload = ndefRecord.getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
int languageSize = payload[0] & 0063;
tagContent = new String(payload, languageSize + 1,
payload.length - languageSize - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("getTextFromNdefRecord", e.getMessage(), e);
}
return tagContent;
}
private void showMessage(final String msg) {
if (TextUtils.isEmpty(msg))
return;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
}
});
}
}
VisitorReadFragment
public class VisitorReadFragment extends Fragment {
public EditText mToken;
private EditText mFirstName;
private EditText mLastName;
private EditText mOrganisation;
private Spinner mCategory;
private ImageView mImageView;
private Bitmap mPhoto;
private IMainActivity mIMainActivity;
private String token;
private String base64Image;
private String firstName = null;
private String lastName = null;
private String organisation = null;
private String category;
private Bitmap bitmapPhoto;
private Bundle bundle;
public VisitorReadFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIMainActivity.setToolbarTitle(getTag());
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_visitor_registration, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
ArrayAdapter<CharSequence> categoryAdapter = ArrayAdapter.createFromResource(
this.getActivity(), R.array.categories_array, android.R.layout.simple_spinner_item);
categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mImageView = v.findViewById(R.id.photo);
mToken = v.findViewById(R.id.token);
mToken.setText(token);
mFirstName = v.findViewById(R.id.firstName);
mLastName = v.findViewById(R.id.lastName);
mOrganisation = v.findViewById(R.id.organisation);
mCategory = v.findViewById(R.id.categorySelector);
mCategory.setAdapter(categoryAdapter);
bundle = this.getArguments();
if(bundle != null){
base64Image = bundle.getString("photo");
firstName = bundle.getString("first_name");
lastName = bundle.getString("last_name");
organisation = bundle.getString("organisation");
category = "1";
if(category.contains("1")){
v.setBackgroundResource(R.color.s_color);
}
else if(category.contains("2")){
v.setBackgroundResource(R.color.m_color);
}
else if(category.contains("3")){
v.setBackgroundResource(R.color.o_color);
}
setBundleData();
}
return v;
}
public void setBundleData(){
if(category.equals("0")){
mFirstName.setText(firstName);
mLastName.setText(lastName);
mOrganisation.setText(organisation);
mCategory.setSelection(0);
}
else if(category.equals("1")){
mFirstName.setText(firstName);
mLastName.setText(lastName);
mOrganisation.setText(organisation);
mCategory.setSelection(1);
}
else if(category.equals("2")){
mFirstName.setText(firstName);
mLastName.setText(lastName);
mOrganisation.setText(organisation);
mCategory.setSelection(2);
}
else if(category.equals("3")){
mFirstName.setText(firstName);
mLastName.setText(lastName);
mOrganisation.setText(organisation);
mCategory.setSelection(3);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mIMainActivity = (Main) getActivity();
}
@Override
public void onDetach() {
super.onDetach();
}
}
我尝试删除VisitorReadFragment并将其替换为
Fragment fragment = getSupportManager().findFragmentByTag(getString(R.string.fragment_visitor_read));
,并且不会发生错误。
我还尝试了在可见另一个片段并且在此处也不会发生错误时扫描标签,因此我将其范围缩小到了ReadFragment和VisitorReadFragment。
让我知道是否还有其他需要。