Search code examples
androidandroid-listfragment

Listfragment can not be cast to android.app.activity


im trying to use a class extends for Listfragment but i cant open it.

I have 2 activitys, firts is the main and extends ActionBarActivity to get action bar to older APIs:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;


    public class MainActivity extends  ActionBarActivity {

    //......

    public void makeAndEvent (View v){

    Intent intent = new Intent(getBaseContext(), Events.class);
            startActivity(intent);
        }
    }

The seconds is the conflic here:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class Events extends ListFragment {



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_events, container, false);
        return rootView;
    }
}

But when the app try lo launch the second activity, Events, i get this error in log:

08-02 18:59:38.279: E/AndroidRuntime(12284): FATAL EXCEPTION: main
08-02 18:59:38.279: E/AndroidRuntime(12284): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.myapp/com.app.myapp.Events}: java.lang.ClassCastException: com.app.myapp.Events cannot be cast to android.app.Activity
08-02 18:59:38.279: E/AndroidRuntime(12284):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1998)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at android.os.Looper.loop(Looper.java:137)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at android.app.ActivityThread.main(ActivityThread.java:4929)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at java.lang.reflect.Method.invokeNative(Native Method)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at java.lang.reflect.Method.invoke(Method.java:511)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at dalvik.system.NativeStart.main(Native Method)
08-02 18:59:38.279: E/AndroidRuntime(12284): Caused by: java.lang.ClassCastException: com.app.myapp.Events cannot be cast to android.app.Activity
08-02 18:59:38.279: E/AndroidRuntime(12284):    at android.app.Instrumentation.newActivity(Instrumentation.java:1056)
08-02 18:59:38.279: E/AndroidRuntime(12284):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1989)

Some one can help me pls?


Solution

  • Fragments can't be launched. they are to be included in an Activity. You can either use ListActivity or add you ListFragment on a FragmentActivity.