我正在尝试将FloatingActionButton添加到FloatingActionMenu 但是在启动FloatingActionButton期间无法获取上下文 所有方法都返回null
这是我的片段调用
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.getbase.floatingactionbutton.FloatingActionButton;
import com.getbase.floatingactionbutton.FloatingActionsMenu;
import demo.com.example.syed.test.R;
public class stdDetails extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FloatingActionMenu detailsfabmenu=(FloatingActionMenu)Container.findViewById(R,id,std_Main_Detials_fab_menu);
FoatingActionButton(getContext());
detailsfabmenu.addButton(changename);
return inflater.inflate(R.layout.stddetailsfragment,container,false);
}}
码
FloatingActionButton changename=new FloatingActionButton(getContext());
返回null值 我甚至尝试过基于avilabel答案的选项
FloatingActionButton changename=new FloatingActionButton(container.getContext());
FloatingActionButton changename=new FloatingActionButton(getActivity().getApplicationContext());
FloatingActionButton changename=new FloatingActionButton(this.getContext());
FloatingActionButton changename=new FloatingActionButton(this.getActivity().getApplicationContext());
这是这里提供的所有答案 Using context in a fragment
答案 0 :(得分:0)
这是初始化FAB的方法:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabBtn);
此处fabBtn
是布局XML文件中FAB的ID。
使用以下代码行:
FloatingActionButton changename=new FloatingActionButton(getBaseContext());
答案 1 :(得分:0)
您应该从MainActivity调用您的浮动操作按钮
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabBtn);
fab.show().
如果要显示特定片段的fab,可以使用片段标记:
Fragment fragmentA = new FragmentA();
getFragmentManager().beginTransaction()
.replace(R.id.MainFrameLayout,fragmentA,"YOUR_TARGET_FRAGMENT_TAG")
.addToBackStack("YOUR_SOURCE_FRAGMENT_TAG").commit();
然后检查您是否在Mainactivity中有所需的片段:
if(currentFrag.equals("YOUR_TARGET_FRAGMENT_TAG")){
fab.show();
}
else
fab.hide();