I have this error when wanting to go from an Activity to a Fragment

0

He tells me that I should add the fragment to Manifest but he will not let me ...

This is the error in the logcat

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.matias.finalcode, PID: 9941
                  android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.matias.finalcode/com.example.matias.finalcode.Fragments.InicioFragment}; have you declared this activity in your AndroidManifest.xml?
                      at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761)
                      at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
                      at android.app.Activity.startActivityForResult(Activity.java:3782)
                      at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
                      at android.app.Activity.startActivityForResult(Activity.java:3743)
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:732)
                      at android.app.Activity.startActivity(Activity.java:4053)
                      at android.app.Activity.startActivity(Activity.java:4021)
                      at com.example.matias.finalcode.Bancos.Banco1$2.onClick(Banco1.java:99)
                      at android.view.View.performClick(View.java:4757)
                      at android.view.View$PerformClick.run(View.java:19757)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5258)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
I/Process: Sending signal. PID: 9941 SIG: 9
Application terminated.

And this is the button in the Activity that should take me to the fragment

btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Banco1.this, InicioFragment.class);
            startActivity(intent);
            finish();
            }
        });

This is my MANIFEST

<application
        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=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.matias.finalcode.InicioActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".SignupActivity" />
        <activity android:name=".ResetPasswordActivity" />      
        <activity android:name=".Bancos.Banco1">
        </activity>
    </application>
    
asked by Matías Nicolás Núñez Rivas 01.04.2018 в 17:59
source

1 answer

0

The problem is that this does not start a Fragment, a Fragment is a component of an Activity and needs it to start. when you do startActivity(intent); Android expects you to pass an Activity and not a Fragment. That's why I give you the error saying that you need to declare it in the Manifest, since you can not find it.

To perform Fragments transactions in your Activity (such as adding, removing or replacing a Fragment), you must use the FragmentTransaction APIs. You can get an instance of FragmentTransaction of your Activity like this:

FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

Then you can add a Fragment using the add () method and specifying the Fragment to be added and the view in which it will be inserted. For example:

ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit();

An example of a Fragment class would be the following

public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.example_fragment, container, false);
}

}

I recommend that you take a look at the Android development guide that you have in Spanish.

Fragments | Android developers

    
answered by 01.04.2018 / 21:35
source