您好在本应用程序中,我创建了一个包含以下代码的表单。单击“保存”按钮后,数据已保存到数据库中。现在如果我搜索一些mobile_no
意味着我使用一个类访问所有数据。
通过使用我正在访问我已编写以下代码的字段。
现在在我的Logcat中,它显示不幸的错误。任何人都可以帮我解决我错误的地方吗?
PatientRegistration.java
public class PatientRegistration extends Activity implements OnClickListener
{
EditText regno1,fname1,mname1,lname1,dob1,age1,f_hname1,adress1,tel_o1,pin_zip1,tel_r,email1,mobile1,id_prof1,b_group1,name_kin1,relation1;
String data="";
TextView tv1;
Button back,home,choosefile;
Spinner initial,pmaincat,pcatory,religion,caste;
RadioGroup sex,payment;
RadioButton rb;
private static final int SELECT_PICTURE = 1;
String[] initial_name,initial_id,patient_main_type_name,patient_type_name,religion_name,caste_name,reg_no,patient_main_type_id,patient_type_id,religion_id,caste_id;
private EditText fromDateEtxt;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;
private String selectedImagePath;
private Map<String, String> patmainMap = new HashMap<String, String>();
private Map<String, String> patcatMap = new HashMap<String, String>();
private Map<String, String> regMap = new HashMap<String, String>();
private Map<String, String> casteMap = new HashMap<String, String>();
private Map<String, String> initialMap = new HashMap<String, String>();
/** Called when the activity is first created. */
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.pat_registration);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
tv1=(TextView) findViewById(R.id.tv);
payment = (RadioGroup) findViewById(R.id.sex);
int selectedId = payment.getCheckedRadioButtonId();
rb = (RadioButton) findViewById(selectedId);
back=(Button) findViewById(R.id.back);
home=(Button) findViewById(R.id.home);
fname1=(EditText) findViewById(R.id.fname);
lname1=(EditText) findViewById(R.id.lname);
regno1=(EditText) findViewById(R.id.rgno);
regno1.setEnabled(false);
mname1=(EditText) findViewById(R.id.mname);
age1=(EditText) findViewById(R.id.age);
f_hname1=(EditText) findViewById(R.id.f_hname);
adress1=(EditText) findViewById(R.id.adress);
tel_o1=(EditText) findViewById(R.id.telo);
pin_zip1=(EditText) findViewById(R.id.pzipcode);
tel_r=(EditText) findViewById(R.id.tel_r);
email1=(EditText) findViewById(R.id.email);
mobile1=(EditText) findViewById(R.id.mobile);
id_prof1=(EditText) findViewById(R.id.idprof);
b_group1=(EditText) findViewById(R.id.bgroup);
name_kin1=(EditText) findViewById(R.id.name_kin);
relation1=(EditText) findViewById(R.id.relationship);
initial=(Spinner)findViewById(R.id.intial);
pmaincat=(Spinner)findViewById(R.id.pmaincat);
pcatory=(Spinner)findViewById(R.id.pcatgory);
religion=(Spinner)findViewById(R.id.religion);
caste=(Spinner)findViewById(R.id.caste);
sex=(RadioGroup)findViewById(R.id.sex);
payment=(RadioGroup)findViewById(R.id.paymentmode);
tv1.setText("PATIENT REGISTRATION");
final Button photo1 = (Button) findViewById(R.id.photo);
Button Send = (Button) findViewById(R.id.Send);
Send.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Global.first_name = fname1.getText().toString();
String middle_name = mname1.getText().toString();
String last_name = lname1.getText().toString();
String reg_no = regno1.getText().toString();
String dob = fromDateEtxt.getText().toString();
String age = age1.getText().toString();
String address = adress1.getText().toString();
String phone_office = tel_o1.getText().toString();
String fh = f_hname1.getText().toString();
String pin = pin_zip1.getText().toString();
String phone_resi = tel_r.getText().toString();
String email = email1.getText().toString();
String mobile_no = mobile1.getText().toString();
String id_proof = id_prof1.getText().toString();
String blood_group = b_group1.getText().toString();
String name_of_kin = name_kin1.getText().toString();
String relation = relation1.getText().toString();
String gender= ((RadioButton)findViewById(sex.getCheckedRadioButtonId())).getText().toString();
String pmode= ((RadioButton)findViewById(payment.getCheckedRadioButtonId())).getText().toString();
String photo = photo1.getUrls().toString();
String patient_main_type_name = patmainMap.get(String
.valueOf(pmaincat.getSelectedItem()));
String pc = patcatMap.get(String
.valueOf(pcatory.getSelectedItem()));
String religion_id = regMap.get(String
.valueOf(religion.getSelectedItem()));
String caste_id=casteMap.get(String
.valueOf(caste.getSelectedItem()));
String initial_id=initialMap.get(String
.valueOf(initial.getSelectedItem()));
Toast.makeText(
PatientRegistration.this,
"Result : " + "\nclassId : " + initial_id
,
Toast.LENGTH_SHORT).show();
/* if(first_name.equals(""))
{
fname1.setError( "Please Enter First Name" );
}
else if(last_name.equals(""))
{
lname1.setError( "Please Enter Last Name" );
}
if(reg_no.equals(""))
{
fname1.setError( "Please Select Religon" );
}
else if(last_name.equals(""))
{
lname1.setError( "Please Enter Last Name" );
}
else if(mobile.equals(""))
{
mobile1.setError( "Please Enter Mobile No." );
}
else if(!isValidMobile(mobile)){
}
else if(email.equals(""))
{
email1.setError( "Please Enter EmailId" );
}
else if(!isValidMail(email)){
}
else
{
*/
try{
String queryString ="reg_no="+ reg_no
+"&pmc="+patient_main_type_name+"&pc="+pc+ "&initial_id="+initial_id+"&first_name="+Global.first_name+"&middle_name="+middle_name+"&last_name="+last_name
+"&fh="+fh+"&religion_id="+religion_id+"&dob="+dob+"&caste_id="+caste_id+"&address="+address+"&pin="+pin
+"&email="+email+"&id_proof="+id_proof+"&phone_office="+phone_office+"&phone_resi="+phone_resi+"&mobile_no="+mobile_no+"&name_of_kin="+name_of_kin
+"&relation="+relation+"&blood_group="+blood_group+"&photo="+photo+"&pmode="+pmode;
data = DatabaseUtility.executeQueryPhp("pat_registration",queryString);
/*fname1.setText("");
lname1.setText("");
mobile1.setText("");
altmob1.setText("");
email1.setText("");
comment1.setText("");*/
Toast.makeText(
PatientRegistration.this,
"Message:Records Saved Sucessfully",
Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
findViewsById();
setDateTimeField();
String result = DatabaseUtility.executeQueryPhp("initialName","");
System.out.print(result);
try
{
JSONArray JA = new JSONArray(result);
initial_name = new String[JA.length()];
initial_id = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
initial_name[i] = JA.getJSONObject(i).getString("initial_name");
initial_id[i] = JA.getJSONObject(i).getString("initial_id");
initialMap.put(initial_name[i],initial_id[i]);
}
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
e.printStackTrace();
}
String pat_main_type_id = DatabaseUtility.executeQueryPhp("pat_main_type_name","");
System.out.print(pat_main_type_id);
try
{
JSONArray JA = new JSONArray(pat_main_type_id);
JSONObject json = null;
patient_main_type_id = new String[JA.length()];
patient_main_type_name = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
json = JA.getJSONObject(i);
patient_main_type_id[i] = json.getString("patient_main_type_id");
patient_main_type_name[i] = json.getString("patient_main_type_name");
patmainMap.put(patient_main_type_name[i],patient_main_type_id[i]);
}
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
e.printStackTrace();
}
String RegNo = DatabaseUtility.executeQueryPhp("reg","");
System.out.print(RegNo);
try
{
JSONArray JA = new JSONArray(RegNo);
reg_no = new String[JA.length()];
String value = "PAT";
for(int i=0;i<JA.length();i++)
{
reg_no[i] = JA.getJSONObject(i).getString("reg_no");
value+=reg_no[i];
}
regno1.setText(value);
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
e.printStackTrace();
}
String pat_catgory = DatabaseUtility.executeQueryPhp("pat_catgory","");
System.out.print(pat_catgory);
try
{
JSONArray JA = new JSONArray(pat_catgory);
patient_type_id = new String[JA.length()];
patient_type_name = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
patient_type_id[i] = JA.getJSONObject(i).getString("patient_type_id");
patient_type_name[i] = JA.getJSONObject(i).getString("patient_type_name");
patcatMap.put(patient_type_name[i], patient_type_id[i]);
}
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
e.printStackTrace();
}
String religion = DatabaseUtility.executeQueryPhp("religion","");
System.out.print(religion);
try
{
JSONArray JA = new JSONArray(religion);
religion_id= new String[JA.length()];
religion_name= new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
religion_id[i] = JA.getJSONObject(i).getString("religion_id");
religion_name[i] = JA.getJSONObject(i).getString("religion_name");
regMap.put(religion_name[i], religion_id[i]);
}
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
e.printStackTrace();
}
String caste = DatabaseUtility.executeQueryPhp("caste","");
System.out.print(caste);
try
{
JSONArray JA = new JSONArray(caste);
caste_id= new String[JA.length()];
caste_name= new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
caste_id[i] = JA.getJSONObject(i).getString("caste_id");
caste_name[i] = JA.getJSONObject(i).getString("caste_name");
casteMap.put(caste_name[i],caste_id[i]);
}
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
e.printStackTrace();
}
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
onBackPressed();
}
});
home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent in=new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
}
});
}
private void spinner_fn() {
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
initial_name);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
initial.setAdapter(dataAdapter);
initial.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
Log.e("Position new", initial_name[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
patient_main_type_name);
dataAdapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
pmaincat.setAdapter(dataAdapter1);
pmaincat.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
Log.e("Position new", patient_main_type_name[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
patient_type_name);
dataAdapter2
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
pcatory.setAdapter(dataAdapter2);
pcatory.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
Log.e("Position new", patient_main_type_name[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
religion_name);
dataAdapter3
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
religion.setAdapter(dataAdapter3);
religion.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
Log.e("Position new", patient_main_type_name[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
caste_name);
dataAdapter4
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
caste.setAdapter(dataAdapter4);
caste.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
Log.e("Position new", patient_main_type_name[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private void findViewsById() {
fromDateEtxt = (EditText) findViewById(R.id.dob);
fromDateEtxt.setInputType(InputType.TYPE_NULL);
fromDateEtxt.requestFocus();
}
private void setDateTimeField() {
fromDateEtxt.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this,
new OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate
.getTime()));
}
}, newCalendar.get(Calendar.YEAR),
newCalendar.get(Calendar.MONTH),
newCalendar.get(Calendar.DAY_OF_MONTH));
}
@Override
public void onBackPressed(){
super.onBackPressed();
}
private boolean isValidMail(String email)
{
boolean check;
Pattern p;
Matcher m;
String EMAIL_STRING = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
p = Pattern.compile(EMAIL_STRING);
m = p.matcher(email);
check = m.matches();
if(!check)
{
email1.setError("Not Valid Email");
}
return check;
}
private boolean isValidMobile(String mobile)
{
boolean check;
if(mobile.length() < 6 || mobile.length() > 13)
{
check = false;
mobile1.setError("Not Valid Number");
}
else
{
check = true;
}
return check;
}
public void onNothingSelected(AdapterView<?> arg0) {
}
public void onClick(View view) {
if (view == fromDateEtxt) {
fromDatePickerDialog.show();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
//img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
OPD_Registration.java
public class OPD_Registration extends Activity implements OnClickListener
{
EditText age,fname1,mname1,lname1,dob1,age1adress1,tel_o1,pin_zip1,tel_r,email1,mobile1,b_group1,sex,dob,address,religion,caste,entry_date;
String data="";
TextView tv1;
Button back,home;
Spinner department,ref_doctor,consl_doctor;
private static final int SELECT_PICTURE = 1;
String[] department_name,department_id,patient_main_type_name,patient_type_name,religion_name,caste_name;
private EditText fromDateEtxt;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;
private String selectedImagePath;
private Map<String, String> departMap = new HashMap<String, String>();
private Map<String, String> ref_doctMap = new HashMap<String, String>();
private Map<String, String> cons_doctMap = new HashMap<String, String>();
/** Called when the activity is first created. */
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.opd_registration);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
tv1=(TextView) findViewById(R.id.tv);
back=(Button) findViewById(R.id.back);
home=(Button) findViewById(R.id.home);
fname1=(EditText) findViewById(R.id.fname);
lname1=(EditText) findViewById(R.id.lname);
age=(EditText) findViewById(R.id.age);
sex=(EditText) findViewById(R.id.sex);
dob=(EditText) findViewById(R.id.dob);
mname1=(EditText) findViewById(R.id.mname);
address=(EditText) findViewById(R.id.adress);
tel_o1=(EditText) findViewById(R.id.telo);
pin_zip1=(EditText) findViewById(R.id.pzipcode);
tel_r=(EditText) findViewById(R.id.tel_r);
email1=(EditText) findViewById(R.id.email);
mobile1=(EditText) findViewById(R.id.mobile);
tel_o1=(EditText) findViewById(R.id.telo);
b_group1=(EditText) findViewById(R.id.bgroup);
tel_r=(EditText) findViewById(R.id.tel_r);
religion=(EditText)findViewById(R.id.religion);
caste=(EditText)findViewById(R.id.caste);
entry_date=(EditText)findViewById(R.id.etdate);
department=(Spinner)findViewById(R.id.intial);
ref_doctor=(Spinner)findViewById(R.id.pmaincat);
consl_doctor=(Spinner)findViewById(R.id.pcatgory);
tv1.setText("OPD REGISTRATION");
fname1.setText(Global.first_name);
mname1.setText(Global.middle_name);
lname1.setText(Global.last_name);
fromDateEtxt.setText(Global.dob);
tel_o1.setText(Global.phone_office);
pin_zip1.setText(Global.pin);
tel_r.setText(Global.phone_resi);
email1.setText(Global.email);
mobile1.setText(Global.mobile_no);
b_group1.setText(Global.blood_group);
String queryString ="reg_no="+Global.reg_no
+"&mobile_no="+Global.mobile_no;
data = DatabaseUtility.executeQueryPhp("Opd_Reg",queryString);
Logcat
11-04 08:38:17.613: E/AndroidRuntime(4002): FATAL EXCEPTION: main
11-04 08:38:17.613: E/AndroidRuntime(4002): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vcarehospital/com.example.vcarehospital.OPD_Registration}: java.lang.NullPointerException
11-04 08:38:17.613: E/AndroidRuntime(4002): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-04 08:38:17.613: E/AndroidRuntime(4002): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-04 08:38:17.613: E/AndroidRuntime(4002): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-04 08:38:17.613: E/AndroidRuntime(4002): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-04 08:38:17.613: E/AndroidRuntime(4002): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 08:38:17.613: E/AndroidRuntime(4002): at android.os.Looper.loop(Looper.java:137)
11-04 08:38:17.613: E/AndroidRuntime(4002): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-04 08:38:17.613: E/AndroidRuntime(4002): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 08:38:17.613: E/AndroidRuntime(4002): at java.lang.reflect.Method.invoke(Method.java:511)
11-04 08:38:17.613: E/AndroidRuntime(4002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-04 08:38:17.613: E/AndroidRuntime(4002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-04 08:38:17.613: E/AndroidRuntime(4002): at dalvik.system.NativeStart.main(Native Method)
11-04 08:38:17.613: E/AndroidRuntime(4002): Caused by: java.lang.NullPointerException
11-04 08:38:17.613: E/AndroidRuntime(4002): at com.example.vcarehospital.OPD_Registration.onCreate(OPD_Registration.java:135)
11-04 08:38:17.613: E/AndroidRuntime(4002): at android.app.Activity.performCreate(Activity.java:5104)
11-04 08:38:17.613: E/AndroidRuntime(4002): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-04 08:38:17.613: E/AndroidRuntime(4002): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-04 08:38:17.613: E/AndroidRuntime(4002): ... 11 more
11-04 08:38:17.613: E/AndroidRuntime(4002): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vcarehospital/com.example.vcarehospital.OPD_Registration}: java.lang.NullPointerException
答案 0 :(得分:0)
NPE:
fromDateEtxt.setText(Global.dob);
您尚未使用fromDateEtxt
初始化findViewById()
,就像您正在初始化其他一些视图引用一样。
通常,像这样的错误很容易通过堆栈跟踪和可能的调试器找出。学习使用两者对于Android开发人员来说至关重要。