android.content.ActivityNotFoundException:无法找到明确的活动类{您是否在AndroidManifest.xml中声明了此活动?

时间:2019-09-22 13:44:18

标签: java android android-manifest android-fragmentactivity insert-update

我遇到这些错误

  

android.content.ActivityNotFoundException:无法找到显式的活动类{};您是否在AndroidManifest.xml中声明了此活动?           在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2005)           在android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)           在android.app.Activity.startActivityForResult(Activity.java:4586)           在androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)           在androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234)           在androidx.fragment.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:791)           在androidx.fragment.app.FragmentActivity $ HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:933)           在androidx.fragment.app.Fragment.startActivity(Fragment.java:1185)           在androidx.fragment.app.Fragment.startActivity(Fragment.java:1173)           在com.example.gerobokgo.AccountFragment.openUpdateProfile(AccountFragment.java:110)           在com.example.gerobokgo.AccountFragment.access $ 000(AccountFragment.java:29)           在com.example.gerobokgo.AccountFragment $ 2.onClick(AccountFragment.java:93)

这些error上有code

// 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);

    }
public class AccountFragment extends Fragment  
 //For Update Method

        btn_update.setOnClickListener( new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                openUpdateProfile();
            }

AndroidManifest

<application
        android:name=".Home"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".RegisterActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name=".LoginActivity"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".splashScreen"
            android:theme="@style/SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme" />
    </application>

UpdateProfileFragment.java

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));


                }
            }
        });
    }
}



2 个答案:

答案 0 :(得分:0)

根据您的代码 UpdateProfileFragment openUpdateProfile 方法内的活动。

使用前,您应该在 AndroidManifest 中声明UpdateProfileFragment活动。

编辑:

由于UpdateProfileFragment是片段,因此您通过startActivity()以错误的方式调用了片段。要从活动中调用fragemnt,请执行以下操作-

private void openUpdateProfile() {
Bundle bundle = new Bundle();

bundle.putString("name",tv_name.getText().toString().trim());
bundle.putString("email", tv_email.getText().toString().trim());
bundle.putString("home_address",tv_home.getText().toString().trim());
bundle.putString("telephone", tv_phone.getText().toString().trim());

UpdateProfileFragment updateFrag = new UpdateProfileFragment ();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();

updateFrag.setArguments(bundle);

transaction.add(R.id.fragmentContainer, updateFrag);
transaction.commit();

}

在UpdateProfileFragment中,您可以获取如下数据

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    String name = getArguments().getString("name");
    String email = getArguments().getString("email");
    String address= getArguments().getString("home_address");
    String telephone= getArguments().getString("telephone");
    // rest of your code here.
    ...
    ...

}

注意:如果UpdateProfileFragment是片段,则无需在清单文件中声明。

答案 1 :(得分:0)

根据您的说法,UpdateProfileFragment是一个片段。

因此,您无法通过fragment呼叫startActivity()。要从活动中呼叫fragemnt,您必须遵循以下步骤。

UpdateProfileFragment frag = new UpdateProfileFragment ();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragmentContainer,frag,"Test Fragment");
transaction.commit();

此外,您可以使用fragment

将数据传递到setArguments

示例:

private void openUpdateProfile() {

    Bundle bundle = new Bundle();

    bundle.putString("name",tv_name.getText().toString().trim());
    bundle.putString("email", tv_email.getText().toString().trim());
    bundle.putString("home_address",tv_home.getText().toString().trim());
    bundle.putString("telephone", tv_phone.getText().toString().trim());

    UpdateProfileFragment frag = new UpdateProfileFragment ();
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();

    frag.setArguments(bundle);

    transaction.add(R.id.fragmentContainer,frag,"Test Fragment");
    transaction.commit();

    }

在您的fragment中,您可以像下面这样获得data

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        .....
        String name = this.getArguments().getString("name");
        String email = this.getArguments().getString("email");
        String address= this.getArguments().getString("home_address");
        String telephone= this.getArguments().getString("telephone");
        // rest of your code here.
        ...
        ...
}

希望它对您有帮助。