This question has already been answered:
Just started working with Handler. The application does not even want to run, but the error is this:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.post(java.lang.Runnable)' on a null object reference at com.example.x.threaddevelopmentpart2.MainActivity$4.run(MainActivity.java:66) at java.lang.Thread.run(Thread.java:764) Here is the java code:
package com.example.x.threaddevelopmentpart2; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ProgressBar; import android.widget.TextView; import static java.lang.Thread.sleep; public class MainActivity extends AppCompatActivity { TextView text; CheckBox checkBox; ProgressBar progressBar; int count; Handler handler; Runnable updata = new Runnable() { @Override public void run() { progressBar.setProgress(count); } }; Runnable showInfo = new Runnable() { @Override public void run() { text.setText(count + "%"); handler.postDelayed(showInfo,1000); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox = (CheckBox) findViewById(R.id.checkbox); text = (TextView) findViewById(R.id.textView); progressBar = (ProgressBar) findViewById(R.id.progressBar); progressBar.setMax(count); progressBar.setProgress(0); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if(b){ text.setVisibility(View.VISIBLE); handler.post(showInfo); } else{ handler.removeCallbacks(showInfo); } } }); Thread thread = new Thread(new Runnable() { @Override public void run() { for(count =1 ; count < 100;count++){ handler.post(updata); try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }); thread.start(); } } But xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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="com.example.x.threaddevelopmentpart2.MainActivity"> <ProgressBar android:id="@+id/progressBar" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="376dp" android:layout_height="64dp" android:layout_marginEnd="5dp" android:layout_marginStart="3dp" android:layout_marginTop="64dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <CheckBox android:id="@+id/checkbox" android:layout_width="169dp" android:layout_height="52dp" android:layout_marginBottom="331dp" android:layout_marginLeft="16dp" android:text="More information" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="279dp" android:layout_marginEnd="258dp" android:layout_marginStart="125dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </android.support.constraint.ConstraintLayout> How to fix the error?
null. - kulikovman