After displaying a Snackbar, I run a Thread to execute actions. I want to change text according current progress in this Thread.
I create the Snackbar:
private View snackbarview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton upload = (FloatingActionButton) findViewById(R.id.upload);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Confirm ?", Snackbar.LENGTH_SHORT)
.setAction("YES", new View.OnClickListener() {
@Override
public void onClick(View view) {
lauchMyThread();
Snackbar snackbarconfirm = Snackbar.make(view, "Working...", Snackbar.LENGTH_INDEFINITE);
snackbarconfirm.show();
snackbarview = snackbarconfirm.getView();
}
}).show();
}
});
}
I try to update its text in run() method of Runnable() :
public void run() {
TextView tv = (TextView) snackbarview.findViewById(android.support.design.R.id.snackbar_text);
try {
tv.post(new Runnable() {
public void run() {
TextView tv = (TextView) snackbarview.findViewById(android.support.design.R.id.snackbar_text);
tv.setText("Task 1 performed.");
}
});
// and so on...
}
}
This method works for Textview, but crashes with Snackbar :
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
How to fix it?
You should start thread after
snackbarview
initialization.