Question

Here , Basically all i am doing is, checking whether the connection is available or not.If the connection is not available i am redirecting to another activity.That is all.But it keeps crashing.

Intent i;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     i = new Intent(Jsouptesting3Activity.this, NoInternet.class);
    Jsouptesting3Activity ob1=new Jsouptesting3Activity();
    ob1.isDataConnectionAvailable(this);
    try {
        doc = Jsoup.connect("http://www.way2franchise.com/").get();

        //System.out.println(doc);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("cant connect");
        e.printStackTrace();


public  void isDataConnectionAvailable(Context context){
    ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    if(info == null)
    {
        System.out.println("No net bob");
         i = new Intent(Jsouptesting3Activity.this, NoInternet.class);
        startActivity(i);

    }
    System.out.println("YES net bob");

While the program executes, i am calling isDataConnectionAvailable() and i have no connection available.So according to the logic it must be redirected and NoInternet activity should open.
Am i missing anything serious?
please help.

logcat stack

Thread [<1> main] (Suspended (exception RuntimeException))  
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1659    
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1675 
ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 121   
ActivityThread$H.handleMessage(Message) line: 943   
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 130 
ActivityThread.main(String[]) line: 3701    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 507  
ZygoteInit$MethodAndArgsCaller.run() line: 866  
ZygoteInit.main(String[]) line: 624 
NativeStart.main(String[]) line: not available [native method]  
Was it helpful?

Solution

check isDataConnectionAvailable() as construction if (isDataConnectionAvailable())

 Jsouptesting3Activity ob1=new Jsouptesting3Activity();
   if( ob1.isDataConnectionAvailable(this))
{
    try {
        doc = Jsoup.connect("http://www.way2franchise.com/").get();

        //System.out.println(doc);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("JSON Error");
        e.printStackTrace();
}
else
{
    System.out.println("cant connect");
}

OTHER TIPS

you should check isDataConnectionAvailable() as construction if (isDataConnectionAvailable())

and I think that the best way is dont check the connection, but check doc, if it equals null, then open another activity

You have to call the method from the if condition so it will not execute

if(ob1.isDataConnectionAvailable(this))
{
    try {
        doc = Jsoup.connect("http://www.way2franchise.com/").get();

        //System.out.println(doc);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("cant connect");
        e.printStackTrace();
}

and return value from isDataConnectionAvailable(this).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top