继承我的logcat错误strace
06-17 10:50:59.578: E/AndroidRuntime(1899): FATAL EXCEPTION: AsyncTask #1
06-17 10:50:59.578: E/AndroidRuntime(1899): java.lang.RuntimeException: An error occured while executing doInBackground()
06-17 10:50:59.578: E/AndroidRuntime(1899): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-17 10:50:59.578: E/AndroidRuntime(1899): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-17 10:50:59.578: E/AndroidRuntime(1899): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-17 10:50:59.578: E/AndroidRuntime(1899): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-17 10:50:59.578: E/AndroidRuntime(1899): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-17 10:50:59.578: E/AndroidRuntime(1899): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-17 10:50:59.578: E/AndroidRuntime(1899): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-17 10:50:59.578: E/AndroidRuntime(1899): at java.lang.Thread.run(Thread.java:856)
06-17 10:50:59.578: E/AndroidRuntime(1899): Caused by: java.lang.NullPointerException
06-17 10:50:59.578: E/AndroidRuntime(1899): at com.merimasjid.prayertimes.hijri.FavouritePagePrayerTimes1$LoadTimes.doInBackground(FavouritePagePrayerTimes1.java:561)
06-17 10:50:59.578: E/AndroidRuntime(1899): at com.merimasjid.prayertimes.hijri.FavouritePagePrayerTimes1$LoadTimes.doInBackground(FavouritePagePrayerTimes1.java:1)
06-17 10:50:59.578: E/AndroidRuntime(1899): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-17 10:50:59.578: E/AndroidRuntime(1899): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-17 10:50:59.578: E/AndroidRuntime(1899): ... 4 more
我有一个单独的线程,当应用程序启动时自动执行,并且此线程在片段内定义。该线程从互联网上抓取一些JSON数据并将其显示在屏幕上,当数据连接或wifi打开时,应用程序正常工作但是在没有数据包数据的情况下它会在启动时崩溃应用程序
class LoadTimes extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
//pBar.setVisibility(View.VISIBLE);
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// params.add(new BasicNameValuePair("areaID","0"));
try{
params.add(new BasicNameValuePair("MasjidID",String.valueOf(_appPrefs.getFavOneID())));
params.add(new BasicNameValuePair("Date", getParameterisedDate()));
//params.add(new BasicNameValuePair("date",getParameterisedDate()));
}catch(Exception e){}
JSONObject json = null;
try{
json = jsonParser.makeHttpRequest(URL_GET_TIMES, "GET",
params);
}catch(Exception e){}
// Check your log cat for JSON response
Log.d("Outbox JSON: ", json.toString());
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
try {
albums = json.getJSONArray("masjids");
// looping through All messages
if(albums!=null){
for (int i = 0; i < albums.length(); i++) {
JSONObject c = albums.getJSONObject(i);
Date d1 = df.parse(c.getString("FajarAzan"));
SunrH = d1.getHours();
SunRM = d1.getMinutes();
Date d2 = df.parse(c.getString("FajarAzan"));
fajrH = d2.getHours();
fajrM = d2.getMinutes();
Date d3 = df.parse(c.getString("ZoharAzan"));
DhuhrH = d3.getHours();
DhuhrM = d3.getMinutes();
Date d4 = df.parse(c.getString("AsarAzan"));
AsrH = d4.getHours();
AsrM = d4.getMinutes();
Date d5 = df.parse(c.getString("Maghrib"));
MaghribH = d5.getHours();
MaghribM = d5.getMinutes();
Date d6 = df.parse(c.getString("IshaAzan"));
IshaH = d6.getHours();
IshaM = d6.getMinutes();
_appPrefs.storeFavouriteMasjidTimes(fajrH, fajrM, SunrH, SunRM, DhuhrH, DhuhrM, AsrH, AsrM, MaghribH, MaghribM, IshaH, IshaM, day, month, year, _appPrefs.getFavOneID());
try{
initNamazTimes();
}catch(Exception e){}
/*
namazTimesH = new int[]{fajrH,SunrH,DhuhrH,AsrH,MaghribH,IshaH};
namazTimesM = new int[]{fajrM,SunRM,DhuhrM,AsrM,MaghribM,IshaM};*/
}
}
} catch (Exception e) {
// pBar.setVisibility(View.GONE);
//
// cantGet = true;
publishProgress(null);
Log.e("Exception while Loading Times",e.toString());
}
return null;
}
@Override
public void onProgressUpdate(String... args) {
super.onProgressUpdate(args);
try{
FetchStoreFavouriteNamazTimings.cantGetNamazTimes();}catch(Exception e){}
}
protected void onPostExecute(String file_url) {
if(cantGet)
{
FetchStoreFavouriteNamazTimings.cantGet = false;
}
else{
try{
generateButtonsAlarms();
generateButtonsFavourites();}
catch(Exception e){}
/////////////////////////////////////////////////////////////////////
}
}
}
答案 0 :(得分:3)
JSONObject json = null;
try{
json = jsonParser.makeHttpRequest(URL_GET_TIMES, "GET", params);
}catch(Exception e){
// handle me!
}
你说没有互联网连接,所以可能会引发你没有处理过的例外情况。稍后您将访问明确json
的{{1}}对象。
<强>更新强>
大多数其他答案建议在执行AsyncTask之前检查互联网连接。但这不是一个完整的解决方案,因为在&#34;检查&#34;之后连接可能会丢失。在网络呼叫之前,这将导致相同的异常。
您必须处理异常!检查互联网连接无法解决问题。
答案 1 :(得分:2)
再创建一个ConnectionDetector类
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
然后从您的活动中首先声明
ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
在调用AsyncTask之前检查互联网连接
Boolean isInternetPresent = cd.isConnectingToInternet(); // true or false
if (isInternetPresent)
{
// call your AsyncTask
}
else
// display a toast message saying "No internet connection"
也不要忘记捕捉异常。有时互联网会在获取信息时崩溃。最好赶上例外。
答案 2 :(得分:0)
请在调用班级的.execute()方法之前检查互联网连接。
尝试使用此方法并在if条件下调用此函数,如果返回true,则调用您的Asyncktask类,否则显示一些警告消息。
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
答案 3 :(得分:0)
您可以验证手机是否与wifi连接
public static Boolean isWifiAvailable(Context context)
{
ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return networkInfo.isConnected();
}
不要忘记在清单中添加适当的权利:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">