请教关于View的android:onClick的问题

I Du I Du 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在下面添加android:onClick总是出错
<AutoCompleteTextView
android:id="@+id/txtsex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:text=""
android:onClick="setSex" />

错误信息:

java.lang.NullPointerException   
at android.widget.AutoCompleteTextView$PassThroughClickListener.access$302   
at android.widget.AutoCompleteTextView.setOnClickListener   
at android.view.View.<init>   
at android.widget.TextView.<init>   
at android.widget.EditText.<init>   
at android.widget.AutoCompleteTextView.<init>   
at android.widget.AutoCompleteTextView.<init>   
at java.lang.reflect.Constructor.newInstance   
at android.view.LayoutInflater.createView   
at android.view.LayoutInflater.onCreateView   
at android.view.LayoutInflater.createViewFromTag   
at android.view.LayoutInflater.rInflate_Original   
at android.view.LayoutInflater_Delegate.rInflate   
at android.view.LayoutInflater.rInflate   
at android.view.LayoutInflater.rInflate_Original   
at android.view.LayoutInflater_Delegate.rInflate   
at android.view.LayoutInflater.rInflate   
at android.view.LayoutInflater.rInflate_Original   
at android.view.LayoutInflater_Delegate.rInflate   
at android.view.LayoutInflater.rInflate   
at android.view.LayoutInflater.inflate   
at android.view.LayoutInflater.inflate

对应的java页面的函数
public void setSex(View view)
{
final EditText txtsex = (EditText)this.findViewById(R.id.txtsex);
String[] strSex = getResources().getStringArray(R.array.sex);
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
builder.setTitle("选择性别");
builder.setCancelable(false);
builder.setSingleChoiceItems(strSex, 0, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
index = which;
}
});
builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
txtsex.setText(getResources().getStringArray(R.array.sex)[index]);
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
txtsex.setText("");
}
});
builder.create().show();
}
...全文
评论 10
收藏 已收藏
分享
返回顶部
I Du
Android 80,489 成员 91,371 内容