Implementing a chat in Android Studio

Hey there,

So I tried doing a chat on my Application, and whenever the ChatApplication is openning the app crashes, I used as model the “Real Time Chat” from Backenless’s Code Generator.

I’m just asking for help because I really don’t understand the problem here.

Here’s my code:

package ninja.engineer.contacts2019;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.backendless.Backendless;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;
import com.backendless.messaging.MessageStatus;
import com.backendless.rt.messaging.Channel;

public class ChatActivity extends AppCompatActivity {

public static final String TAG = “ChatTest”;
private EditText message;
private TextView messages;
private Channel channel;
private String color = ColorPicker.next();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);

message = findViewById(R.id.message);
messages = findViewById(R.id.messages);

final String name = getIntent().getStringExtra(“name”);

channel = Backendless.Messaging.subscribe(ApplicationClass.DEFAULT_CHANNEL);
channel.addJoinListener(new AsyncCallback<Void>() {
@Override
public void handleResponse(Void response) {
Backendless.Messaging.publish(ApplicationClass.DEFAULT_CHANNEL,
wrapToColor(name) + " joined the chat",
new AsyncCallback<MessageStatus>()
{
@Override
public void handleResponse(MessageStatus response) {
Log.d(TAG, "Sent joined " + response);
}

@Override
public void handleFault(BackendlessFault fault) {
ChatActivity.this.handleFault(fault);
}
});
}

@Override
public void handleFault(BackendlessFault fault) {
ChatActivity.this.handleFault(fault);
}
});

channel.addMessageListener(new AsyncCallback<String>() {
@Override
public void handleResponse(String response) {
messages.append(Html.fromHtml("<br/>" + response));
}

@Override
public void handleFault(BackendlessFault fault) {
ChatActivity.this.handleFault(fault);
}
});

message.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;

if (actionId == EditorInfo.IME_ACTION_SEND || event.getKeyCode() == KeyEvent.KEYCODE_ENTER){

message.setEnabled(false);

Backendless.Messaging.publish(ApplicationClass.DEFAULT_CHANNEL, wrapToColor("[" + name + “]”) +
": " + message.getText().toString(), new AsyncCallback<MessageStatus>() {
@Override
public void handleResponse(MessageStatus response) {

Log.d(TAG, “Sent message " + response);
message.setText(”", TextView.BufferType.EDITABLE);
message.setEnabled(true);
}

@Override
public void handleFault(BackendlessFault fault) {
message.setEnabled(true);
}
});
handled = true;
}
return handled;
}
});

}
private void handleFault(BackendlessFault fault) {
Log.e(TAG, fault.toString());
}
private String wrapToColor(String value) {
return “<font color=’” + color + “’>” + value + “</font>”;
}

@Override
protected void onDestroy() {
super.onDestroy();

if (channel != null)
channel.leave();
}
}

Thank you! Any help is appreciated :slight_smile:

Hi @Alexandre_Coutinho,

Are you running the code you got from the code generator? Or perhaps you made some changes to it?

Regards,
Mark

Hi Mark,

I didn’t change anything really, but in my code this line was unused:
import chat.messaging.rt.backendless.com.realtimechat.R;

Thank you,
Alex

I didn’t change anything, but I used the code on my code.

Thank you for confirming. We will test it on our end and let you know the result.

For tracking purposes the internal trouble ticket is BKNDLSS-18415

Alright Mark, thanks for the time!

I just generated the chat project and was able to run just fine:

I suspect the problem is somewhere in your code. Try to use the debugger to see where the crash happens.

Well, when I use the generated code I can run it aswell, but as I put the chat together with my app it just doesn’t work.

As I ran the debugger and entered the chatApplication, the crash gives me this in the console:

I/cketIOConnectionManager: Socket not connected. Try to get lock
Got lock
E/AndroidRuntime: FATAL EXCEPTION: pool-5-thread-3
Process: ninja.engineer.contacts2019, PID: 24917
java.lang.NoClassDefFoundError: Failed resolution of: Lio/socket/client/IO$Options;
at com.backendless.rt.SocketIOConnectionManager.get(SocketIOConnectionManager.java:59)
at com.backendless.rt.RTClientSocketIO.subscribe(RTClientSocketIO.java:133)
at com.backendless.rt.AsynRTClient$1.run(AsynRTClient.java:19)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassNotFoundException: Didn’t find class “io.socket.client.IO$Options” on path: DexPathList[[zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/base.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_dependencies_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_resources_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_slice_0_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_slice_1_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_slice_2_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_slice_3_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_slice_4_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_slice_5_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_slice_6_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_slice_7_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_slice_8_apk.apk”, zip file “/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_slice_9_apk.apk”],nativeLibraryDirectories=[/data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.backendless.rt.SocketIOConnectionManager.get(SocketIOConnectionManager.java:59)
at com.backendless.rt.RTClientSocketIO.subscribe(RTClientSocketIO.java:133)
at com.backendless.rt.AsynRTClient$1.run(AsynRTClient.java:19)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Suppressed: java.io.IOException: No original dex files found for dex location /data/app/ninja.engineer.contacts2019-g7pph2dixIOsSCkZRocuEw==/split_lib_resources_apk.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:353)
at dalvik.system.DexFile.(DexFile.java:100)
at dalvik.system.DexFile.(DexFile.java:74)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:374)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:337)
at dalvik.system.DexPathList.(DexPathList.java:157)
at dalvik.system.BaseDexClassLoader.(BaseDexClassLoader.java:65)
at dalvik.system.PathClassLoader.(PathClassLoader.java:64)
at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:69)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:35)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:693)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:727)
at android.app.LoadedApk.getResources(LoadedApk.java:954)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2270)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5639)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

That means your project is missing the socket.io dependency. Check how our project is configured.

Sorry Mark, but how can I check that out?

Thank you mark! You always save my life <3

Keep up the great work!

Did you figure it out?

Yes I did mark, it was the socket.io dependency that was missing! Thanks!