我遇到了問題findViewById()
。每當我在這段代碼中使用它,它都會顯示我錯誤。有時候,Android的工作室建議我做這樣的TextView DateView = null;
和無法使用findViewById
DateView = (TextView)DateView.findViewById(R.id.DateView);
但也給出了一個錯誤。有人可以幫我解決這個問題。
public class OperationFragment extends Fragment {
public OperationFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_operation, container, false);
}
public void onCreate (Bundle savedInstabceState){
super.onCreate(savedInstabceState);
Calendar c = Calendar.getInstance();
SimpleDateFormat dd = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat dt = new SimpleDateFormat("HH:mm a");
String formattedDate = dd.format(c.getTime());
String formattedTime = dt.format(c.getTime());
TextView DateView;
TextView TimeView;
System.out.println(formattedTime);
System.out.println(formattedDate);
DateView = (TextView)findViewById(R.id.DateView);
DateView.setText(formattedDate);
TimeView = (TextView)findViewById(R.id.TimeView);
TimeView.setText(formattedTime);
}
}
你必須在onCreateView() –