我可以在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;
}
答案 0 :(得分:0)
它不起作用,因为Fragment
没有提供getSupportActionBar()
方法,它是托管AppCompatActivity
(不推荐使用ActionBarActivity)的方法片段。如果使用getActivity().getSupportActionBar()
onCreateView
获取对它的引用
答案 1 :(得分:0)
非常感谢。我懂了
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);