Android 如何从类中调用片段
我只想调用这个类中的片段。 实际上,我的应用程序中有很多片段,我不得不一次又一次地调用它们。 所以我想创建一个类和一个函数来加载一个片段,这样每当我需要调用一个片段时,我都可以使用这个类的函数。 但我无法在此处获取getSupportFragmentManager()。 我尝试将类扩展为片段,但随后它会生成null异常。 还通过使用Appcompactactivity进行扩展,并使用getSupportFragmentManager()但也表示活动已销毁,从而给出错误。 有人有办法从一个简单的类中调用一个片段吗Android 如何从类中调用片段,android,android-fragments,fragment,android-fragmentactivity,fragmentmanager,Android,Android Fragments,Fragment,Android Fragmentactivity,Fragmentmanager,我只想调用这个类中的片段。 实际上,我的应用程序中有很多片段,我不得不一次又一次地调用它们。 所以我想创建一个类和一个函数来加载一个片段,这样每当我需要调用一个片段时,我都可以使用这个类的函数。 但我无法在此处获取getSupportFragmentManager()。 我尝试将类扩展为片段,但随后它会生成null异常。 还通过使用Appcompactactivity进行扩展,并使用getSupportFragmentManager()但也表示活动已销毁,从而给出错误。 有人有办法从一个简单的类
public class CompletedandPendingScreensLoader {
public void pendingscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("pending","pen");
frag.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void completedscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("completed","yes");
frag.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void simpleScreenLoader( int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
FragmentTransaction transaction = getFragmentManager.beginTransaction();
transaction.replace(R.id.frame, new LessonTwo()).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit();
}
}
类必须扩展AppCompatActivity类才能调用此函数,扩展类时,还必须重写活动的onCreate()方法,还必须在onCreate()方法中设置活动的布局
类必须扩展AppCompatActivity类才能调用此函数,扩展类时,还必须重写活动的onCreate()方法,还必须在onCreate()方法中设置活动的布局
我相信您会在某个时候从某个活动或片段调用这个类,如果是这样,那么使用修改过的构造函数它将打开片段。否则,如果没有活动或片段中FragmentManager的参考,则无法执行此操作
public class CompletedandPendingScreensLoader {
private FragmentManager fragmentManager = null;
//when ever you start your class just start using this constructor
CompletedandPendingScreensLoader(FragmentManager fragmentManager){
this.fragmentManager = fragmentManager
}
public void pendingscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("pending","pen");
frag.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void completedscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("completed","yes");
frag.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void simpleScreenLoader( int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, new LessonTwo()).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit();
}
}
我相信您会在某个时候从某个活动或片段调用这个类,如果是这样,那么使用修改过的构造函数它将打开片段。否则,如果没有活动或片段中FragmentManager的参考,则无法执行此操作
public class CompletedandPendingScreensLoader {
private FragmentManager fragmentManager = null;
//when ever you start your class just start using this constructor
CompletedandPendingScreensLoader(FragmentManager fragmentManager){
this.fragmentManager = fragmentManager
}
public void pendingscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("pending","pen");
frag.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void completedscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("completed","yes");
frag.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void simpleScreenLoader( int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, new LessonTwo()).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit();
}
}
但是我不需要那个。。。我只想使用这个类的函数而不让它成为活动。然后你可以在你的类中创建一个AppCompatActivity类的实例,并在你的类的构造函数中初始化它,然后从AppCompatActivity的对象调用函数,你可以用代码示例解释它。。。因为我不明白你的意思我明白了。。。但是,在片段中创建appcompatactivity的对象时,要传递哪些对象参数呢?CompletedandPendingScreensLoader screensLoader=新的CompletedandPendingScreensLoader(---在此传递的内容--);如果您在活动过程中创建此类的对象“this”,如果您在片段过程中创建此类的对象“getActivty()”。但是我不需要这个。。。我只想使用这个类的函数而不让它成为活动。然后你可以在你的类中创建一个AppCompatActivity类的实例,并在你的类的构造函数中初始化它,然后从AppCompatActivity的对象调用函数,你可以用代码示例解释它。。。因为我不明白你的意思我明白了。。。但是,在片段中创建appcompatactivity的对象时,要传递哪些对象参数呢?CompletedandPendingScreensLoader screensLoader=新的CompletedandPendingScreensLoader(---在此传递的内容--);如果在活动过程中生成此类的对象“this”,如果在片段过程中生成此类的对象“getActivty()”。