TheDevMan
TheDevMan

Reputation: 5954

Calling activity intent in Fragment in Android

I am trying to call a activity class from a fragment. I have implemented OnClickListener and it's method too.

    if(v.getId() == sortbutton.getId())
    {
        //callSorting();
        Intent intent = new Intent(getActivity(), EMB_SortList.class);
        //startActivityForResult(intent, REQUEST_CODE_LEVEL);
        startActivity(intent);
    }

Here is my java file:

public class ListViewFragment extends Fragment implements OnClickListener 
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
{
    if (rootView != null) 
    {
        ViewGroup parent = (ViewGroup) rootView.getParent();

        if (parent != null)
            parent.removeView(rootView);
    }
    try 
    {
        rootView = inflater.inflate(R.layout.list, container, false);
    } 
    catch (InflateException e) 
    {

    }

    sortbutton = (TextView) rootView.findViewById(R.id.sortText);
    sortbutton.setOnClickListener(this);        

    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
}

but I am getting a strange error.

03-16 19:40:20.620: E/AndroidRuntime(1346): FATAL EXCEPTION: main
03-16 19:40:20.620: E/AndroidRuntime(1346): android.app.SuperNotCalledException: Activity {com.ylg.Link/com.ylg.Link.SortList} did not call through to super.onCreate()
03-16 19:40:20.620: E/AndroidRuntime(1346):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at android.app.ActivityThread.access$700(ActivityThread.java:159)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at android.os.Looper.loop(Looper.java:176)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at android.app.ActivityThread.main(ActivityThread.java:5419)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at java.lang.reflect.Method.invoke(Method.java:525)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
03-16 19:40:20.620: E/AndroidRuntime(1346):     at dalvik.system.NativeStart.main(Native Method)

I am not sure what is wrong here? Can somebody help me fix this?

Thanks!

Upvotes: 0

Views: 725

Answers (4)

Xjasz
Xjasz

Reputation: 1248

It obvious that this class isn't using onCreate the right way

EMB_SortList.class

You need to quadruple check it

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

In fact you need to replace it with one of our answers and make sure you're extending Activity.

---------------------> Dev i think you put this code in your fragment.

@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
}

If you did you need to remove it.

That code doesn't go in your fragment it goes in the activity you want to create.

Upvotes: 2

Vigneshwaran Murugesan
Vigneshwaran Murugesan

Reputation: 757

In the EMB_Sortlist.java Class add

 public void onCreate(Bundle savedInstanceState) {
  //You should override the below method
  super.onCreate(savedInstanceState);
  //Do your work here
}

Upvotes: 1

Fahim
Fahim

Reputation: 12358

android.app.SuperNotCalledException: Activity {com.ylg.Link/com.ylg.Link.SortList} did not call through to super.onCreate()

It seems u have forgotted to call super.onCreate() method in onCreate() method of the activity

Upvotes: 1

Mohammad Arman
Mohammad Arman

Reputation: 7065

Add this.getActivity() in the place of getActivity()

if(v.getId() == sortbutton.getId())
    {
        //callSorting();
        Intent intent = new Intent(this.getActivity(), EMB_SortList.class);
        //startActivityForResult(intent, REQUEST_CODE_LEVEL);
        startActivity(intent);
    }

Upvotes: 1

Related Questions