我正在尝试为我的Android应用程序创建一个自定义对话框。当我尝试从'ok'按钮方法访问字段时,我得到空指针异常。一旦onclick听众开火,似乎我的领域从存在中消失。有更好的方法吗?
addchars.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/scrollBox">
<RelativeLayout
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/addChars">
<AutoCompleteTextView android:layout_height="wrap_content" android:id="@+id/inCharName" android:hint="@string/txtCharacterNameBox" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</AutoCompleteTextView>
<TextView android:id="@+id/textView2" android:layout_height="wrap_content" android:gravity="center" android:layout_width="wrap_content" android:text="@string/txtHP" android:layout_below="@+id/inCharName" android:layout_alignParentLeft="true" android:layout_marginLeft="22dp"></TextView>
<TextView android:id="@+id/textView3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/txtTempHP" android:layout_below="@+id/inCharName" android:layout_alignParentRight="true" android:layout_marginRight="57dp"></TextView>
<EditText android:id="@+id/inHP" android:digits="-0123456789" android:minEms="3" android:gravity="center" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_below="@+id/textView2" android:layout_alignLeft="@+id/textView2">
</EditText>
<EditText android:id="@+id/inTempHP" android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_alignBaseline="@+id/inHP" android:layout_alignBottom="@+id/inHP" android:layout_alignLeft="@+id/textView3"></EditText>
<TextView android:id="@+id/TextView01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@+id/inHP" android:layout_alignLeft="@+id/inHP" android:text="@string/txtInitRoll"></TextView>
<TextView android:id="@+id/TextView02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@+id/inTempHP" android:layout_alignLeft="@+id/inTempHP" android:text="@string/txtInitBonus"></TextView>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="@+id/TextView01" android:layout_alignLeft="@+id/TextView01" android:id="@+id/inInitRoll"></EditText>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="@+id/TextView02" android:layout_alignLeft="@+id/TextView02" android:id="@+id/inInitBonus"></EditText>
<TextView android:id="@+id/TextView03" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/txtAC" android:layout_below="@+id/inInitRoll" android:layout_alignRight="@+id/textView2"></TextView>
<EditText android:id="@+id/inAC" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="@+id/TextView03" android:layout_alignLeft="@+id/TextView03"></EditText>
<TextView android:id="@+id/TextView04" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="@+id/inAC" android:layout_alignLeft="@+id/inInitBonus" android:text="@string/txtFortitude"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="@+id/TextView04" android:layout_alignLeft="@+id/TextView04" android:id="@+id/inFort"></EditText>
<TextView android:id="@+id/TextView06" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@+id/inFort" android:layout_alignLeft="@+id/inFort" android:text="@string/txtWill"></TextView>
<TextView android:id="@+id/TextView05" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@+id/inAC" android:layout_alignLeft="@+id/inAC" android:text="@string/txtRef"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="@+id/TextView05" android:layout_alignLeft="@+id/inInitRoll" android:id="@+id/inReflex"></EditText>
<EditText android:id="@+id/inWill" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_above="@+id/chkRoll" android:layout_alignLeft="@+id/TextView06"></EditText>
<CheckBox android:text="@string/txtRollBox" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/chkRoll" android:layout_below="@+id/inReflex" android:layout_alignLeft="@+id/TextView05" android:layout_alignParentRight="true"></CheckBox>
</RelativeLayout>
</ScrollView>
这是我的警报:
addBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//this block is to prep the layout for the alert dialog.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = inflater.inflate(R.layout.addchars, (ViewGroup) findViewById(R.id.scrollBox));
//taking this part out to try a better way.
new AlertDialog.Builder(initiative.this)
.setTitle("Add a Character")
.setView(addView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//ui controls
AutoCompleteTextView characterName = (AutoCompleteTextView) findViewById(R.id.inCharName);
EditText hp = (EditText) findViewById(R.id.inHP);
EditText tempHp = (EditText) findViewById(R.id.inTempHP);
EditText initRoll = (EditText) findViewById(R.id.inInitRoll);
EditText initBonus = (EditText) findViewById(R.id.inInitBonus);
EditText ac = (EditText) findViewById(R.id.inAC);
EditText fort = (EditText) findViewById(R.id.inFort);
EditText reflex = (EditText) findViewById(R.id.inReflex);
EditText will = (EditText) findViewById(R.id.inWill);
CheckBox rollInit = (CheckBox) findViewById(R.id.chkRoll);
//variables for various checks
entriesOk=false;
needToRoll=false;
if(characterName.getText().length() > 0)
{
holdCharacterName = characterName.getText().toString();
if(hp.getText().length() > 0)
{
holdHp = hp.getText().toString();
if(initBonus.getText().length() > 0)
{
holdInitBonus = initBonus.getText().toString();
if(ac.getText().length() > 0)
{
holdAc = ac.getText().toString();
if(fort.getText().length() > 0)
{
holdFort = fort.getText().toString();
if(reflex.getText().length() > 0)
{
holdReflex = reflex.getText().toString();
if(will.getText().length() > 0)
{
holdWill = will.getText().toString();
if(tempHp.getText().length() == 0)
{
holdTempHp = "0";
tempHp.setText("0");
}
else
{
holdTempHp = tempHp.getText().toString();
}
if(rollInit.isChecked())
{
entriesOk = true;
needToRoll = true;
}
else
{
if(initRoll.getText().length() > 0)
{
holdInitRoll = initRoll.getText().toString();
entriesOk = true;
}
}
}
}
}
}
}
}
}
btnLogic();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//do nothing
}
}).show();
答案 0 :(得分:1)
addView.findViewById(R.id.inHP)
代替findViewById(R.id.inHP)
答案 1 :(得分:0)
问题是你在onClick事件中调用addView.findViewById,onclick确实不会发生,直到有人点击它,我建议你制作addView类成员变量,以便它可以在内存中持久存在。这将解决你的问题。