怎么做" getSupportActionBar()。setDisplayShowHomeEnabled" at onCreateView()

时间:2015-06-17 04:43:35

标签: android

我可以在onCreate()上完成。但我不能在onCreateView()做。我在---

发生错误
myDB = new DatabaseHelper(this);        
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

代码:

public class Register extends Fragment {

DatabaseHelper myDB;
EditText username, email, password;
Button save;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_register, container, false);
    myDB = new DatabaseHelper(this);
    username = (EditText) v.findViewById(R.id.etRegisterUserName);
    email = (EditText) v.findViewById(R.id.etRegisterEmail);
    password = (EditText) v.findViewById(R.id.etRegisterPassword);
    save = (Button) v.findViewById(R.id.bSave);        
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    return v;
}

2 个答案:

答案 0 :(得分:0)

它不起作用,因为Fragment没有提供getSupportActionBar()方法,它是托管AppCompatActivity(不推荐使用ActionBarActivity)的方法片段。如果使用getActivity().getSupportActionBar()

,请使用onCreateView获取对它的引用

答案 1 :(得分:0)

非常感谢。我懂了

((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);