Error when dismissing ProgressDialog - E/ViewRootImpl: sendUserActionEvent() mView == null - java

Apologies, I know this question has been asked a couple times.
But I've tried the suggested solution and have had no success.
In my Android app, I started randomly getting this error out of nowhere, I didn't modify the code, it ran fine a couple of times, and now it shows me this error:
`E/ViewRootImpl: sendUserActionEvent() mView == null`
It happens when I call my java class where I set up a BluetoothConnectionService. Specifically, it occurs when the method to dismiss a Progress Dialog box is called.
`public ConnectedThread(BluetoothSocket mSocket) {
Log.d(TAG, "ConnectedThread: Starting");
mmBTSocket = mSocket;
InputStream mTempIn = null;
OutputStream mTempOut = null;
// dismiss the progressdialog when the connection is established.
try{
mProgressDialog.dismiss();
} catch (NullPointerException e) {
Log.e(TAG, "ConnectedThread: Couldn't dismiss progressDialogBox" + e.getMessage(), e);
}
try {
mTempIn = mmBTSocket.getInputStream();
mTempOut = mmBTSocket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "ConnectedThread: Failed to get I/O Stream: " + e.getMessage(), e);
}
mInStream = mTempIn;
mOutStream = mTempOut;
}`
In the Dialog.java file, I think it is occurring in this method.
`#Override
public void dismiss() {
if (Looper.myLooper() == mHandler.getLooper()) {
dismissDialog();
} else {
mHandler.post(mDismissAction);
}
}`
Previous suggestions for this error which I have found here on stackoverflow, haven't worked. I have tried adding the following code to my AndroidManifest which didn't work:
` android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenLayout|screenSize"
android:label="#string/app_name"`
Please, any suggestions would help as I had the app mostly finished before this started happening and I can't figure out why!!

You need to check if your context is getting null. This mostly happens when context or view to which you are pointing becomes null. Just try to implement following checks before calling that:
if ( getContext() != null && getView != null )
{
// do your stuff here
}
Also you need to update views on main thread. If you are not on a main thread then you should implement handler to update your UI components. Visit following link for details:
https://developer.android.com/training/multiple-threads/communicate-ui
Do the following:
Handler handler = new Handler(); // write in onCreate function
handler.post(new Runnable() {
#Override
public void run()
{
// Update your UI components here
}
});

Related

Threads at onCreate() method execute before setting the view

