-1

I Build my own file sharing and website app using android studio.

The app is a very simple file sharing and website app, for which I already had to make a lot of fixes. Everything was working fine but in some phone it crashed.

Oh boy wasn't I wrong

The app builds fine, and working perfectly in my phone, but in some phones it will crashed.

below is my Videostatis.java which is connected to my website using wenview

--CODE--

package com.IApps.IndianShareIt.fragment;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DownloadManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import okhttp3.Request;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.IApps.IndianShareIt.ui.callback.IconSupport;
import com.IApps.IndianShareIt.ui.callback.TitleSupport;
import com.genonbeta.TrebleShot.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import static android.content.Context.DOWNLOAD_SERVICE;
import static androidx.core.content.ContextCompat.getSystemService;

/**
 * A simple {@link Fragment} subclass.
 */
public class VideoStatus extends Fragment implements IconSupport, TitleSupport {

    public WebView mWebView;
    private Dialog loadingDialog;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case 1:{
                    webViewGoBack();
                }break;
            }
        }
    };

    public VideoStatus() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_video_status, container, false);
        mWebView = (WebView)v.findViewById(R.id.webviewVideo);

        loadingDialog = new Dialog(getActivity());
        loadingDialog.setContentView(R.layout.loading);
        loadingDialog.getWindow().setBackgroundDrawable(getActivity().getDrawable(R.drawable.round_btn));
        loadingDialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        loadingDialog.setCancelable(false);

        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        mWebView.getSettings().setBuiltInZoomControls(false);
//        mWebView.loadUrl("https://indianshareitwhatsappstatus.blogspot.com/2020/07/blog-post.html");

        mWebView.setOnKeyListener(new View.OnKeyListener(){

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == MotionEvent.ACTION_UP
                        && mWebView.canGoBack()) {
                    handler.sendEmptyMessage(1);
                    return true;
                }

                return false;
            }

        });


        mWebView.setWebViewClient(new WebViewClient());

        mWebView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        });

        if(haveNetworkConnection()){
            mWebView.loadUrl("https://indianshareitwhatsappstatus.blogspot.com/2020/07/blog-post.html");
            mWebView.setWebViewClient(new WebViewClient(){
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    loadingDialog.show();
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    loadingDialog.dismiss();
                    mWebView.setVisibility(View.VISIBLE);
                }
            });
        } else {
            mWebView.loadUrl("file:///android_asset/error.html");
            final Handler ha=new Handler();
            ha.postDelayed(new Runnable() {

                @Override
                public void run() {
                    //call function
                    if (haveNetworkConnection()) {
                        mWebView.loadUrl("https://indianshareitwhatsappstatus.blogspot.com/2020/07/blog-post.html");
                        mWebView.setWebViewClient(new WebViewClient(){
                            @Override
                            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                                super.onPageStarted(view, url, favicon);
                                loadingDialog.show();
                            }

                            @Override
                            public void onPageFinished(WebView view, String url) {
                                super.onPageFinished(view, url);
                                loadingDialog.dismiss();
                                mWebView.setVisibility(View.VISIBLE);
                            }
                        });
                        return;
                    }
                    mWebView.loadUrl("file:///android_asset/error.html");
                    ha.postDelayed(this, 1000);
                }
            }, 1000);
        }


        mWebView.setDownloadListener(new DownloadListener()
        {

            @Override


            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimeType,
                                        long contentLength) {

                DownloadManager.Request request = new DownloadManager.Request(
                        Uri.parse(url));


                request.setMimeType(mimeType);


                String cookies = CookieManager.getInstance().getCookie(url);


                request.addRequestHeader("cookie", cookies);


                request.addRequestHeader("User-Agent", userAgent);


                request.setDescription("Downloading file...");


                request.setTitle(URLUtil.guessFileName(url, contentDisposition,
                        mimeType));


                request.allowScanningByMediaScanner();


                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(
                        Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                                url, contentDisposition, mimeType));
                DownloadManager dm = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);
                Toast.makeText(getActivity().getApplicationContext(), "Downloading File Check Your Gallery When Download is Complete",
                        Toast.LENGTH_LONG).show();
            }});
        return v;
    }


    @Override
    public int getIconRes() {
        return R.drawable.ic_slow_motion_video_black_24dp;
    }

    @Override
    public CharSequence getTitle(Context context) {
        return context.getString(R.string.video_status);
    }

    private void webViewGoBack(){
        mWebView.goBack();
    }

    private boolean haveNetworkConnection() {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();

        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

}

My app has been stopped in some phones and shows this error

  • You should be able to look at the line numbers and see which lines are at fault, we can't help because we don't see which lines are which numbers. – HopefullyHelpful Jul 16 '20 at 13:42
  • Error is in 234 "NetworkInfo[] netInfo = cm.getAllNetworkInfo();" if source/exception are sync => "cm" is null, needs to be checked before "getAllNetworkInfo()" can be called. cause? don´t know, maybe app lacks permission for "(ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE)" – Michael Hauptmann Jul 16 '20 at 13:46
  • You need to include the full stack trace so that we can see not only where the exception occurs, but how it got there. – NomadMaker Jul 16 '20 at 14:30

1 Answers1

0

Watch your API-Level in your Manifest, in some cases, people have an older one than your minimum is