我有一个列表活动,其中包含所有处方的列表。当单击列表项活动时,会调用编辑处方来更新表格,并在成功时切换回列表活动并重新加载它。
但是当我回到列表活动错误更新后:无法启动活动组件信息 java.lang.nullpointerexception 被显示。
行号 堆栈跟踪中的 87 是 pid = extras.getString("TAG_PATIENT_ID"); 在 DocPresc 列表活动中。
这是我的列表活动:
public class DocPresc extends ListActivity {
//public static Context ctx;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String,String>>();
String pid;
JSONArray products = null;
EditText ailm,date,comment;
Button delete;
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_patient_presc = "http://192.168.44.208/get_prescription.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
// private static final String TAG_PATIENT_ID = "patient_id";
private static final String TAG_AIL = "ailment";
private static final String TAG_MED = "medicine_name";
private static final String TAG_D1 = "qty1";
private static final String TAG_D2 = "qty2";
private static final String TAG_D3 = "qty3";
private static final String TAG_DATE = "prescription_date";
private static final String TAG_COM = "comment";
private static final String TAG_DID = "dosage_id";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.docpresc);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
.penaltyLog().build());
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent i = getIntent();
Bundle extras = i.getExtras();
pid = extras.getString("TAG_PATIENT_ID");
System.out.println("Docpresc"+pid);
ailm = (EditText)findViewById(R.id.ailment1);
date = (EditText)findViewById(R.id.date1);
comment = (EditText)findViewById(R.id.comment1);
new LoadPrescriptions().execute();
}
class LoadPrescriptions extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
// Building Parameters
runOnUiThread(new Runnable() {
public void run() {
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
params.add(new BasicNameValuePair("patient_id",pid));//search1.getText().toString()));
System.out.println("database"+pid);
JSONObject json = jParser.makeHttpRequest(url_patient_presc, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Patients: ", json.toString());
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
//-----------------------------------
/*JSONObject product = products.getJSONObject(0);
ailm = (EditText)findViewById(R.id.ailment1);
ailm.setText(product.getString(TAG_AIL));*/
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String medicine = c.getString(TAG_MED).toUpperCase();
String qty1 = c.getString(TAG_D1).toUpperCase();
String qty2 = c.getString(TAG_D2).toUpperCase();
String qty3 = c.getString(TAG_D3).toUpperCase();
String dsg_id = c.getString(TAG_DID).toUpperCase();
//String ail = c.getString(TAG_AIL).toUpperCase();
ailm.setText(c.getString(TAG_AIL));
date.setText(c.getString(TAG_DATE));
comment.setText(c.getString(TAG_COM));
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_MED, medicine);
map.put(TAG_D1,qty1);
map.put(TAG_D2,qty2);
map.put(TAG_D3,qty3);
map.put(TAG_DID,dsg_id);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
// pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
DocPresc.this, productsList,
R.layout.list_item2, new String[] {
TAG_MED,TAG_D1,TAG_D2,TAG_D3,TAG_DID},
new int[] {R.id.med,R.id.d1,R.id.d2,R.id.d3,R.id.did });
// updating listview
//setListAdapter(adapter);
setListAdapter(adapter);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
//menuSearch = menu.findItem(R.id.delete);
//menuSearch.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.add:
Bundle bundle = new Bundle();
bundle.putString("TAG_PATIENT_ID",pid );
System.out.println("bundle"+pid);
Intent i = new Intent(DocPresc.this,AddPresc.class);
i.putExtras(bundle);
startActivity(i);
//finish();
// startActivity(new Intent(DocPresc.this,AddPresc.class));
// finish();
// Intent i = getIntent();
// finish();
// startActivity(i);
// case R.id.print:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onListItemClick(ListView lv, View v, int position, long id) {
// TODO Auto-generated method stub
//onAttach(getActivity());
//lv.setAdapter(adapter);
String did1 = ((TextView) v.findViewById(R.id.did)).getText().toString();
String med1 = ((TextView) v.findViewById(R.id.med)).getText().toString();
String dg1 = ((TextView) v.findViewById(R.id.d1)).getText().toString();
String dg2 = ((TextView) v.findViewById(R.id.d2)).getText().toString();
String dg3 = ((TextView) v.findViewById(R.id.d3)).getText().toString();
// System.out.println("all patient"+id1);
Bundle bundle = new Bundle();
bundle.putString("TAG_DOSAGE_ID",did1 );
bundle.putString("TAG_DOSAGE_ID1",med1 );
bundle.putString("TAG_DOSAGE_ID2",dg1 );
bundle.putString("TAG_DOSAGE_ID3",dg2 );
bundle.putString("TAG_DOSAGE_ID4",dg3 );
// System.out.println("bundle"+id1);
Intent i = new Intent(DocPresc.this,EditPresc.class);
i.putExtras(bundle);
startActivityForResult(i,100);
//passData(date);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
}
这是编辑处方活动
public class EditPresc extends Activity implements View.OnClickListener{
//public static Context ctx;
EditText medicine;
EditText dosage1;
EditText dosage2;
EditText dosage3;
Button edit;
ImageButton up1 , up2,up3;
ImageButton down1,down2,down3;
String did,medi,q1,q2,q3;
int count = 1;
//JSONArray products = null;
//int pid = "100";
//private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
// private static final String url_getdosage = "http://192.168.44.208/get_dosage.php";
private static final String url_updatedosage = "http://192.168.44.208/update_dosage.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCT = "product";
// private static final String TAG_PATIENT_ID = "patient_id";
private static final String TAG_MED = "medicine_name";
private static final String TAG_D1 = "qty1";
private static final String TAG_D2 = "qty2";
private static final String TAG_D3 = "qty3";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editpresc);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
.penaltyLog().build());
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent i = getIntent();
Bundle extras = i.getExtras();
did = extras.getString("TAG_DOSAGE_ID");
medi = extras.getString("TAG_DOSAGE_ID1");
q1 = extras.getString("TAG_DOSAGE_ID2");
q2 = extras.getString("TAG_DOSAGE_ID3");
q3 = extras.getString("TAG_DOSAGE_ID4");
medicine = (EditText)findViewById(R.id.atxt1);
dosage1 = (EditText)findViewById(R.id.text1);
dosage2 = (EditText)findViewById(R.id.text2);
dosage3 = (EditText)findViewById(R.id.text3);
medicine.setText(medi);
dosage1.setText(q1);
dosage2.setText(q2);
dosage3.setText(q3);
System.out.println("Editpresc"+did);
//send = (Button)findViewById(R.id.b1);
up1=(ImageButton)findViewById(R.id.up1);
down1=(ImageButton)findViewById(R.id.down1);
dosage1=(EditText)findViewById(R.id.text1);
up2=(ImageButton)findViewById(R.id.up2);
down2=(ImageButton)findViewById(R.id.down2);
dosage2=(EditText)findViewById(R.id.text2);
up3=(ImageButton)findViewById(R.id.up3);
down3=(ImageButton)findViewById(R.id.down3);
dosage3=(EditText)findViewById(R.id.text3);
//dosage1.setText("1");
up1.setOnClickListener(this);
up2.setOnClickListener(this);
up3.setOnClickListener(this);
down1.setOnClickListener(this);
down2.setOnClickListener(this);
down3.setOnClickListener(this);
//ailment = (EditText)findViewById(R.id.atxt);
//comment = (EditText)findViewById(R.id.ctxt);
//presc=(EditText)findViewById(R.id.presc_id);
edit = (Button)findViewById(R.id.save);
// new GetDosage().execute();
edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new EditDosageDetails().execute();
}
});
}
private class EditDosageDetails extends AsyncTask<String, Void, String> {
// JSONObject product;
protected String doInBackground(String... args) {
//JSONObject product = null;
//id.setText(100);
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
String med = medicine.getText().toString();
System.out.println("editdosage"+med);
String do1 = dosage1.getText().toString();
String do2 = dosage2.getText().toString();
String do3 = dosage3.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("medicine_name", med));
params.add(new BasicNameValuePair("dosage_id", did));
params.add(new BasicNameValuePair("qty1", do1));
params.add(new BasicNameValuePair("qty2", do2));
params.add(new BasicNameValuePair("qty3", do3));
JSONObject json = jsonParser.makeHttpRequest(url_updatedosage,
"POST", params);
// json success tag
// Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = getIntent();
setResult(100, i);
finish();
// super.onBackPressed();
// closing this screen
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
// }
}
});
//return product;
return null;
// });
// return product;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
//menuSearch = menu.findItem(R.id.delete);
//menuSearch.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.up1:
int a=Integer.parseInt(dosage1.getText().toString());
int b = a+1;
dosage1.setText(new Integer(b).toString());
break;
case R.id.down1:
a=Integer.parseInt(dosage1.getText().toString());
b = a-1;
dosage1.setText(new Integer(b).toString());
break;
case R.id.up2:
a=Integer.parseInt(dosage2.getText().toString());
b = a+1;
dosage2.setText(new Integer(b).toString());
break;
case R.id.down2:
a=Integer.parseInt(dosage2.getText().toString());
b = a-1;
dosage2.setText(new Integer(b).toString());
case R.id.up3:
a=Integer.parseInt(dosage3.getText().toString());
b = a+1;
dosage3.setText(new Integer(b).toString());
break;
case R.id.down3:
a=Integer.parseInt(dosage3.getText().toString());
b = a-1;
dosage3.setText(new Integer(b).toString());
break;
//case R.id.save:
// System.out.println("save pressed");
}
}
}
这是堆栈跟踪
06-13 17:59:35.558: E/AndroidRuntime(26369): FATAL EXCEPTION: main
06-13 17:59:35.558: E/AndroidRuntime(26369): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.actionbar/com.example.actionbar.DocPresc}: java.lang.NullPointerException
06-13 17:59:35.558: E/AndroidRuntime(26369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
06-13 17:59:35.558: E/AndroidRuntime(26369): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
06-13 17:59:35.558: E/AndroidRuntime(26369): at android.app.ActivityThread.access$600(ActivityThread.java:140)
06-13 17:59:35.558: E/AndroidRuntime(26369): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
06-13 17:59:35.558: E/AndroidRuntime(26369): at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 17:59:35.558: E/AndroidRuntime(26369): at android.os.Looper.loop(Looper.java:137)
06-13 17:59:35.558: E/AndroidRuntime(26369): at android.app.ActivityThread.main(ActivityThread.java:4895)
06-13 17:59:35.558: E/AndroidRuntime(26369): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 17:59:35.558: E/AndroidRuntime(26369): at java.lang.reflect.Method.invoke(Method.java:511)
06-13 17:59:35.558: E/AndroidRuntime(26369): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
06-13 17:59:35.558: E/AndroidRuntime(26369): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
06-13 17:59:35.558: E/AndroidRuntime(26369): at dalvik.system.NativeStart.main(Native Method)
06-13 17:59:35.558: E/AndroidRuntime(26369): Caused by: java.lang.NullPointerException
06-13 17:59:35.558: E/AndroidRuntime(26369): at com.example.actionbar.DocPresc.onCreate(DocPresc.java:87)
06-13 17:59:35.558: E/AndroidRuntime(26369): at android.app.Activity.performCreate(Activity.java:5163)
06-13 17:59:35.558: E/AndroidRuntime(26369): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-13 17:59:35.558: E/AndroidRuntime(26369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
06-13 17:59:35.558: E/AndroidRuntime(26369): ... 11 more