I'm creating my splash screen for app. While loading it executes 4 methods. First one checks if Internet permission is granted, second one sends request to API to check if it is Online, third one is getting Token from Firebase and the fourth one is checking if user is already logged-in. I'm doing it using 4 threads. Each method in case of error sets the flag as false. Then when all the threads end their work (I used .join()) The last method checks the state of flag and launch new activity or just display Error and try everything once again.
The problem I have is that I'm getting the view after all the threads finish their work. For example I have black screen, then message ("Error occured") and only after that I can see UI. But on Error the UI is refreshed, so one more time I have black screen, then result and UI for 1sec until another restart.
My question is, can I in some way stop these Threads until my UI is ready ?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.activity_splash);
checkProgress = findViewById(R.id.checkProgressText);
auth = FirebaseAuth.getInstance();
tokenUtils = new TokenUtils();
requestQueue = Volley.newRequestQueue(getApplicationContext());
animatedCircleLoadingView = findViewById(R.id.circle_loading_view);
//starting the animation
startLoading();
Thread[] checkers = new Thread[4];
checkers[0] = new Thread(this::checkInternetPermissions);
checkers[1] = new Thread(this::checkConnection);
checkers[2] = new Thread(this::getUserAuth);
checkers[3] = new Thread(this::getUserToken);
for (Thread t : checkers) {
try {
t.start();
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
changeActivity();
}
Check internet permission method:
private void checkInternetPermissions() {
checkProgress.setText(getString(R.string.check_internet_permissions_text));
if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET)
!= PackageManager.PERMISSION_GRANTED)
requestPermissions(new String[]{Manifest.permission.INTERNET}, 1);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode != 1) {
connectionFlag = false;
}
}
Check connection method:
private void checkConnection() {
checkProgress.setText(getString(R.string.checking_api_connection));
RequestFuture<String> requestFuture = RequestFuture.newFuture();
StringRequest request = new StringRequest
(Request.Method.GET, API_CHECK,
requestFuture,
requestFuture);
requestQueue.add(request);
String response = null;
try {
response = requestFuture.get(5, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
this.connectionFlag = false;
}
if (!Objects.equals(response, "ok"))
this.connectionFlag = false;
}
Get user token method:
private void getUserToken() {
checkProgress.setText(getString(R.string.getting_user_auth_token));
String token = null;
try {
token = tokenUtils.getFirebaseToken();
} catch (ExecutionException | InterruptedException e) {
this.connectionFlag = false;
}
if (Objects.isNull(token) || Objects.requireNonNull(token).isEmpty())
this.connectionFlag = false;
}
And finally get user auth method:
private void getUserAuth() {
checkProgress.setText(getString(R.string.checking_user_auth));
authStateListener = firebaseAuth -> {
firebaseUser = firebaseAuth.getCurrentUser();
if (Objects.isNull(firebaseUser) || Objects.requireNonNull(firebaseUser.getEmail()).isEmpty()) {
this.authFlag = false;
}
};
}
Last method which handle the states of flags:
private void changeActivity() {
checkProgress.setText(getString(R.string.finalizing_text_progress));
if (connectionFlag && authFlag) {
startActivity(new Intent(SplashActivity.this, MapActivity.class));
} else if (!connectionFlag) {
Toast.makeText(getApplicationContext(), "Error occurred.", Toast.LENGTH_LONG).show();
finish();
startActivity(getIntent());
} else {
startActivity(new Intent(SplashActivity.this, LoginActivity.class));
}
}
Yes, You can try it with handler thread with some delay then it will work fine or you can start your thread on onResume() method at the time of onResume your view will have been created
I think, your way wrong. Because, API request working on asynchronous. Your app should run like this;
Check Internet connection.
API Request.
Get token in API Request onSuccess method.
Get User Auth.
I think, you shouldn't use Thread.

In android studio, how to prevent errors with buffered reader reading from a url, when the phone has no internet

In my app, when I press a button, a buffered reader should read a line of a text from a text file online.
As a test, if the text is read correctly, I want a toast to appear saying "success". If the read fails, such as because the phone has no connection to the internet, I want a toast to appear saying "failed".
However, if I turn on airplane mode, and then press the button, it simply seems to "hang" forever, and the "failed" toast never appears -- or it just crashes the app entirely.
This is the code I am using:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new NotePadFileFromServer().execute();
}
});
public class NotePadFileFromServer extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
try {
url = new URL(TextFileURL);
bufferReader = new BufferedReader(new InputStreamReader(url.openStream()));
TextHolder = bufferReader.readLine();
bufferReader.close();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Fail!", Toast.LENGTH_SHORT).show();
}
return null;
}
#Override
protected void onPostExecute(Void finalTextHolder) {
Toast.makeText(MainActivity.this, "Success!", Toast.LENGTH_SHORT).show();
super.onPostExecute(finalTextHolder);
}
}
I tried adding in a pre-check using ConnectivityManager to test if there is an internet connection as per this code: https://stackoverflow.com/a/58146646/4250107, but that only works if the phone user has specifically turned off the internet, and the crashes occur again if the wifi function is turned on, but there is no internet. I then tried checking the internet connection, as per this code: https://stackoverflow.com/a/58146896/4250107, but this also crashes the app, as apparently (?) attempting to ping a server does not work on Samsung phones.
EDIT: Final fixed code.
public class NotePadFileFromServer extends AsyncTask<Void, Void, String>{
#Override
protected String doInBackground(Void... params) {
try {
URLConnection url = new URL(TextFileURL).openConnection());
url.setConnectTimeout(1000);
url.setReadTimeout(1000);
bufferReader = new BufferedReader(new InputStreamReader(url.getInputStream()));
TextHolder = bufferReader.readLine();
bufferReader.close();
return "Success!";
} catch (Exception e) {
return "Fail!";
}
}
#Override
protected void onPostExecute(String success) {
Toast.makeText(MainActivity.this, success, Toast.LENGTH_SHORT).show();
super.onPostExecute(success);
}
}
The app is crashing because you are trying to perform UI related task in the Background Thread when there is an exception. So, the following is responsible for the crash,
catch (Exception e) {
Toast.makeText(MainActivity.this, "Fail!", Toast.LENGTH_SHORT).show();
}
So, you can avoid the crash by refactoring you code in the following way,
public class NotePadFileFromServer extends AsyncTask<Void, Void, String>{
#Override
protected String doInBackground(Void... params) {
try {
url = new URL(TextFileURL);
bufferReader = new BufferedReader(new InputStreamReader(url.openStream()));
TextHolder = bufferReader.readLine();
bufferReader.close();
return "Success!";
} catch (Exception e) {
return "Fail!";
}
}
#Override
protected void onPostExecute(String finalTextHolder) {
Toast.makeText(MainActivity.this, finalTextHolder, Toast.LENGTH_SHORT).show();
super.onPostExecute(finalTextHolder);
}
}
And in case of timeout issue which you described here as hang, I would recommend you to use openConnection() (which returns a UrlConnection) instead of openStream(). So that you can set shorter connection and read timeout.
Yes, as you say ConnectivityManager will not help you because if you have wifi but no internet it will crash.
However, it is possible to check internet connection. I couldn't do it with ping (same as you), but i could when i try to open a socket to some of the opened ports (80 or 443). Here is a code using rxjava but you can adapt it to what you are using.
fun isOnline(context: Context?): Single<Boolean> {
return Single.fromCallable {
try {
// Connect to Google DNS to check for connection
val timeoutMs = 2500
val socket = Socket()
val address = InetAddress.getByName("www.google.com")
val socketAddress = InetSocketAddress(address, 443)
socket.connect(socketAddress, timeoutMs)
socket.close()
true
} catch (e: Exception) {
false
}
}.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
In my case i opened the socket with my backend so also i can check if it is working. I put www.google.com in case you don't have a backend.
The way to use it is:
isOnline(context).subscribe { hasInternet ->
//Conditional check
}

Toasts are shown not simultaneously

I have a part of code in which I firstly setText, then make a Toast and after that I'm trying to connect via Bluetooth. The problem is that my setText and Toasts appear only after connection has been made.
I tried to put Log.i instead of Toasts and they were shown simultaneously.
Can somebody explain me why and how to make Toasts simultaneously?
Code:
........
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
tvDevices.setText("");
Toast.makeText(getApplicationContext(), "Lost connection!", Toast.LENGTH_SHORT).show();
connect(btDevice, ConstantsVariables.reconnectionAttempts);
}
public void connect(BluetoothDevice bt, int attempts){
Toast.makeText(getApplicationContext(), "Trying to connect...", Toast.LENGTH_SHORT).show();
if(attempts > 0){
for(int i = 1; i <= ConstantsVariables.reconnectionAttempts; i++){
ConnectThread thread = new ConnectThread(bt);
boolean connectVar = thread.connect();
if(connectVar){
break;
}
}
}
}
.......
public boolean connect() {
BA.cancelDiscovery();
try {
mSocket.connect();
} catch (IOException e) {
Log.d("CONNECTTHREAD","Could not connect: " + e.toString());
try {
mSocket.close();
} catch (IOException exception){}
return false;
}
return true;
}
It is possible that you're blocking the UI Thread while connection is being attempted. Try to move the connection code to a background thread, or an AsyncTask, and handle the UI Changes in the AsyncTask's callbacks.
Edit: Also the context getApplicationContext() passed to Toast is ambiguous. Are you in an activity? In that case it should simply point to the Activity's context i.e. this and not the Application's context

