1

在这里,基本上我所做的就是检查连接是否可用。如果连接不可用,我将重定向到另一个活动。就是这样。但它一直在崩溃。

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

当程序执行时,我正在调用 isDataConnectionAvailable()并且我没有可用的连接。因此根据逻辑它必须被重定向并且应该打开 NoInternet 活动。
我错过了什么严重的事情吗?
请帮忙。

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]  
4

3 回答 3

1

检查 isDataConnectionAvailable() 作为构造 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");
}
于 2013-04-04T11:46:03.617 回答
0

您应该检查 isDataConnectionAvailable() 作为构造if (isDataConnectionAvailable())

我认为最好的方法是不检查连接,而是检查doc,如果它等于 null,然后打开另一个活动

于 2013-04-04T11:32:14.303 回答
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)

于 2013-04-04T11:33:15.447 回答