android找不到edittext,androidannotations @AfterViews没有调用,editText为null

在Android应用开发中,遇到EditText为null的问题,原因可能是未正确使用AndroidAnnotations库。解决方案是在@EFragment注解中指定布局文件,并让AndroidAnnotations处理视图注入。参考示例代码展示了如何修正此问题,通过@ViewById注解初始化EditText并在@AfterViews方法中进行操作。
摘要由CSDN通过智能技术生成

我有一个类,它从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 urlList = new ArrayList();

@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!

解决方法:

你必须让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");

}

}

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

标签:android,xml,android-annotations

来源: https://codeday.me/bug/20190703/1363606.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值