androidannotations @AfterViews没有调用,editText为null

时间:2014-08-09 06:30:10

标签: android xml android-annotations

我有一个类,它从Fragment扩展。在一个类中,我的editText为null,而@AfterViews不在这里调用是我的代码:

@EFragment
public class Search extends Fragment {

private final String TAG = Search.class.getCanonicalName();
private static ProgressDialog progressDialog;
private Activity activity;
private ArrayList<String> urlList = new ArrayList<String>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.search, container, false);
    Log.i(TAG, "onCreateView");
    return rootView;
}
@ViewById
RadioButton firstRadio;

@ViewById
RadioButton secondRadio;

@ViewById(R.id.editText1)
EditText editText1;

@AfterViews
void afterViews() {
    int a = 1;
    a++;
    Log.i(TAG, editText1 + "AfterViews");
    Log.i(TAG, a + "aaaaaaaa");
}

private void extractUrlsFromText(String s) {
    Matcher matcher = Patterns.WEB_URL.matcher(s);
    while (matcher.find())
        urlList.add(matcher.group());
}

public void searchWebUrl() {
    Log.i(TAG, editText1 + "searchWebUrl");
    String text = editText1.getText().toString();
    if (!TextUtils.isEmpty(text))
        extractUrlsFromText(text);
        parseWithUrls(urlList);
        ((Main) getActivity()).switchToFragmentUrlList(urlList);
}

}

请提前帮助我为什么editText为null!

1 个答案:

答案 0 :(得分:2)

你必须让AndroidAnnotations处理布局通胀并注入视图参考:

@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {

    @ViewById(R.id.my_text_view) // must exist in layout, must be of correct type
    TextView mTextView;

    @AfterViews
    void initViews() {
        mTextView.setText("Hello World");
    }
}

然后使用带有_前缀的生成类。