0

That is a fragment of my MainActivity.kt file.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    message.text=URL("http://pboss.pozit.pl/contact.html").readText()
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}

The app is working with just plain text instead of URL("http://pboss.pozit.pl/contact.html").readText() but is crashing with the following code. I already added <uses-permission android:name="android.permission.INTERNET"/> to the mainfest.

Note: URL("http://pboss.pozit.pl/contact.html").readText() is the only edited thing in whole app.

tynn
  • 38,113
  • 8
  • 108
  • 143
Filip Wachowiak
  • 63
  • 2
  • 10
  • 5
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Dec 25 '17 at 22:57
  • 2
    Android doesn't allow synchronous requests in the UI thread. That's _probably_ why the app crashes. Use an async approach, like [Volley](https://developer.android.com/training/volley/simple.html) with its StringRequest class. – andred Dec 25 '17 at 23:17
  • I used `val policy = StrictMode.ThreadPolicy.Builder().permitAll().build() StrictMode.setThreadPolicy(policy)` for now. I know, that it is not the best solution but since what I'm doing is just experimental it is OK. Thanks a lot guys. – Filip Wachowiak Dec 25 '17 at 23:43

0 Answers0