[英]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 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 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. 因此,当我尝试加载SESSION类时,我不知道为什么以另一种方式引发ClassNotFoundException。 Maybe the declaration of
intent
is wrong? 也许
intent
声明是错误的? Thanks in advance to any help. 在此先感谢您的帮助。
I don't know why the other way rises an ClassNotFoundException
我不知道为什么相反会引发ClassNotFoundException
es.uam.eps.dadm.SESSION
is Action name which you have added during Activity declaration in AndroidManifest.xml
. es.uam.eps.dadm.SESSION
是您在AndroidManifest.xml
中的Activity声明期间添加的动作名称。
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 :
但是从Fragment尝试使用操作String而不是带有包名的类名加载类:
Use class name for loading class using loadClass
: 使用类名使用
loadClass
加载类:
Intent intent = new Intent(getActivity(), getActivity().getClassLoader().
loadClass("es.uam.eps.dadm.<Class_Name>"));
似乎是您的es.uam.eps.dadm.SESSION软件包文件夹中没有SESSION.java文件,或者您在清单文件中错过了它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.