嗨,我是android新手,如果有人能够帮助我解决此问题,将不胜感激。现在我正在尝试回到上一个视图或根视图,有人知道如何从静态上下文返回到上一个视图(我不确定静态上下文是正确的表达方式,如果有的话可以随意纠正我)我错了)?
我已经尝试创建FragmentManager的原因是因为我要创建的应用程序使用了Fragment。
FragmentManager fragmentManager =
((FragmentActivity)mContext).getSupportFragmentManager();
fragmentManager.popBackStack();
`
public class SomeView1 extends View implements View.OnTouchListener {
public SomeView1(Context c) {
super(c);
mContext = c;
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
}
public SomeView1(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public void foodPortionCal() {
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Output");
builder.setMessage("Message here");
builder.setCancelable(true);
builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sqLiteHelper.insertDataFood(GlobalVariable.mealTypeF, String.format("%.2f", totalProteinPer) + '%', String.format("%.2f", totalGrainsPer) + '%', String.format("%.2f", totalVegetablePer) + '%', String.format("%.2f", totalFruitPer) + '%', String.format("%.2f", totalDairyPer) + '%', GlobalVariable.dataF);
Toast.makeText(mContext, "Added", Toast.LENGTH_SHORT).show();
//this is where I want to set the feature to remove the view/static context?
FragmentManager fragmentManager = ((FragmentActivity)mContext).getSupportFragmentManager();
fragmentManager.popBackStack();
dialog.dismiss();
}
});
builder.show();
}
}
//Before going the code will move to someview1 we will be in a fragment class called FoodFragment and this is how it look like
public class Food_Cal extends Fragment {
private void mealTypeSelection()
{
//this is how I call the come view activity
getActivity().setContentView(new SomeView1(view.getContext()));
}
}
//这是此应用程序构建方式的链接Android: Free Cropping of Image 唯一最大的不同是,我的应用使用片段,而引用没有的代码
想要获得的结果是要么将其移动到带有片段的下一个视图,要么一直返回到带有片段的根视图。我得到的结果是应用程序继续崩溃并且无法返回到根视图,然后应用程序崩溃这是我得到的错误消息
2019-04-27 00:35:36.599 624-624/apd.bii.diabetesappv1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: apd.bii.diabetesappv1, PID: 624
java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.support.v4.app.FragmentActivity
at apd.bii.diabetesappv1.FoodCalculation.SomeView1$1.onClick(SomeView1.java:437)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:173)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6894)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
答案 0 :(得分:0)
这是一种用于将片段添加到FragmentManger
中以便使其可见的简单方法。您需要做的就是传递要添加到屏幕上的片段。
public void addScreen(Fragment fragment) {
getSupportManager()
.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
screensOnTheStack++;
}
当您想将该片段从堆栈中弹出并转到父视图时,是
while (screensOnTheStack > 0) {
fragments.popBackStackImmediate();
}
或者只是通过定义片段TAG来确定root(parent) fragment
,例如
private static final String BACK_STACK_ROOT_TAG = "root_fragment";
然后
// Pop off everything up to and including the current tab
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStack(BACK_STACK_ROOT_TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
// Add the new tab fragment
fragmentManager.beginTransaction()
.replace(R.id.container, TabFragment.newInstance())
.addToBackStack(BACK_STACK_ROOT_TAG)
.commit();
现在,对于您所说的部分
想要获得的结果是要么将其移至带有片段的下一个视图,要么一直返回至具有片段的根视图。
您可以根据自己的条件来决定何时要转到想要的片段,或者转到父视图。