2013-04-04 112 views
1

在這裏,基本上我所做的只是檢查連接是否可用。如果連接不可用,我將重定向到另一個activity.That是全部。但它不斷崩潰。沒有互聯網的應用程序崩潰

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"); 

當程序執行,我根據它必須重定向和NoInternet活動應打開邏輯調用isDataConnectionAvailable(),我沒有連接available.So。
我錯過了什麼嚴重?
請幫忙。

logcat的堆棧

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] 
+0

是什麼logcat的說? – geoffspear 2013-04-04 11:26:56

+3

你的邏輯不正確...首先你不檢查什麼'isDataConnectionAvailable()'return ...其次,如果'info'爲空,那麼你不返回false,所以'Jsoup.connect(...)'仍然被調用... – Selvin 2013-04-04 11:27:03

+1

我甚至不知道從哪裏開始,真的。這段代碼有太多的錯誤...我怎麼解釋... – 2013-04-04 11:31:28

回答

1

檢查isDataConnectionAvailable()建築如果(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"); 
} 
0

你應該檢查isDataConnectionAvailable()施工

,我認爲最好的辦法就是不要檢查連接,但檢查doc,如果它等於空,然後打開另一個活動

0

您必須調用if條件的方法,以便它不會執行

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(); 
} 

和從isDataConnectionAvailable(this)返回的值。

相關問題