简体   繁体   中英

App crashing when trying to check network connection

I have just started doing some android programming, as I downloaded the AIDE for my phone. I am trying to make an app which requires a connection to a website, but every time I run the app and try any network related operations, it just crashes.

I have set the permissions for INTERNET and ACCESS_NETWORK_STATE in the manifest file.

I would show a log of the error, but i am unaware about how to do this with the AIDE app, or on an android device, as I don't have access to proper IDE on a computer.

Help would be appreciated

public class MainActivity extends Activity{

    private TextView text;

    @Override
    public void onCreate ( Bundle savedInstanceState ){
        super onCreate( savedInstanceState );
        setContextView( R.layout.main );
        textView = ( TextView )findViewById( R.id.myText );
    }

    public void checkConn(){
         ConnectivityManager connMgr = ( ConnectivityManager ) getSystemService ( Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr getActiveNetworkInfo();
    if ( networkInfo != null && networkInfo.idconnected() ){ 
        textView.setText( "conection" );
    }else{
        textView.setText( "no connection" );
    }

EDIT: I fixed the check connection issue, but now I added the method to connect to my desired website, and now my app keeps crashing again.

private void makeConnection(){
  try{
  URL url = new URL("website");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.connect();
  }catch (MalformedURLException e) {
     textView.setText( "no connection" );
  }catch ( IOExcpetion e ){
     textView.setText( "no connection" );
  }
}

Try this:

MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main );
        textView = ( TextView )findViewById( R.id.myText );

        checkConn();
    }

    public void checkConn(){
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            textView.setText( "conection" );
         }else{
             textView.setText( "no connection" );
         }
    }

}

main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.SPTechnos.ambassador.Home"
    tools:ignore="MergeRootFrame" >


    <TextView 
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>



</FrameLayout>

Don't forget to add this permission in the application manifest:

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

you are define as like this

   private TextView text

And you are using as

  textView = ( TextView )findViewById( R.id.myText );

then definitely its crash. Change like this

private TextView textView;

and try to run and check...

Hope It helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM