我有一个自定义对话框,其中有两个单选按钮。两个单选按钮放在Radio组中。每当我将onClickListener应用于单选按钮时,我面临一个错误,我的应用程序停止。
这是我的代码 - :
package com.example.user.attendance;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.Patterns;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.hdodenhof.circleimageview.CircleImageView;
import static android.app.Activity.RESULT_OK;
public class Fragments extends Fragment {
Button Register_Cancel_fullname, Register_Cancel_email, Register_Cancel_phone,
Register_Cancel_password, Sign_up,Register_Cancel_Confirm_Password;
String Passcode,Confirm_Passcode;
EditText Full_Name, Email_Address, Phone_number, Password_register,Confirm_Password_register;
CircleImageView Register_add_photo;
LinearLayout select_photo_from_camera,select_photo_from_gallery;
TextView textView,hint_textview_circular_image_view;
// RadioButton Category_teacher,Category_student;
RadioGroup Teacher_student;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myfragmentVieew = inflater.inflate(R.layout.fragment_signup, container, false);
//Edit Text
Full_Name = (EditText) myfragmentVieew.findViewById(R.id.Register_name_and_surname);
Email_Address = (EditText) myfragmentVieew.findViewById(R.id.Register_Email_address);
Phone_number = (EditText) myfragmentVieew.findViewById(R.id.Register_Phone);
Password_register = (EditText) myfragmentVieew.findViewById(R.id.Register_Password);
Confirm_Password_register=(EditText)myfragmentVieew.findViewById(R.id
.Confirm_Password_Register);
//Button
Sign_up = (Button) myfragmentVieew.findViewById(R.id.Register_Signup);
Register_Cancel_fullname = (Button) myfragmentVieew.findViewById(R.id.Register_First_Cancel_Button);
Register_Cancel_email = (Button) myfragmentVieew.findViewById(R.id.Register_Second_Cancel_Button);
Register_Cancel_phone = (Button) myfragmentVieew.findViewById(R.id.Register_Third_Cancel_Button);
Register_Cancel_password = (Button) myfragmentVieew.findViewById(R.id.Register_fourth_Cancel_Button);
Register_Cancel_Confirm_Password=(Button)myfragmentVieew.findViewById(R.id
.Register_Cancel_Confirm_Password_Button);
Register_add_photo=(CircleImageView)myfragmentVieew.findViewById(R.id.Register_add_photo);
Teacher_student=(RadioGroup)myfragmentVieew.findViewById(R.id.Category_teacher_student);
/*Category_student=(RadioButton)myfragmentVieew.findViewById(R.id
.Student_category_radio_button);
Category_teacher=(RadioButton)myfragmentVieew.findViewById(R.id
.Teacher_category_radio_button);*/
//TextView
textView = (TextView) myfragmentVieew.findViewById(R.id.Register_textview);
hint_textview_circular_image_view=(TextView)myfragmentVieew.findViewById(R.id
.hint_of_image_view);
/*Setting the Visibility of the Cancel Button*/
Register_Cancel_fullname.setVisibility(View.INVISIBLE);
Register_Cancel_email.setVisibility(View.INVISIBLE);
Register_Cancel_phone.setVisibility(View.INVISIBLE);
Register_Cancel_password.setVisibility(View.INVISIBLE);
Register_Cancel_Confirm_Password.setVisibility(View.INVISIBLE);
// Signup Button disabled by default
Sign_up.setEnabled(false);
/*IMPLEMENTING TEXT WATCHER*/
//Full Name
Full_Name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
cancel_button_visibility(Full_Name,Register_Cancel_fullname);
}
@Override
public void afterTextChanged(Editable editable) {
signup_button_enability();
}
});
//Email Id
Email_Address.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
cancel_button_visibility(Email_Address,Register_Cancel_email);
}
@Override
public void afterTextChanged(Editable editable) {
signup_button_enability();
}
});
//Phone Number
Phone_number.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
cancel_button_visibility(Phone_number,Register_Cancel_phone);
}
@Override
public void afterTextChanged(Editable editable) {
signup_button_enability();
}
});
//Password
Password_register.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
cancel_button_visibility(Password_register,Register_Cancel_password);
}
@Override
public void afterTextChanged(Editable editable) {
signup_button_enability();
}
});
//Confirm Password
Confirm_Password_register.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
cancel_button_visibility(Confirm_Password_register,Register_Cancel_Confirm_Password);
}
@Override
public void afterTextChanged(Editable editable) {
signup_button_enability();
}
});
/*IMPLEMENTING ONCLICK LISTENER FOR CANCEL BUTTON*/
cancel_button_visibility_delete(Register_Cancel_fullname, Full_Name);
cancel_button_visibility_delete(Register_Cancel_email, Email_Address);
cancel_button_visibility_delete(Register_Cancel_phone, Phone_number);
cancel_button_visibility_delete(Register_Cancel_password, Password_register);
cancel_button_visibility_delete(Register_Cancel_Confirm_Password, Confirm_Password_register);
/*For Onlick Submit Button*/
Sign_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//int flag=0;
int name_flag=0,passcode_flag=0,repasscode_flag=0,email_flag=0,mobile_flag=0;
Pattern pattern;
Matcher matcher;
/*NAME PATTERN*/
String NAME_PATTERN = "" +
"[.]+||[aeiou]+||[AEIOU]+||[b-df-hj-nq-tv-z]+||[B-DF-HJ-NQ-TV-Z]+||[0-9" +
"]+||[B-DF" +
"-HJ-NQ-T]+[AEIOU]+||[b-df-hj-nq-tv-z]+[aeiou]+||[A-Za-z]+[0-9]+||[0-9]+[A-Za-z]+";
pattern = Pattern.compile(NAME_PATTERN);
matcher = pattern.matcher(Full_Name.getText().toString().trim());
if (matcher.matches() == true) {
Full_Name.setBackgroundResource(R.drawable.edit_text_border_red);
Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim
.shake);
Full_Name.startAnimation(shake);
name_flag=0;
}
else {
name_flag= 1;
}
/*EMAIL PATTERN*/
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(Email_Address.getText().toString().trim());
if (matcher.matches() == false) {
Email_Address.setBackgroundResource(R.drawable.edit_text_border_red);
Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim
.shake);
Email_Address.startAnimation(shake);
email_flag=0;
}
else {
email_flag = 1;
}
/*PHONE PATTERN*/
String PHONE_PATTERN = "[0]||[1]||[2]||[3]||[4]||[5]||[6]||9{10}||8{10}||7{10}||9{5}0" +
"{5}||8{5}0{5}||7{5}0{5}||9{5}1{5}||8{5}1{5}||7{5}1{5}||9{5}2{5}||8{5}2{5}||7{5}2{5" +
"}||9{5}3{5}||8{5}3{5}||7{5}3{5}||9{5}4{5}||8{5}4{5}||7{5}4{5}||9{5}5{5}||8{5}5{5" +
"}||7{5}5{5}||9{5}6{5}||8{5}6{5}||7{5}6{5}||9{5}7{5}||8{5}7{5}||9{5}8{5}||9{4}||8{4" +
"}||7{4}";
pattern = Pattern.compile(PHONE_PATTERN);
matcher = pattern.matcher(Phone_number.getText().toString().trim());
if (matcher.matches() == true || Phone_number.getText().toString().length()
< 10) {
Phone_number.setBackgroundResource(R.drawable.edit_text_border_red);
Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim
.shake);
Phone_number.startAnimation(shake);
mobile_flag=0;
}
else {
mobile_flag = 1;
}
/*PASSWORD VALIDATION*/
Passcode = Password_register.getText().toString().trim();
Confirm_Passcode = Confirm_Password_register.getText().toString()
.trim();
if (Passcode.length()<5) {
Toast.makeText(getActivity(),"The passcode should be greater than the 5" +
" digit",Toast.LENGTH_SHORT).show();
passcode_flag=0;
}
/*CHECKING PASSWORD EQUALITY*/
if (Passcode.equals(Confirm_Passcode)) {
Toast.makeText(getActivity(), "MATCHED",
Toast.LENGTH_LONG).show();
repasscode_flag=1;
}
else {
Toast.makeText(getActivity(), "UNMATCHED",
Toast.LENGTH_LONG).show();
repasscode_flag=0;
}
if(name_flag==1 && email_flag==1 && mobile_flag==1 &&
repasscode_flag==1){
LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
View promptView2=layoutInflater.inflate(R.layout
.custom_dialog_for_category_teacher_student,null);
AlertDialog alertDialog=new AlertDialog.Builder(getActivity()).create();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.setView(promptView2);
alertDialog.show();
}
}
});
Teacher_student.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(i==R.id.Radio_button_teacher)
Toast.makeText(getActivity(), "CLICKED", Toast.LENGTH_SHORT).show();
}
});
Register_add_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
View promptView=layoutInflater.inflate(R.layout.customdialog,null);
final AlertDialog builder=new AlertDialog.Builder(getActivity()).create();
builder.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
select_photo_from_camera=(LinearLayout) promptView.findViewById(R.id
.Select_photo_from_Camera);
select_photo_from_gallery=(LinearLayout)promptView.findViewById(R.id
.Select_photo_from_gallery);
builder.setView(promptView);
select_photo_from_camera.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent cameraintent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
int CAMERA_PIC_REQUEST=1;
startActivityForResult(cameraintent,CAMERA_PIC_REQUEST);
}
});
select_photo_from_gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int PICK_IMAGE_REQUEST=0;
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select picture"),
PICK_IMAGE_REQUEST);
}
});
builder.show();
}
});
return myfragmentVieew;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode,resultCode,data);
/*if (requestCode == 1 && resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
hint_textview_circular_image_view.setVisibility(View.GONE);
Register_add_photo.setImageBitmap(bitmap);
}*/
if (requestCode == 0 && resultCode == RESULT_OK) {
// address of the image
Uri selectedImages=data.getData();
//declare a stream to read the image data from the "selectedimages"
InputStream inputStream;
try {
inputStream=getActivity().getContentResolver().openInputStream(selectedImages);
Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
Register_add_photo.setImageBitmap(bitmap);
hint_textview_circular_image_view.setVisibility(View.GONE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Bitmap bitmap = (Bitmap) data.getExtras().get("data");
}
}
void signup_button_enability() {
if (Full_Name.getText().toString().trim().length() > 0 && Email_Address.getText().toString().trim().length() > 0 &&
Phone_number.getText().toString().trim().length() > 0 && Password_register
.getText().toString().trim().length() > 0 && Confirm_Password_register.getText()
.toString().trim().length()>0) {
Sign_up.setEnabled(true);
} else {
Sign_up.setEnabled(false);
}
}
public void cancel_button_visibility_delete(Button cancel, final EditText editText) {
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText.setText("");
}
});
}
public void cancel_button_visibility(EditText editText,Button button){
if(editText.getText().toString().length()>0)
button.setVisibility(View.VISIBLE);
else
button.setVisibility(View.GONE);
}
}
自定义对话框xml文件:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_dialog">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHOOSE YOUR CATEGORY"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:layout_marginLeft="12dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/hint_color"
android:layout_marginTop="40dp"
android:id="@+id/view"></View>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEACHER"
android:layout_marginTop="53sp"
android:layout_marginLeft="12sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STUDENT"
android:layout_marginTop="95sp"
android:layout_marginLeft="12sp"
android:textStyle="bold"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/view"
android:paddingTop="5dp"
android:id="@+id/Category_teacher_student"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Teacher_category_radio_button"
android:layout_marginLeft="300dp"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/hint_color"
android:layout_marginTop="5dp"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="300dp"
android:id="@+id/Student_category_radio_button"/>
</RadioGroup>
</RelativeLayout>
logcat显示:
Process: com.example.user.attendance, PID: 15344
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference
at com.example.user.attendance.Fragments.onCreateView(Fragments.java:336)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7402)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我甚至尝试将onClickListener用于单选按钮,但显示的错误相同。