java.lang.RuntimeException: 无法实例化 activity ComponentInfo{.UpdateProfile 片段无法转换为 android.app.Activity
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{.UpdateProfileFragment cannot be cast to android.app.Activity
点击我的 AccountFragment 中的按钮后出现此错误
public class AccountFragment extends Fragment {
private static final String TAG = "Profile" ;
FirebaseAuth firebaseAuth;
FirebaseUser currentUser;
DatabaseReference databaseReference;
TextView tv_name, tv_email, tv_gender, tv_home, tv_phone;
private String userID;
Button btn_update;
public AccountFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_account, container, false);
tv_name=view.findViewById(R.id.tv_name);
tv_email=view.findViewById(R.id.tv_email);
tv_gender=view.findViewById(R.id.tv_gender);
tv_home=view.findViewById(R.id.tv_home);
tv_phone=view.findViewById(R.id.tv_phone);
btn_update=view.findViewById(R.id.btn_update);
//ini
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
userID = currentUser.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference("Customer").child(firebaseAuth.getUid());
//For Retrieve Information
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d( TAG, "onDataChange :" +dataSnapshot);
Customer cust = dataSnapshot.getValue(Customer.class);
tv_name.setText(cust.getName());
tv_email.setText(cust.getEmail());
tv_gender.setText(cust.getGender());
tv_home.setText(cust.getHome_address());
tv_phone.setText(cust.getTelephone_number());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//For Update Method
btn_update.setOnClickListener( new View.OnClickListener(){
@Override
public void onClick(View view) {
openUpdateProfile();
}
});
return view;
}
// For update and pass information method
private void openUpdateProfile() {
Intent intent= new Intent(getActivity(), UpdateProfileFragment.class);
//pass value from current to next page
intent.putExtra("name",tv_name.getText().toString().trim());
intent.putExtra("email", tv_email.getText().toString().trim());
intent.putExtra("Home Address",tv_home.getText().toString().trim());
intent.putExtra("Telephone", tv_phone.getText().toString().trim());
startActivity(intent);
}
点击按钮后出现这个错误
2019-09-22 22:20:32.757 6631-6631/com.example.g E/AndroidRuntime:致命异常:主要
过程:com.example.g,PID:6631
java.lang.RuntimeException: 无法实例化 activity ComponentInfo{com.example.g/com.example.g.UpdateProfileFragment}: java.lang.ClassCastException: com.example.g.UpdateProfileFragment无法转换为 android.app.Activity
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
在 android.app.Activity线程$H.handleMessage(ActivityThread.java:1808)
在 android.os.Handler.dispatchMessage(Handler.java:106)
在 android.os.Looper.loop(Looper.java:193)
在 android.app.ActivityThread.main(ActivityThread.java:6669)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
原因:java.lang.ClassCastException:com.example.gerobokgo.UpdateProfileFragment 无法转换为 android.app.Activity
在 android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
在 androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
在 android.app.Instrumentation.newActivity(Instrumentation.java:1215)
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
在 android.app.Activity线程$H.handleMessage(ActivityThread.java:1808)
在 android.os.Handler.dispatchMessage(Handler.java:106)
在 android.os.Looper.loop(Looper.java:193)
在 android.app.ActivityThread.main(ActivityThread.java:6669)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
UpdateProfileFragment
public class UpdateProfileFragment extends Fragment implements View.OnClickListener {
EditText tv_name, tv_email, tv_home, tv_phone;
Button btn_update;
FirebaseAuth firebaseAuth;
FirebaseUser currentUser;
DatabaseReference databaseReference;
public UpdateProfileFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_update_profile, container, false);
//get data from intert (data from previous page)
Intent intent = getActivity().getIntent();
String name = intent.getStringExtra("Name");
String email = intent.getStringExtra("Email");
String address = intent.getStringExtra(" Shipping Address");
String phone = intent.getStringExtra("Telephone Number");
//ini
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference("Customer").child(firebaseAuth.getUid());
tv_name = view.findViewById(R.id.tv_name);
tv_email = view.findViewById(R.id.tv_email);
tv_home = view.findViewById(R.id.tv_home);
tv_phone = view.findViewById(R.id.tv_phone);
tv_name.setText(name);
tv_email.setText(email);
tv_home.setText(address);
tv_phone.setText(phone);
btn_update.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
}
private void updateProfile() {
Intent intent = getActivity().getIntent();
String userUid = firebaseAuth.getCurrentUser().getUid();
String name = tv_name.getText().toString().trim();
String email = tv_email.getText().toString().trim();
String home = tv_home.getText().toString().trim();
String phone = tv_phone.getText().toString().trim();
Customer cust = new Customer(userUid, name, email, home, phone);
databaseReference.child("Customer").child(userUid).setValue(cust).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(getActivity(), "Update Successfully", Toast.LENGTH_SHORT).show();
getActivity().finish();
startActivity(new Intent(getActivity(), AccountFragment.class));
}
}
});
}
}
您无法使用 startActivity 打开片段,您需要实现一个回调,该回调会在您的用户个人资料更新成功时触发。在你的主机activity中创建如下界面:
public interface UpdateProfileSuccess{
void presentAccountFragment();
}
同时在 activity 中将其实例化为:
UpdateProfileSuccess callback = new UpdateProfileSuccess(){
@Override
void presentAccountFragment(){
getSupportFragmentManager().beginTransaction().replace(R.id.container,new
AccuontFragment()).commit();
}
}
在您的更新配置文件片段中创建一个构造函数,将该回调作为:
private MainActivity.UpdateProfileSuccess callback;
public UpdateProfileFragment(MainActivity.UpdateProfileSuccess callback){
this.callback = callback;
}
最后在您的请求的 onComplete 中将该回调消费为:
callback.presentAccountFragment();
这样您的 activity 将负责呈现 AccountFragment。
点击我的 AccountFragment 中的按钮后出现此错误
public class AccountFragment extends Fragment {
private static final String TAG = "Profile" ;
FirebaseAuth firebaseAuth;
FirebaseUser currentUser;
DatabaseReference databaseReference;
TextView tv_name, tv_email, tv_gender, tv_home, tv_phone;
private String userID;
Button btn_update;
public AccountFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_account, container, false);
tv_name=view.findViewById(R.id.tv_name);
tv_email=view.findViewById(R.id.tv_email);
tv_gender=view.findViewById(R.id.tv_gender);
tv_home=view.findViewById(R.id.tv_home);
tv_phone=view.findViewById(R.id.tv_phone);
btn_update=view.findViewById(R.id.btn_update);
//ini
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
userID = currentUser.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference("Customer").child(firebaseAuth.getUid());
//For Retrieve Information
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d( TAG, "onDataChange :" +dataSnapshot);
Customer cust = dataSnapshot.getValue(Customer.class);
tv_name.setText(cust.getName());
tv_email.setText(cust.getEmail());
tv_gender.setText(cust.getGender());
tv_home.setText(cust.getHome_address());
tv_phone.setText(cust.getTelephone_number());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//For Update Method
btn_update.setOnClickListener( new View.OnClickListener(){
@Override
public void onClick(View view) {
openUpdateProfile();
}
});
return view;
}
// For update and pass information method
private void openUpdateProfile() {
Intent intent= new Intent(getActivity(), UpdateProfileFragment.class);
//pass value from current to next page
intent.putExtra("name",tv_name.getText().toString().trim());
intent.putExtra("email", tv_email.getText().toString().trim());
intent.putExtra("Home Address",tv_home.getText().toString().trim());
intent.putExtra("Telephone", tv_phone.getText().toString().trim());
startActivity(intent);
}
点击按钮后出现这个错误 2019-09-22 22:20:32.757 6631-6631/com.example.g E/AndroidRuntime:致命异常:主要 过程:com.example.g,PID:6631 java.lang.RuntimeException: 无法实例化 activity ComponentInfo{com.example.g/com.example.g.UpdateProfileFragment}: java.lang.ClassCastException: com.example.g.UpdateProfileFragment无法转换为 android.app.Activity 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.Activity线程$H.handleMessage(ActivityThread.java:1808) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6669) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 原因:java.lang.ClassCastException:com.example.gerobokgo.UpdateProfileFragment 无法转换为 android.app.Activity 在 android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) 在 androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41) 在 android.app.Instrumentation.newActivity(Instrumentation.java:1215) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.Activity线程$H.handleMessage(ActivityThread.java:1808) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6669) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
UpdateProfileFragment
public class UpdateProfileFragment extends Fragment implements View.OnClickListener {
EditText tv_name, tv_email, tv_home, tv_phone;
Button btn_update;
FirebaseAuth firebaseAuth;
FirebaseUser currentUser;
DatabaseReference databaseReference;
public UpdateProfileFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_update_profile, container, false);
//get data from intert (data from previous page)
Intent intent = getActivity().getIntent();
String name = intent.getStringExtra("Name");
String email = intent.getStringExtra("Email");
String address = intent.getStringExtra(" Shipping Address");
String phone = intent.getStringExtra("Telephone Number");
//ini
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference("Customer").child(firebaseAuth.getUid());
tv_name = view.findViewById(R.id.tv_name);
tv_email = view.findViewById(R.id.tv_email);
tv_home = view.findViewById(R.id.tv_home);
tv_phone = view.findViewById(R.id.tv_phone);
tv_name.setText(name);
tv_email.setText(email);
tv_home.setText(address);
tv_phone.setText(phone);
btn_update.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
}
private void updateProfile() {
Intent intent = getActivity().getIntent();
String userUid = firebaseAuth.getCurrentUser().getUid();
String name = tv_name.getText().toString().trim();
String email = tv_email.getText().toString().trim();
String home = tv_home.getText().toString().trim();
String phone = tv_phone.getText().toString().trim();
Customer cust = new Customer(userUid, name, email, home, phone);
databaseReference.child("Customer").child(userUid).setValue(cust).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(getActivity(), "Update Successfully", Toast.LENGTH_SHORT).show();
getActivity().finish();
startActivity(new Intent(getActivity(), AccountFragment.class));
}
}
});
}
}
您无法使用 startActivity 打开片段,您需要实现一个回调,该回调会在您的用户个人资料更新成功时触发。在你的主机activity中创建如下界面:
public interface UpdateProfileSuccess{
void presentAccountFragment();
}
同时在 activity 中将其实例化为:
UpdateProfileSuccess callback = new UpdateProfileSuccess(){
@Override
void presentAccountFragment(){
getSupportFragmentManager().beginTransaction().replace(R.id.container,new
AccuontFragment()).commit();
}
}
在您的更新配置文件片段中创建一个构造函数,将该回调作为:
private MainActivity.UpdateProfileSuccess callback;
public UpdateProfileFragment(MainActivity.UpdateProfileSuccess callback){
this.callback = callback;
}
最后在您的请求的 onComplete 中将该回调消费为:
callback.presentAccountFragment();
这样您的 activity 将负责呈现 AccountFragment。