我正在使用Android Annotations,我想实现一个简单的Fragment来测试它,所以我主要使用它来通过@ViewById将视图绑定到它们的ID。当我启动片段时,我通过注释进行绑定,然后使用@AfterViews执行其他操作。但是,我看到的只是一个空白屏幕。
流程活动开始 - >插入片段 - >空白屏幕
@EActivity(R.layout.activity_add_buddy)
public class AddBuddyActivityNew extends AppCompatActivity {
/**
* Simply performing frag transaction to place the add from contacts fragment
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.add_buddy_container, AddFromContactsFragment.newInstance(), ConstantValues.Tag.ADD_FROM_CONTACTS);
transaction.commit();
}
@EFragment(R.layout.fragment_add_from_contacts)
public class AddFromContactsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>,GetUsernamesFromPhoneNumbersListener,AddFrom ContactsActivityAdapter.AddBuddyInterface, AddBuddyListener, SearchView.OnQueryTextListener, UsernameExistsListener {
private static final String LOG_TAG = AddFromContactsFragment.class.getCanonicalName();
private static final int PERMISSIONS_REQUEST_CONTACTS = 0;
private SessionManager sessionManager;
private LBDatabaseHelper databaseHelper;
HashMap<String,String> numbersAndNames;
AddFromContactsActivityAdapter addFromContactsActivityAdapter;
private ContactsHandler handler;
private Timer timer;
/** UI References */
@ViewById(R.id.add_buddy_button) TextView addBuddyButton;
@ViewById(R.id.user_input_textview) TextView addBuddyUserInput;
@ViewById(R.id.user_input_error_display) TextView inputErrorDisplay;
@ViewById(R.id.user_input_layout) RelativeLayout userInputLayout;
@ViewById(R.id.finding_friends_layout) RelativeLayout findingFriendsLayout;
@ViewById(R.id.no_permission_layout) RelativeLayout noPermissionsLayout;
@ViewById(R.id.add_from_contacts_snackbar) CoordinatorLayout coordinatorLayout;
@ViewById(R.id.add_from_contacts_listview) ListView contactsList;
@ViewById(R.id.add_from_contacts_searchview) SearchView searchView;
@ViewById(R.id.add_from_contacts_progressbar) ProgressBar progressBar;
ProgressDialog progressDialog;
private String currentQuery; // User's current query
/**
* Grab a new instance of this fragment
*/
public static AddFromContactsFragment newInstance() {
return new AddFromContactsFragment();
}
/**
* Called after the views are set, place all view logic here
*/
@AfterViews
void afterAddFromContactsViews() {
Log.d(LOG_TAG, "after views start");
contactsList.setVisibility(View.GONE);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconifiedByDefault(false); // iconify the widget, don't expand it by default
searchView.setOnQueryTextListener(this);
searchView.setBackgroundColor(Color.WHITE);
contactsList.setAdapter(addFromContactsActivityAdapter);
handler = new ContactsHandler(findingFriendsLayout, addFromContactsActivityAdapter, getActivity(), contactsList); // The handler that will do all the hard stuff
if (sessionManager.getUserContactPermission()) {
checkForPermissions();
} else {
createAskForContactPermissionDialog();
}
Log.d(LOG_TAG, "after views end");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, "on create start");
sessionManager = SessionManager.getInstance(getActivity());
databaseHelper = LBDatabaseHelper.getInstance(getActivity());
timer = new Timer();
addFromContactsActivityAdapter = new AddFromContactsActivityAdapter(getActivity(), this, new ArrayList<Buddy>()); // Instantiate so handler doesn't get a null one
Log.d(LOG_TAG, "on create end");
}
onCreate()在片段中调用,但不在@AfterViews方法中调用。有人可以帮忙吗?谢谢。此外,新类别由AA生成。
答案 0 :(得分:0)
答案是棘手的newInstance()方法,我调用它来获取片段的实例。
/**
* Grab a new instance of this fragment
*/
public static AddFromContactsFragment newInstance() {
return new AddFromContactsFragment();
}
这需要修改为返回生成类型AddFromContactsFragment_的片段。下划线很重要,因为它是Android Annotations版本。同样(我已经知道),当创建Intent时,你必须使用Activity的'_'后缀版本来制作它们。