Unfortunately App Is Getting Stopped While Checking for Network

unfortunately app is getting stopped while checking for network

NOTE: If you're targeting android N Preview. Above receiver will not work as per constrained restricted by Google.

Link: https://developer.android.com/preview/features/background-optimization.html#connectivity-action

Use: WorkManager or JobScheduler for same.


Have you added this in AndroidManifest.xml?

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

if you have added above then check this code:

<receiver android:name=".UpdateReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>

and

public class UpdateReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();
if (isConnected)
Log.i("NET", "Connected" + isConnected);
else
Log.i("NET", "Not Connected" + isConnected);
}
}

Unfortunately MyApp has stopped. How can I solve this?

This answer describes the process of retrieving the stack trace. Already have the stack trace? Read up on stack traces in "What is a stack trace, and how can I use it to debug my application errors?"

The Problem

Your application quit because an uncaught RuntimeException was thrown.

The most common of these is the NullPointerException.

How to solve it?

Every time an Android application crashes (or any Java application for that matter), a Stack trace is written to the console (in this case, logcat). This stack trace contains vital information for solving your problem.

Android Studio

Finding the stack trace in Android Studio

In the bottom bar of the window, click on the Logcat button. Alternatively, you can press alt+6. Make sure your emulator or device is selected in the Devices panel. Next, try to find the stack trace, which is shown in red. There may be a lot of stuff logged into logcat, so you may need to scroll a bit. An easy way to find the stack trace is to clear the logcat (using the recycle bin on the right), and let the app crash again.

I have found the stack trace, now what?

Yay! You're halfway to solving your problem.

You only need to find out what exactly made your application crash, by analyzing the stack trace.

Read up on stack traces in "What is a stack trace, and how can I use it to debug my application errors?"

I still can't solve my problem!

If you've found your Exception and the line where it occurred, and still cannot figure out how to fix it, don't hesitate to ask a question on StackOverflow.

Try to be as concise as possible: post the stack trace, and the relevant code (e.g. a few lines up to the line which threw the Exception).

Unfortunately App Has Been Stopped If No Internet Access

Your code failed here:

Log.d("one", result);

Because result is null. Check this variable at the beginning of method onPostExecute() and return if it's null.

EDITED:

protected void onPostExecute(String result) {
if (result == null) {
Log.w("one", "result is null");
return;
}

// your code
}

Android Studio API 22 gives an error Unfortunately, “App Name” has stopped working after successful build

Try removing the android prefix in <item name="android:windowNoTitle">true</item> i.e replace it with <item name="windowNoTitle">true</item>.

Also replace <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> with <style name="MyMaterialTheme.Base" parent="Theme.AppCompat"> , the Light.DarkActionBar part is unnecessary as you are specifying windowActionbar as false and windowNoTitle as true and setting action bar in activity.

Also one more thing, ActionBarActivity is deprecated in revision 22.1.0 of the Support Library and so AppCompatActivity should be used instead of ActionBarActivity. This has nothing to do with issue, but is just a suggestion.

Hope this will help you resolve the issue as all other parts of your code look ok.

Internet Connectivity Change Broadcast Not Happening

Its better to check Internet Connectivity as follows :

Just make one common function in a Common utility class as

  /*
* A Common function to check internet connection.
* */
public static boolean isOnline(Context c) {
try {
ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

Now, Use it where you required internet connection in your code as below :

       if (isOnline(YourActivity.this)) {
//Your Tasks..
} else {
//Display your AlertBox..
}


Related Topics



Leave a reply



Submit