Why does my app crash when I try to display my ip usig Inet?

user7620446

I am trying to display my ip address using Inet but the app crashes when i do so.. do i need to add any permissions so that this does not happen? following is my code

try {
            Inet4Address ip= (Inet4Address) Inet4Address.getLocalHost();
            String s=ip.toString();
            TextView text= (TextView)findViewById(R.id.txt);
            text.setText(s);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
Rashed Zzaman

android don't give u permission to run any network related work direct in main thread so you get this Exception :

android.os.NetworkOnMainThreadException 

run your code like this:

Thread thread = new Thread(){
        public void run(){
            try {
                Inet4Address ip= (Inet4Address) Inet4Address.getLocalHost();
                String s=ip.toString();

             TextView text= (TextView)findViewById(R.id.txt);
              text.setText(s);
                Log.e("my ip=> ",s+" <<");
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    };

    thread.start();

it's just run you code another thread...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my app crash when I change my namespace?

Why does my activity crash when I try to get a number?

Why does my Android app crash when my threads exit?

why my app crash when i try to post some data into web service?

Why does my C program crash when I try to realloc() a pointer array of structs?

Why does my app crash when an NSPanel is closed by pressing Escape?

Why does my app crash when firebase rules are set to true?

Why does NavigationLink make my app crash when there are no errors?

Why does my Android studio App crash when I spam click at the beginning of an activity?

why my app crash when i change its layout?

Why my app crash when I switch fragments fast?

Why is my session not working when I try to count IP hits?

Why does my program crash when I increment a pointer and then delete it?

Why does my computer crash when I minimise the last window?

java: Why does my app crashed when i try to restart a thread?

why does my app crashes when i try adding a radio button to the radio group?

Why does pressing the button crash my app?

Why does my Xcode Swift app crash when I make a call to webView.load when I use a Development build?

Why does my program break when I try to refresh the usestate?

Why does my clickListener make my app crash only the second time i call it

Why does my app crash in my mobile phone but not on AVD emulator?

why my android studio app crash when i send data to my firebase

Why my QTextEdit inherited rich text editor always crash when I try to call textCursor()?

Why does my WPF application crash when I bump my mousewheel?

My app crash when try to pass data from an activity to a fragment

Why does my Bootstrap Modal window doesn't work on php? When I try to open the modal window it won't display

Why does my express router crash only when i send a JSON and not when i send a text ?

Why does my RSA key look different on my server than when I try to connect?

Why does my AngularJS app not work when I add routing?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  7. 7

    Do Idle Snowflake Connections Use Cloud Services Credits?

  8. 8

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  9. 9

    Binding element 'string' implicitly has an 'any' type

  10. 10

    BigQuery - concatenate ignoring NULL

  11. 11

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  12. 12

    In Skype, how to block "User requests your details"?

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Pandas - check if dataframe has negative value in any column

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Generate random UUIDv4 with Elm

  17. 17

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  18. 18

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  19. 19

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  20. 20

    EXCEL: Find sum of values in one column with criteria from other column

  21. 21

    How to use merge windows unallocated space into Ubuntu using GParted?

HotTag

Archive