esseara
esseara

Reputation: 880

Launch Activity from Fragment throws ClassNotFoundException

I know it's a long shot but I tried a lot of solutions and none worked. I'm trying to launch an activity from a fragment when a button is tapped.

Fragment.java

public class Lev1 extends Fragment implements OnClickListener {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        View v = inflater.inflate(R.layout.lev1, null);     
        Button button1= (Button) v.findViewById(R.id.level1);

        button1.setOnClickListener(this);           

        return v;

    }

    @Override
    public void onClick(View v) {

        try {
            Intent intent = new Intent(getActivity(), getActivity().getClassLoader().loadClass("es.uam.eps.dadm.SESSION"));
            startActivity(intent);
        }
        catch(ClassNotFoundException e) {
            //to handle carefully
            Toast.makeText(context, "Class not found",
                     Toast.LENGTH_SHORT).show();
        }
    } 

Fragment.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout        
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/level1"            
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_margin="8dp"
        android:background="@drawable/fr1"
         />        
</LinearLayout>

I suppose that is not a package problem, because if I use an activity and not a fragment the following works well:

Button button1= (Button)findViewById(R.id.level1);

button1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
        startActivity(new Intent("es.uam.eps.dadm.SESSION"));
}

So I don't know why the other way rises an ClassNotFoundException when I try to load my SESSION class. Maybe the declaration of intent is wrong? Thanks in advance to any help.

Upvotes: 0

Views: 161

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

I don't know why the other way rises an ClassNotFoundException

es.uam.eps.dadm.SESSION is Action name which you have added during Activity declaration in AndroidManifest.xml.

From Activity on Button click using action to prepare Intent for starting Activity. but from Fragment trying to load a class using action String instead of class name with package name :

Use class name for loading class using loadClass :

Intent intent = new Intent(getActivity(), getActivity().getClassLoader().
                                      loadClass("es.uam.eps.dadm.<Class_Name>"));

Upvotes: 2

Fahim
Fahim

Reputation: 12358

It seems to be you dont have SESSION.java file in your es.uam.eps.dadm.SESSION package folder or you have missed it in manifest file

Upvotes: 1

Related Questions