这是我的自定义对话的LinearLayout。
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:textColor="@android:color/white"
android:hint="@string/enter_phone_number"
android:textColorHint="@android:color/white"
android:padding="15dp"
android:ems="10"
android:id="@+id/editNumber"
android:layout_gravity="center_horizontal" />
<View
style="@style/HeaderDivider" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:padding="15dp"
android:textColor="@android:color/white"
android:hint="@string/enter_name"
android:textColorHint="@android:color/white"
android:ems="10"
android:id="@+id/editName"
android:layout_gravity="center_horizontal" />
<View
style="@style/HeaderDivider" />
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_horizontal"
android:weightSum="2">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ok"
android:background="@drawable/button_shape"
android:layout_weight="1"
android:id="@+id/edit" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel"
android:background="@drawable/button_shape"
android:layout_weight="1"
android:id="@+id/cancel" />
</TableRow>
</LinearLayout>
这是我尝试创建自定义对话框的代码:
public class SendMessage extends ActionBarActivity {
private TextView friendNumber;
private Button chooseNumber;
private TextView friendName;
private String phoneNo;
private EditText editName;
private EditText editNumber;
private Button writeNumber;
private Button okButton;
private Button cancel;
public Button checkNumber;
private SharedPreferencesWrapper _spw;
public static final int PICK_CONTACT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_message);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView tv = (TextView) findViewById(R.id.edit_friend);
friendName = (TextView) findViewById(R.id.friendName);
_spw = new SharedPreferencesWrapper(SendMessage.this);
friendNumber = (TextView) findViewById(R.id.friend_number);
chooseNumber = (Button) findViewById(R.id.choose_number);
checkNumber = (Button) findViewById(R.id.check_number);
writeNumber = (Button) findViewById(R.id.write_number);
setTitle(R.string.title_section5);
chooseNumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
});
checkNumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phoneNumber = friendNumber.getText().toString();
phoneNo = phoneNumber.replace(" ", "").replace("-", "");
if(phoneNo.length() > 0 && friendName.getText().toString().length() > 0) {
if(phoneNo.length() >= 10) {
if (phoneNo.length() == 13) {
AsyncWebRequestsSender awrs = new AsyncWebRequestsSender(SendMessage.this, R.string.loading, R.string.getting_data);
awrs.execute(ISettings.Method_Invite, SendMessage.this, new CardHolder(_spw.getHolderId(), null, friendName.getText().toString(), null, new Card(null, phoneNo, null)));
} else {
int index = phoneNo.indexOf("0");
String phone = phoneNo.substring(index + 1);
phoneNo = "+996" + phone;
AsyncWebRequestsSender awrs = new AsyncWebRequestsSender(SendMessage.this, R.string.loading, R.string.getting_data);
awrs.execute(ISettings.Method_Invite, SendMessage.this, new CardHolder(_spw.getHolderId(), null, friendName.getText().toString(), null, new Card(null, phoneNo, null)));
}
}
else
ToastHelper.show(SendMessage.this, R.string.please_check_number);
}
else{
ToastHelper.show(SendMessage.this, R.string.please_enter_friend_name);
}
}
});
}
public void editFriend(View view) throws IOException{
try {
final Dialog dialog = new Dialog(SendMessage.this);
dialog.setContentView(R.layout.action_write_number);
dialog.setTitle("Title...");
editNumber = (EditText) findViewById(R.id.editNumber);
editName = (EditText) findViewById(R.id.editName);
okButton = (Button) findViewById(R.id.edit); // here okButton = null
okButton.setOnClickListener(new View.OnClickListener() { // and here IO catch Exception (NullPointerException)
@Override
public void onClick(View v) {
String phone = editNumber.getText().toString();
String name = editName.getText().toString();
friendNumber.setText(phone);
friendName.setText(name);
dialog.dismiss();
}
});
cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
catch (Exception e) {
String a = "";
}
}
问题是 - 当我点击按钮异常时捕获NullPointerException,我没有找到我的值不在哪里初始化。请帮帮我。
答案 0 :(得分:1)
喜欢
editNumber = (EditText) dialog.findViewById(R.id.editNumber);
editName = (EditText) dialog.findViewById(R.id.editName);
okButton = (Button) dialog.findViewById(R.id.edit);
editFriend(View view)
中的