5

I have a class and it extends from Fragment. Inside a class my editText is null and @AfterViews is not calling here is my code:

@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);
}

}

Please help me why editText is null thanks in advance!

nAkhmedov
  • 3,522
  • 4
  • 37
  • 72

1 Answers1

2

You have to let AndroidAnnotations handle the layout inflation and inject the view reference:

@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");
    }
}

An then use the generated class with _ prefix.

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • I've already done this but my xml is not being rendered. If i write "View rootView = inflater.inflate(R.layout.search, container, false); Log.i(TAG, "onCreateView"); return rootView;" inside onCreateVies it will display. – nAkhmedov Aug 09 '14 at 06:52
  • @nAkhmedov Clear generated classes, compile again. Use the newly generated `_` class. If it does not generate, there's an issue with setup. – S.D. Aug 09 '14 at 06:56
  • I called Search class method in the followinf in Main Activity – nAkhmedov Aug 09 '14 at 07:00
  • ((Search) pagerAdapter.getCurrFragment()).searchWebUrl(); – nAkhmedov Aug 09 '14 at 07:01
  • Should "@EFragment(R.layout.my_fragment_layout)" be rendered layout?layout is only being displayed if i write inflater.inflate(R.layout.search, container, false) inside onCreateView. – nAkhmedov Aug 09 '14 at 07:17
  • if i use _ like ((Search_) pagerAdapter.getCurrFragment()).searchWebUrl(); class cast exception is appeared. cannot cast Search to Search_ – nAkhmedov Aug 09 '14 at 07:29
  • Did you ever resolve this? I'm having the exact same issue – Brandon Mar 05 '16 at 16:42