我遇到了片段问题.我想在我的应用程序中添加一个抽屉菜单,而不是使用片段.我有这段代码,当我把片段“ findviewbyid”放不下时.有人可以帮我吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_contact);
final EditText nome = (EditText) findViewById(R.id.nome);
final EditText citta = (EditText) findViewById(R.id.citta);
final EditText numero = (EditText) findViewById(R.id.numero);
final EditText email = (EditText) findViewById(R.id.email);
final EditText oggetto = (EditText) findViewById(R.id.oggetto);
final EditText messaggio = (EditText) findViewById(R.id.messaggio);
Button email = (Button) findViewById(R.id.post_message);
email.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nome.getText().toString();
String citta = citta.getText().toString();
String numero = numero.getText().toString();
String email = mail.getText().toString();
String oggetto = oggetto.getText().toString();
String messaggio = messaggio.getText().toString();
if (TextUtils.isEmpty(nome)){
nome.setError("Inserisci il Tuo Nome");
nome.requestFocus();
return;
}
谢谢你,抱歉我的英语
解决方法:
片段不是活动,这意味着您不能使用活动方法或其生命周期.这意味着您无法在Fragment的onCreate()方法中找到视图,也不能在其上使用findViewById.
这是Android生命周期https://*.com/a/36340059/4467208
并据此调整代码,将findViews移到onCreateView()中,如下所示
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentLayout, container, false);
nome = (EditText) view.findViewById(R.id.nome);
return view;
}
等等.