我需要将同一个Activity调用到多个类中,但当我尝试在两个不同的类中使用相同的Activity时,我得到应用程序停止的错误!
“我的活动”的第一次调用 已完成,没有任何错误:
Intent prevIntent=new Intent(ChequesInfo.this,GetItemsActivity.class);
prevIntent.putExtra("ip", string);
prevIntent.putExtra("dbname", dbname);
prevIntent.putExtra("dbpass", dbpass);
startActivity(prevIntent);
但是在另一个类的第二次调用中,它给了我错误:
Intent prevIntent=new Intent(InventoryStock.this,GetItemsActivity.class);
prevIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
prevIntent.putExtra("ip", ip);
prevIntent.putExtra("dbname", dbname);
prevIntent.putExtra("dbpass", dbpass);
startActivity(prevIntent);
GetItemsActivity 用于从服务器获取项目的类,其中:
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(GetItemsActivity.this);
pDialog.setMessage("Loading Items. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
try {
result=bringData("http://"+ip+"/StatementOfAccount/getItems.php",dbname,dbpass);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Intent prevIntent=new Intent(GetItemsActivity.this,intentname);
prevIntent.putExtra("ip", ip);
prevIntent.putExtra("dbname", dbname);
prevIntent.putExtra("dbpass", dbpass);
dbI.close();
startActivity(prevIntent);
}
dilimeter = result.toString().split("!");
//to save all items in table
try {
dbI.open();
dbI.insertItem( dilimeter,dilimeter.length);
dbI.close();
} catch(Exception ex){
Intent prevIntent=new Intent(GetItemsActivity.this,Classname);
prevIntent.putExtra("ip", ip);
prevIntent.putExtra("dbname", dbname);
prevIntent.putExtra("dbpass", dbpass);
dbI.close();
startActivity(prevIntent);
ex.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
if(pDialog != null){
pDialog.dismiss();
}
Toast.makeText(getApplicationContext(), "Your work Done! ", Toast.LENGTH_LONG).show();
Intent prevIntent=new Intent(GetItemsActivity.this,intentname);
prevIntent.putExtra("ip", ip);
prevIntent.putExtra("dbname", dbname);
prevIntent.putExtra("dbpass", dbpass);
startActivity(prevIntent);
}
}
bringData 方法从服务器获取数据:
public String bringData(String url,String jsonValue, String jsonValue1) throws JSONException {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
JSONObject json = new JSONObject();
try {
json.put("dbname",jsonValue);
json.put("dbpass", jsonValue1);
JSONArray postjson=new JSONArray();
postjson.put(json);
// Post the data:
httppost.setHeader("json",json.toString());
httppost.getParams().setParameter("jsonpost",postjson);
// Execute HTTP Post Request
System.out.print(json);
HttpResponse response = httpclient.execute(httppost);
// for JSON:
if(response != null) {
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
// String item=null;
try {
while ((line = reader.readLine()) != null) {
f=line;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return f;
}
LogCat错误:
01-14 03:41:43.306: E/AndroidRuntime(2430): FATAL EXCEPTION: AsyncTask #1
01-14 03:41:43.306: E/AndroidRuntime(2430): java.lang.RuntimeException: An error occured while executing doInBackground()
01-14 03:41:43.306: E/AndroidRuntime(2430): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-14 03:41:43.306: E/AndroidRuntime(2430): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-14 03:41:43.306: E/AndroidRuntime(2430): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-14 03:41:43.306: E/AndroidRuntime(2430): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-14 03:41:43.306: E/AndroidRuntime(2430): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-14 03:41:43.306: E/AndroidRuntime(2430): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-14 03:41:43.306: E/AndroidRuntime(2430): at java.lang.Thread.run(Thread.java:841)
01-14 03:41:43.306: E/AndroidRuntime(2430): Caused by: java.lang.NullPointerException
01-14 03:41:43.306: E/AndroidRuntime(2430): at SOA.com.GetItemsActivity$LoadAllProducts.doInBackground(GetItemsActivity.java:439)
01-14 03:41:43.306: E/AndroidRuntime(2430): at SOA.com.GetItemsActivity$LoadAllProducts.doInBackground(GetItemsActivity.java:1)
01-14 03:41:43.306: E/AndroidRuntime(2430): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-14 03:41:43.306: E/AndroidRuntime(2430): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-14 03:41:43.306: E/AndroidRuntime(2430): ... 3 more
01-14 03:41:49.166: I/Choreographer(2430): Skipped 68 frames! The application may be doing too much work on its main thread.
01-14 03:41:49.416: I/Choreographer(2430): Skipped 64 frames! The application may be doing too much work on its main thread.
01-14 03:41:50.556: I/Choreographer(2430): Skipped 292 frames! The application may be doing too much work on its main thread.
01-14 03:41:51.316: I/Choreographer(2430): Skipped 129 frames! The application may be doing too much work on its main thread.
虽然我尝试了一些建议的解决方案(但没有机会和我一起运行!),它们是:
为intent设置以下标志:
覆盖 onDestroy()和onPause()方法。
在多个类中调用相同的Activity时是否有任何问题?
任何帮助将不胜感激, ......
答案 0 :(得分:0)
谢谢大家给我建议我的问题。最后我得到了解决方案。
错误原因: IP地址值为空!我解决了这个问题,然后一切顺利!确定在这个杀手错误浪费了两个多小时之后:( !!