片段无法转换为 java.util.concurrent.Executor 错误
Posted
技术标签:
【中文标题】片段无法转换为 java.util.concurrent.Executor 错误【英文标题】:Fragment cannot be cast to java.util.concurrent.Executor error 【发布时间】:2021-08-01 10:08:04 【问题描述】:我正在尝试使用片段登录或注册,在成功登录/注册后,HomeActivity
应该会加载。问题是,当我单击登录/注册按钮时,它会返回到同一片段,而不是转到 HomeActivity.class
。应用程序的初始化页面称为 LoginRegistrationActivity.class,它将在其中调用 SignIn
片段。我确认登录/注册运行良好,但在成功验证后我无法导航到HomeActivity.class
。我不确定 androidManifest.xml 文件是否存在问题。
单击登录后出现此 logcat 错误:
java.lang.ClassCastException:com.fyp.selfzen.fragments.O_LoginRegistration.SignIn 无法转换为 java.util.concurrent.Executor 在 com.fyp.selfzen.fragments.O_LoginRegistration.SignIn.login(SignIn.java:124) 在 com.fyp.selfzen.fragments.O_LoginRegistration.SignIn$1.onClick(SignIn.java:87) 在 android.view.View.performClick(View.java:7448) 在 android.view.View.performClickInternal(View.java:7425) 在 android.view.View.access$3600(View.java:810) 在 android.view.View$PerformClick.run(View.java:28305) 在 android.os.Handler.handleCallback(Handler.java:938) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:223) 在 android.app.ActivityThread.main(ActivityThread.java:7656) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
SignIn.java 片段
public class SignIn extends Fragment
private EditText editText_email, editText_password;
private String email, password;
private FirebaseAuth firebaseAuth;
private ProgressDialog progressDialog;
LoginRegisrationActivity loginRegistration;
public SignIn(LoginRegisrationActivity loginRegistration)
this.loginRegistration = loginRegistration;
public static SignIn newInstance(LoginRegisrationActivity loginRegistration)
SignIn fragment = new SignIn(loginRegistration);
return fragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_login, container, false);
progressDialog = new ProgressDialog(getContext());
firebaseAuth = FirebaseAuth.getInstance();
editText_email = view.findViewById(R.id.editText_email_login_activity);
editText_password = view.findViewById(R.id.editText_password_login_activity);
TextView button_login = view.findViewById(R.id.button_login_activity);
TextView textView_signup_login = view.findViewById(R.id.textView_signup_login);
final SmoothCheckBox checkBox = view.findViewById(R.id.checkbox_login_activity);
checkBox.setChecked(false);
button_login.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
email = editText_email.getText().toString();
password = editText_password.getText().toString();
editText_email.clearFocus();
editText_password.clearFocus();
login(email, password);
);
textView_signup_login.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
SignUp f2 = SignUp.newInstance(loginRegistration);
loginRegistration.loadFrag(f2, getResources().getString(R.string.regis));
);
return view;
public void login(String email, String password)
editText_email.setError(null);
editText_password.setError(null);
if (!isValidMail(email) || email.isEmpty())
editText_email.requestFocus();
editText_email.setError(getResources().getString(R.string.please_enter_email));
else if(password.isEmpty())
editText_password.requestFocus();
editText_password.setError(getResources().getString(R.string.please_enter_password));
else
progressDialog.setMessage("Please wait...");
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);
firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if (task.isSuccessful())
Toast.makeText(getContext(), "Successfully sign in!", Toast.LENGTH_LONG).show();
// Intent i = new Intent(loginRegistration, HomeActivity.class); //First try to go to HomeActivity
Intent i = new Intent(getContext(), HomeActivity.class); //Second try
startActivity(i);
//loginRegistration.finish();
else
Toast.makeText(getContext(), "Sign in failed", Toast.LENGTH_LONG).show();
progressDialog.dismiss();
//onComplete
); // firebaseAuth
// else
// login end
SignUp.java 片段
public class SignUp extends Fragment
private EditText editText_name, editText_email, editText_password, editText_phoneNo;
private String name, email, password, phoneNo;
private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;
LoginRegisrationActivity loginRegistration;
public SignUp(LoginRegisrationActivity loginRegistration)
this.loginRegistration = loginRegistration;
// Required empty public constructor
public static SignUp newInstance(LoginRegisrationActivity loginRegistration)
SignUp fragment = new SignUp(loginRegistration);
return fragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_registration, container, false);
progressDialog = new ProgressDialog(getContext());
firebaseAuth = FirebaseAuth.getInstance();
editText_name = view.findViewById(R.id.editText_name_register);
editText_email = view.findViewById(R.id.editText_email_register);
editText_password = view.findViewById(R.id.editText_password_register);
editText_phoneNo = view.findViewById(R.id.editText_phoneNo_register);
TextView button_submit = view.findViewById(R.id.button_submit);
TextView textView_login = view.findViewById(R.id.textView_login_register);
// Go to login page
textView_login.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
SignIn f1 = SignIn.newInstance(loginRegistration);
loginRegistration.loadFrag(f1, getResources().getString(R.string.login));
);
button_submit.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
name = editText_name.getText().toString();
email = editText_email.getText().toString();
password = editText_password.getText().toString();
phoneNo = editText_phoneNo.getText().toString();
form();
);
return view;
private boolean isValidMail(String email)
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
public void form()
editText_name.setError(null);
editText_email.setError(null);
editText_password.setError(null);
editText_phoneNo.setError(null);
if (name.equals("") || name.isEmpty())
editText_name.requestFocus();
editText_name.setError(getResources().getString(R.string.please_enter_name));
else if (!isValidMail(email) || email.isEmpty())
editText_email.requestFocus();
editText_email.setError(getResources().getString(R.string.please_enter_email));
else if (password.equals("") || password.isEmpty())
editText_password.requestFocus();
editText_password.setError(getResources().getString(R.string.please_enter_password));
else if (phoneNo.equals("") || phoneNo.isEmpty())
editText_phoneNo.requestFocus();
editText_phoneNo.setError(getResources().getString(R.string.please_enter_phone));
else
editText_name.clearFocus();
editText_email.clearFocus();
editText_password.clearFocus();
editText_phoneNo.clearFocus();
progressDialog.setMessage("Please wait...");
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);
firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if(task.isSuccessful())
Toast.makeText(getContext(), "Successfully registered!", Toast.LENGTH_LONG).show();
//Intent i = new Intent(loginRegistration, HomeActivity.class);
Intent i = new Intent(getActivity(), HomeActivity.class);
startActivity(i);
//loginRegistration.finish();
else
Toast.makeText(getContext(), "Sign up failed", Toast.LENGTH_LONG).show();
progressDialog.dismiss();
);
// end of form
LoginRegistrationActivity.class
public class LoginRegisrationActivity extends AppCompatActivity
private String curent;
VideoView vide;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_regisration);
vide = findViewById(R.id.vide);
String video_url = "android.resource://" + getPackageName() + "/" + R.raw.login_video;
Uri videoUri = Uri.parse(video_url);
vide.setVideoURI(videoUri);
vide.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
@Override
public void onPrepared(MediaPlayer mp)
mp.setLooping(true);
vide.requestFocus();
vide.start();
);
SignIn f1 = SignIn.newInstance(this);
loadFrag(f1, getResources().getString(R.string.login));
public void loadFrag(Fragment f1, String name)
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(!name.equals(curent))
curent =name;
ft.replace(R.id.frame_layout, f1, name);
ft.commitAllowingStateLoss();
AndroidManifest.xml
<activity
android:name="com.fyp.selfzen.activities.LoginRegisrationActivity"
android:screenOrientation="portrait" />
<activity
android:name="com.fyp.selfzen.activities.HomeActivity"
android:label="@string/title_activity_home"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
【问题讨论】:
【参考方案1】:在 SignIn.java 和 SignUp.java 片段中,为 firebaseAuth 添加 addOnCompleteListener()。因此,在其中传递参数,
而不是,
firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>()
致,
firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(getContext(), new OnCompleteListener<AuthResult>()
如果,getContext() 不起作用然后尝试编写 getActivity()。
【讨论】:
嗨!我只是将我的代码更新为firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>()
。我刚刚删除了(Executor) this,
。但是在成功登录/注册后,我仍然会重定向回登录页面。
当您单击登录按钮时,您将获得准确的吐司。 “注册成功!”还是“注册失败”?如果您看不到 toast,那么只需在您的日志中打印并检查。
"注册成功!"是显示然后登录页面再次显示。
我尝试使用除HomeActivity.class
之外的其他类来加载,但得到的结果仍然相同。它加载回登录页面。
你调试了吗??你在哪一行出错了??并且可以发布您的家庭活动吗?以上是关于片段无法转换为 java.util.concurrent.Executor 错误的主要内容,如果未能解决你的问题,请参考以下文章
在遍历 ArrayList 时获取 java.util.ConcurrentModificationException
调用片段活动错误无法实例化活动。无法转换为 android.app.Activity
片段无法转换为 java.util.concurrent.Executor 错误