How to set time limit for waiting internet Connection in android AsyncTask?

I have use The AsyncTask For connecting Internet. a Progress dialog can show at onPreExecute() and i check is Online of that mobile if yes means it will execute the http connections code also dismiss the progress dialog at onPostExecute() and its work as good.
But i have problem if net connection is available at time of Request and connection closed before get Response means the Progress dialog showing always.
Now i want solve this if interconnect disconnect before get Response mean it's alert me as No internet connection and dismiss the progress dialog (may set loading time limit for 30 seconds).
below is my code.
can any one help?
public class SubjectTask extends AsyncTask<Void, Void, Integer> {
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(Login.this, "Loading",
"Please wait...");
//checkConnection();
}
#Override
protected Integer doInBackground(Void... arg0) {
if (isOnline()) { //using ConnectivityManager And Network Info
try {
//Http Request connections
} catch (Exception e) {
e.printStackTrace();
}
return 1;
} else {
alert("Check your internet connection");
return 0;
}
}
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
You can use broadcast receiver to get the event of network connect or disconnect. register this receiver in doInbackground method and unregister it in onPostExecute method.
broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivity.getActiveNetworkInfo();
//Play with the info about current network state
if(info.getState()== NetworkInfo.State.DISCONNECTED) {
// hide loader and show alert here
}
}
}
};
intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
If the connection will be closed before or while the http request an IOException will be thrown. Catch that exception and close your Dialog there and inform the user about this event.
if (isOnline()) { //using ConnectivityManager And Network Info
try {
//Http Request connections
} catch (IOException e) {
e.printStackTrace();
// Do the error handling here
} catch (ClientProtocolException e) {
e.printStackTrace();
}
}
I don't know if you really need a return value to your onPostExecute(). If yes consder to make the logic aware that an exception could occur.
i thank to Steve Benettand Bhawna Raheja,
sorry for the late reply,
Simply i have solve this error by set Timeout via HttpRequest.TimeOut .
i dont know how i forgot to set time out.
One again thank to You both.

