0

I was trying to add a timer to the application that I have that checks for the presence of the internet every 3 seconds to switch to the second xml file when the connection is broken but, it gets an error every time. the error message :

E/AndroidRuntime: FATAL EXCEPTION: Timer-0
Process: com.example.friends, PID: 2221
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8530)
    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1432)
    at android.view.View.requestLayout(View.java:23224)
    at android.view.View.requestLayout(View.java:23224)
    at android.view.View.requestLayout(View.java:23224)
    at android.view.View.requestLayout(View.java:23224)
    at android.view.View.requestLayout(View.java:23224)
    at android.view.ViewGroup.removeAllViews(ViewGroup.java:5587)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:554)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
    at com.example.friends.MainActivity$2.run(MainActivity.java:51)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

this is the main activity xml :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/textView17"
    android:layout_width="404dp"
    android:layout_height="196dp"
    android:layout_marginStart="4dp"
    android:layout_marginTop="37dp"
    android:layout_marginEnd="4dp"
    android:layout_marginBottom="118dp"
    android:text="Hello world"
    android:textColor="#020202"
    android:textSize="24sp"
    app:layout_constraintBottom_toTopOf="@+id/textView18"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView18"
    android:layout_width="410dp"
    android:layout_height="71dp"
    android:layout_marginBottom="197dp"
    android:text="welcom to java"
    android:textColor="#000000"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView17" />
</androidx.constraintlayout.widget.ConstraintLayout>

When the internet is not available, I want to move to this XML :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginBottom="30dp"
    android:text="Looks like your connection is broken!"
    android:textSize="24sp"

</androidx.constraintlayout.widget.ConstraintLayout>

and this is the MainActivity class :

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Timer timer = new Timer() ;
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if(!Internet_Class.isConnected(getApplicationContext())){
setContentView(R.layout.no_internet_connection) ;
}
else{
setContentView(R.layout.activity_main) ;
}
        }
    }, 0, 3000);
            }
}

and there is class i called it (Internet_Class) here is the code :

public class Internet_Class {

public static boolean isConnected(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo() ;
    return (info != null && info.isConnected()) ;
}
}

and the manifests :

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".app_information"></activity>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

The error is that the application stops working after three seconds every time. Does anyone know where the error is or any suggestion?

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • No error messages on logcat when it crashes? Where is manifest? – Federico Provenziani Aug 04 '20 at 06:56
  • here is the message –  Aug 04 '20 at 07:10
  • 1
    You can have a look at this https://stackoverflow.com/a/5162096/9039975 – Artory Aug 04 '20 at 07:14
  • "Only the original thread that created a view hierarchy can touch its views." timer.schedule works on different thread than UI, and UI can be accessed only by its own thread. You have to use runOnUIThread method from activity class, or Handler to modify the View. – VIAX Aug 04 '20 at 07:17
  • The main view of an activity is created when the activity is instantiated by Android Framework. If you only want to show that internet is available or not, then you should use fragments and switch between those. I thing this will be much better considering your situation. – Eishon Aug 04 '20 at 15:52

0 Answers0