简体   繁体   English

没有互联网连接时android线程崩溃的应用程序

[英]android thread crashes application when there's no internet connection

heres my logcat error strace 这是我的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

I have a single thread which is automatically executed when the app is launched and this thread is defined inside fragment . 我有一个线程,该线程在应用程序启动时自动执行,并且该线程在fragment中定义。 The thread grabs some JSON data from the internet and diplays it on the screen and the app works fine when the data connection or wifi is on but in absence of packet data it just crashes the app when launched 该线程从互联网上获取一些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){}
            /////////////////////////////////////////////////////////////////////
        }
    }

}
JSONObject json = null; 

try{
   json = jsonParser.makeHttpRequest(URL_GET_TIMES, "GET", params);
}catch(Exception e){
   // handle me!
}

You said there is no internet connection, so probably an exception is raised that you have not handled. 您说没有互联网连接,因此可能引发了一个您未处理的异常。 Later you access the json object that is definately null . 稍后,您访问绝对为nulljson对象。

UPDATE: 更新:

Most of the other answers suggest to check internet connection before executing the AsyncTask. 其他大多数答案建议在执行AsyncTask之前先检查Internet连接。 But that is not a complete solution, since the connection could be lost after the "checking" and before the network call, that would lead to the same exception. 但这不是一个完整的解决方案,因为在“检查”之后和网络调用之前连接可能会丢失,这将导致相同的异常。

You must handle exceptions! 您必须处理异常! Checking internet connection does not solve the problem. 检查互联网连接不能解决问题。

Create a one more class ConnectionDetector 再创建一个类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;
    }
}

Then from your activity first declare 然后从您的活动中首先声明

ConnectionDetector cd = new ConnectionDetector(getApplicationContext());

Before calling AsyncTask check internet connection 调用AsyncTask之前,请检查Internet连接

Boolean isInternetPresent = cd.isConnectingToInternet(); // true or false

    if (isInternetPresent)
    {
       // call your AsyncTask
    }

    else

    // display a toast message saying "No internet connection"

Also don't forget to catch the exception . 也不要忘记捕捉异常 Sometime internet breaks down while getting information. 有时,互联网在获取信息时会崩溃。 So better to catch the exception. 因此更好地捕获异常。

Please check internet connection before calling .execute() method of your class. 调用类的.execute()方法之前,请检查Internet连接。

Try this method and call this function in if condition, if its return true then call your Asyncktask class otherwise show some alert message. 尝试此方法并在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;
    }

You could verify if the phone has a connection to the wifi 您可以验证手机是否与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();
    }

Don't forget to add the proper permision in the manifest: 不要忘记在清单中添加适当的置换:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM