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.
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>"));
似乎是您的es.uam.eps.dadm.SESSION软件包文件夹中没有SESSION.java文件,或者您在清单文件中错过了它
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.