Neelam
Neelam

Reputation: 25

Fragment class showing error in intent's startactivity method

-30 12:14:06.020 24369-24369/com.sourcey.materialloginexample E/AndroidRuntime: FATAL EXCEPTION: main Process: com.sourcey.materialloginexample, PID: 24369 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.sourcey.materialloginexample/com.surbhi.oopscourier.activities.TwoFragment}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1788) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512) at android.app.Activity.startActivityForResult(Activity.java:3810) at android.app.Activity.startActivityForResult(Activity.java:3761) at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:813) at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:871) at android.support.v4.app.Fragment.startActivity(Fragment.java:916) at com.surbhi.oopscourier.activities.OneFragment$1.onClick(OneFragment.java:26) at android.view.View.performClick(View.java:4848) at android.view.View$PerformClick.run(View.java:20262) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5637) 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:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

public class OneFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_one, container, false);
            Button next=(Button)view.findViewById(R.id.nextonefrag);
            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i=new Intent(getActivity(),TwoFragment.class);
                    startActivity(i);
                }
            });
            return view;
        }
    }

xml :

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#ffffff"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:background="@drawable/cell"
        android:layout_marginTop="150dp"
        android:orientation="vertical">


        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:background="@color/colorPrimary"
            android:id="@+id/spinner" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Wait and Return"
            android:textColor="#000000"
            android:id="@+id/checkBox" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Express"
            android:textColor="#000000"
            android:id="@+id/checkBox2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next"
            android:layout_marginBottom="10dp"
            android:background="@color/colorPrimary"
            android:id="@+id/nextonefrag"
            android:layout_marginRight="20dp"
            android:layout_gravity="right" />
    </LinearLayout>
</RelativeLayout>

When I click on button inside fragment the application unfortunately stopped.And showing error in start Activity method indicating to declare the fragment activity in manifest file.I tried to declare the fragment activity inside the manifest but it can not declared there.

Upvotes: 1

Views: 507

Answers (3)

Dominic D&#39;Souza
Dominic D&#39;Souza

Reputation: 981

is Two fragment a Activity or a Fragment?

1.Only Activities can be declared in Manifest. 2.If TwoFragment is a Fragment you would need a different approach to switch between fragments..

Upvotes: 0

Vishnu M Menon
Vishnu M Menon

Reputation: 1469

If you need to replace the First fragment to Second Fragment on button click use Fragment Transaction. The error happening to your program is because you are trying to load a fragment like an activity. So replace the code with Fragment Transaction.

Upvotes: 0

saty
saty

Reputation: 66

If your TwoFragment.java extends Fragment and you have added fragment dynamically, you can replace your previous fragment with Fragment TwoFragment like this:

Fragment fragment = new TwoFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame, fragment);
        fragmentTransaction.commit();

Activity and Fragment are two different things:

An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html

Whereas a Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities). A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. http://developer.android.com/guide/components/fragments.html

Upvotes: 0

Related Questions