Parth Sharma
Parth Sharma

Reputation: 347

Fragment java.lang.ClassCastException Error

How to cast Fragment activity?

Actually I am trying to Open through button on Activity to Fragment Activity

Android MainFest :

<activity android:name="parth.any.ttb.Fragment_1"/>

LogCat:

12-04 15:22:43.132: E/AndroidRuntime(25873): FATAL EXCEPTION: main
12-04 15:22:43.132: E/AndroidRuntime(25873): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{parth.any.ttb/parth.any.ttb.Fragment_1}: java.lang.ClassCastException: parth.any.ttb.Fragment_1
12-04 15:22:43.132: E/AndroidRuntime(25873):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at android.os.Looper.loop(Looper.java:130)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at android.app.ActivityThread.main(ActivityThread.java:3687)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at java.lang.reflect.Method.invokeNative(Native Method)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at java.lang.reflect.Method.invoke(Method.java:507)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at dalvik.system.NativeStart.main(Native Method)
12-04 15:22:43.132: E/AndroidRuntime(25873): Caused by: java.lang.ClassCastException: parth.any.ttb.Fragment_1
12-04 15:22:43.132: E/AndroidRuntime(25873):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-04 15:22:43.132: E/AndroidRuntime(25873):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565

)

Here is Both the class codes: Edit: NewTransaction.java

startActivity(new Intent(getApplicationContext(), Fragment_1.class));

Fragment_1.java

public class Fragment_1 extends Fragment implements OnClickListener {
        TextView Home;
        Animation animMove;

        // slide menu items
        private String[] spinMenuTitles;
        private TypedArray spinMenuIcons;
        private Button Add_New;     
        private ArrayList<TypeCustomSpinnerItem> spinDropItems;
        private TypeSpinnerListAdapter adapter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_1, null);
        Home = (TextView)view.findViewById(R.id.home);
        Add_New = (Button)view.findViewById(R.id.add_new);
        Add_New.setOnClickListener(this);

        animMove = AnimationUtils.loadAnimation(getActivity(),R.anim.move);
        Home.startAnimation(animMove);

                // load type spin menu items
                spinMenuTitles = getResources().getStringArray(R.array.type_spinner_items);

                // type spinner icons from resources
                spinMenuIcons = getResources().obtainTypedArray(R.array.type_spinner_icons);

                spinDropItems = new ArrayList<TypeCustomSpinnerItem>();

                // adding type spinner items to array
                int i;
                for(i=0;i<=9;i++)
                {
                spinDropItems.add(new TypeCustomSpinnerItem(spinMenuTitles[i], spinMenuIcons.getResourceId(i, -1)));
                }
                // Recycle the typed array
                spinMenuIcons.recycle();

                Spinner mySpinner = (Spinner)view.findViewById(R.id.spinner1);
                adapter = new TypeSpinnerListAdapter(getActivity(),spinDropItems);
                mySpinner.setAdapter(adapter);

        return view;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.add_new:
            Intent addintent = new Intent(getActivity() , AddNewTransaction.class);
            startActivity(addintent);
            break;

    }
    }
}

Upvotes: 0

Views: 1271

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

Frament_1 is a fragment

 public class Fragment_1 extends Fragment implements OnClickListener {

You cannot use startActivity for fragment. Fragment is hosted by a activity.

In your activity layout xml have the below.

programmatically add the fragment to an existing ViewGroup.

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" /> 

FrameLayout is the container. You are adding fragment to the container.

In Activity

 FragmentTransaction fragTran = getSupportFragmentManager().beginTransaction();
 Fragment_1  fragment1 = new Fragment_1()
 fragTran.replace(R.id.content_frame, fragment1);   

You can also have the below in xml. Declare the fragment inside the activity's layout file.

 <fragment android:name="com.example.fragment.Fragment_1"
 android:id="@+id/list"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />    

For more info

http://developer.android.com/guide/components/fragments.html

Upvotes: 0

Melquiades
Melquiades

Reputation: 8598

You cannot start fragment as an activity, they need to be added to activities.

Read more in the docs here.

Also, see this

Upvotes: 2

Related Questions