android Java - Textview not appending text when activity restarts

I've been trying to create a function in my app that consist in a bluetooth RFID scanner, it's paired to my device and I have it working and all.
I can receive the text and log it in the console, when I compile the activity, everything goes fine, the stick reads the code, and then appends the text into an EditText, but if I go back and enter the activity again, I can see the code in the log, but the text doesn't go to the Edittext.
I tried a lot of different approaches, but nothing seems to work :/
here's the code I have:
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetooth);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> bondedSet = mBluetoothAdapter.getBondedDevices();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available.", Toast.LENGTH_LONG).show();
}
if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this, "Please enable your BT and re-run this program.", Toast.LENGTH_LONG).show();
finish();
}
if (mBluetoothAdapter.isEnabled()) {
if(bondedSet.size() == 1){
for(BluetoothDevice device : bondedSet){
address = device.getAddress();
Log.d("bt:", address);
}
}
}
String address = "00:A0:96:2A:0A:1B";
out = (EditText) findViewById(R.id.output);
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
Log.d(TAG, device.getName() + " connected");
myConnection = new ConnectThread(device);
myConnection.start();
}
private class ConnectThread extends Thread {
private final BluetoothSocket mySocket;
Message msg;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.d(TAG, "CONNECTION IN THREAD DIDNT WORK");
}
mySocket = tmp;
}
Handler uiThreadHandler = new Handler() {
public void handleMessage(Message msg) {
out = (EditText) findViewById(R.id.output);
Object o = msg.obj;
out.append(o.toString().trim());
Log.d("handler", o.toString());
}
};
public void run() {
out = (EditText) findViewById(R.id.output);
Log.d(TAG, "STARTING TO CONNECT THE SOCKET");
setName("My Connection Thread");
InputStream inStream = null;
boolean run = false;
mBluetoothAdapter.cancelDiscovery();
try {
mySocket.connect();
run = true;
} catch (IOException e) {
Log.d(TAG, this.getName() + ": CONN DIDNT WORK, Try closing socket");
try {
mySocket.close();
Log.d(TAG, this.getName() + ": CLOSED SOCKET");
} catch (IOException e1) {
Log.d(TAG, this.getName() + ": COULD CLOSE SOCKET", e1);
this.destroy();
}
run = false;
}
synchronized (BluetoothActivity.this) {
myConnection = null;
}
byte[] buffer = new byte[1024];
int bytes;
// handle Connection
try {
inStream = mySocket.getInputStream();
while (run) {
try {
bytes = inStream.read(buffer);
readMessage = new String(buffer, 0, bytes);
msg = uiThreadHandler.obtainMessage();
msg.obj = readMessage;
uiThreadHandler.sendMessage(msg);
Log.d(TAG, "Received: " + readMessage);
} catch (IOException e3) {
Log.d(TAG, "disconnected");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
My guess is that this has something to do with the Thread itself. When you start your Activity for the first time, you also call .start() on the Thread, that would work fine.
The problem is when you leave your Activity and open it up again. In that case, one of onStop() or onPause() is called (depending on situation), and onRestart() or onResume() will be called afterwards respectively.
The trick comes now: Meanwhile all that process, your Thread is still running. As you show your code, it has not been stopped/paused, and keeps running all the time. So basically my tip is that there's something you do within your onCreate() method of your Activity that should also be done in your onPause() and onStop() events, and my another tip it's somewhere within your ConnectThread(BluetoothDevice device) method.
To know how to procceed, I'd firstly define both onStop() and onPause() methods within your Activity and see which is fired, log every attribute to see its value/state, and that way you'll be able to debug what is failing.
There's a diagram of the Activity lifecycle.
Problem was solved, the code works, and the TextView get the inputstream, the problem was when i left the activity, the thread continued to work, so far, no problem at all, after TONS of hours spent on this, i turn the TextView a static var and it worked :)
If anyone reads this, i hope it helps.

Categories