问题
My message app works fine until Admob is added and I am not sure how to solve the crash. There is nothing wrong with Admob; it works fine but the issue is that after i add it my app crashes. When commenting out Admob it works fine again. Please help Here is the crash log:
/com.app.androidchatsdk.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.androidchatsdk.app, PID: 18494
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.backendless.services.messaging.MessageStatus
at com.app.androidchatsdk.firebaseplugin.firebase.backendless.BBackendlessHandler$2.handleResponse(BBackendlessHandler.java:123)
at com.backendless.async.message.AsyncMessage$ResponseHandler.handle(AsyncMessage.java:64)
at com.backendless.async.message.AsyncMessage.handleCallback(AsyncMessage.java:41)
at com.backendless.core.AndroidCarrier$1.handleMessage(AndroidCarrier.java:37)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Here are the codes of how my code looks for admob; at the end is the code that we will be directed to when app crashes (blue link):
1.XML page
<com.google.android.gms.ads.AdView
android:id="@+id/googleAds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
here is the main activity:
public class ChatSDKMainActivity extends ChatSDKBaseActivity {
private static final String TAG = ChatSDKMainActivity.class.getSimpleName();
private static boolean DEBUG = Debug.MainActivity;
private ExitHelper exitHelper;
private PagerSlidingTabStrip tabs;
private ViewPager pager;
protected AbstractChatSDKTabsAdapter adapter;
private InterstitialAd mInterstitial;
private AdView adView;
private static final String FIRST_TIME_IN_APP = "First_Time_In_App";
public static final String PAGE_ADAPTER_POS = "page_adapter_pos";
public static final String Action_Contacts_Added = "com.app.androidchatsdk.action.contact_added";
public static final String Action_clear_data = "com.app.androidchatsdk.action.logged_out";
public static final String Action_Refresh_Fragment = "com.app.androidchatsdk.action.refresh_fragment";
private int pageAdapterPos = -1;
private OpenFromPushChecker mOpenFromPushChecker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exitHelper = new ExitHelper(this);
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
// Activity was brought to front and not created,
// Thus finishing this will get us to the last viewed activity
finish();
return;
}
setContentView(R.layout.chat_sdk_activity_view_pager);
firstTimeInApp();
initViews();
enableCheckOnlineOnResumed(true);
if (!fromLoginActivity && savedInstanceState != null) {
pager.setCurrentItem(savedInstanceState.getInt(PAGE_ADAPTER_POS));
}
mOpenFromPushChecker = new OpenFromPushChecker();
if (mOpenFromPushChecker.checkOnCreate(getIntent(), savedInstanceState)) {
startChatActivityForID(getIntent().getExtras().getLong(ChatSDKAbstractChatActivity.THREAD_ID));
return;
}
}
@Override
protected void onResume() {
super.onResume();
ChatSDKThreadPool.getInstance().execute(new Runnable() {
@Override
public void run() {
getNetworkAdapter().getEventManager().removeEventByTag(appEventListener.getTag());
getNetworkAdapter().getEventManager().addEvent(appEventListener);
tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
private int lastPage = 0;
private int refreshContactsInterval = 4000;
private long lastContactsRefresh = 0;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// if (DEBUG) Log.v(TAG, "onPageScrolled");
}
@Override
public void onPageSelected(int position) {
if (DEBUG)
Timber.v("onPageSelected, Pos: %s, Last: %s", position, lastPage);
pageAdapterPos = position;
lastPage = position;
}
@Override
public void onPageScrollStateChanged(int state) {
// if (DEBUG) Log.v(TAG, "onPageScrollStateChanged");
}
});
IntentFilter intentFilter = new IntentFilter(Action_Contacts_Added);
intentFilter.addAction(Action_clear_data);
intentFilter.addAction(Action_Refresh_Fragment);
registerReceiver(mainReceiver, intentFilter);
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (mOpenFromPushChecker == null)
mOpenFromPushChecker = new OpenFromPushChecker();
if (mOpenFromPushChecker.checkOnNewIntent(intent)) {
startChatActivityForID(intent.getExtras().getLong(ChatSDKAbstractChatActivity.THREAD_ID));
return;
}
if (adapter != null) {
ChatSDKBaseFragment pro = getFragment(AbstractChatSDKTabsAdapter.Profile), conv = getFragment(PagerAdapterTabs.Conversations);
if (conv != null)
conv.refreshOnBackground();
if (pro != null)
pro.refresh();
}
}
private AppEventListener appEventListener = new AppEventListener("MainActivity") {
private final int messageDelay = 3000;
private UIUpdater uiUpdaterMessages;
@Override
public boolean onMessageReceived(final BMessage message) {
// Only notify for private threads.
if (message.getThread().getTypeSafely() == BThread.Type.Public) {
return false;
}
// Make sure the message that incoming is not the user message.
if (message.getBUserSender().getEntityID().equals(
BNetworkManager.sharedManager().getNetworkAdapter().currentUserModel().getEntityID()))
return false;
if (uiUpdaterMessages != null)
uiUpdaterMessages.setKilled(true);
handler.removeCallbacks(uiUpdaterMessages);
uiUpdaterMessages = new UIUpdater() {
@Override
public void run() {
if (!isKilled()) {
// We check to see that the ChatActivity is not listening to this messages so we wont alert twice.
if (!getNetworkAdapter().getEventManager().isEventTagExist(ChatSDKChatActivity.MessageListenerTAG + message.getThread())) {
// Checking if the message has a sender with a name, Also if the message was read.
if (message.getBUserSender().getMetaName() != null && !message.wasRead()) {
NotificationUtils.createMessageNotification(ChatSDKMainActivity.this, message);
}
}
}
}
};
handler.postDelayed(uiUpdaterMessages, messageDelay);
return false;
}
};
static final Handler handler = new Handler(Looper.getMainLooper());
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(PAGE_ADAPTER_POS, pageAdapterPos);
mOpenFromPushChecker.onSaveInstanceState(outState);
}
private void initViews() {
pager = (ViewPager) findViewById(R.id.pager);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
// Only creates the adapter if it wasn't initiated already
if (adapter == null)
adapter = new PagerAdapterTabs(getFragmentManager());
pager.setAdapter(adapter);
tabs.setViewPager(pager);
pager.setOffscreenPageLimit(3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_chat_sdk, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.android_settings) {
// FIXME Clearing the cache, Just for debug.
/*final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
VolleyUtils.getBitmapCache().resize(1);
VolleyUtils.getBitmapCache().resize(maxMemory / 8);*/
return true;
} else if (item.getItemId() == R.id.contact_developer) {
if (StringUtils.isNotEmpty(BDefines.ContactDeveloper_Email)) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", BDefines.ContactDeveloper_Email, null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, BDefines.ContactDeveloper_Subject);
startActivity(Intent.createChooser(emailIntent, BDefines.ContactDeveloper_DialogTitle));
}
return true;
} else if (item.getItemId() == R.id.setup_guide) {
// Uri uri = Uri.parse("www.google.com");
// Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// startActivity(intent);
Uri uri = Uri.parse("https://youtu.be/lsJRFAPeM9I");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://youtu.be/lsJRFAPeM9I"));
// openVideo.setDataAndType(Uri.parse("https://www.youtube.com"), uri);
// openVideo.setDataAndType(Uri.parse(video), "video/*");
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
// If the register did not had a chance to register due to orientation change.
try {
unregisterReceiver(mainReceiver);
} catch (IllegalArgumentException e) {
// No need to handle the exception.
}
}
private void firstTimeInApp() {
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(FIRST_TIME_IN_APP, true)) {
// Creating the images directory if not exist.
Utils.ImageSaver.getAlbumStorageDir(this, BDefines.ImageDirName);
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean(FIRST_TIME_IN_APP, false).apply();
}
}
/**
* Refresh the contacts fragment when a contact added action is received.
* Clear Fragments data when logged out.
* Refresh Fragment when wanted.
*/
private BroadcastReceiver mainReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Action_Contacts_Added)) {
ChatSDKBaseFragment contacts = getFragment(AbstractChatSDKTabsAdapter.Contacts);
if (contacts != null)
contacts.refreshOnBackground();
if (intent.getExtras().containsKey(ChatSDKSearchActivity.USER_IDS_LIST)) {
String[] ids = intent.getStringArrayExtra(ChatSDKSearchActivity.USER_IDS_LIST);
for (String id : ids)
getNetworkAdapter().getEventManager().userMetaOn(id, null);
}
} else if (intent.getAction().equals(Action_clear_data)) {
clearData();
} else if (intent.getAction().equals(Action_Refresh_Fragment)) {
if (intent.getExtras() == null)
return;
if (!intent.getExtras().containsKey(PAGE_ADAPTER_POS))
return;
int fragment = intent.getExtras().getInt(PAGE_ADAPTER_POS);
ChatSDKBaseFragment frag = getFragment(fragment);
if (frag != null)
frag.refresh();
} else if (intent.getAction().equals(ChatSDKAbstractChatActivity.ACTION_CHAT_CLOSED)) {
getFragment(AbstractChatSDKTabsAdapter.Conversations).loadDataOnBackground();
}
}
};
private void clearData() {
ChatSDKBaseFragment contacts = getFragment(AbstractChatSDKTabsAdapter.Contacts);
if (contacts != null)
contacts.clearData();
ChatSDKBaseFragment conv = getFragment(AbstractChatSDKTabsAdapter.Conversations);
if (conv != null)
conv.clearData();
ChatSDKBaseFragment pro = getFragment(AbstractChatSDKTabsAdapter.Profile);
if (pro != null)
pro.clearData();
}
/* Exit Stuff*/
@Override
public void onBackPressed() {
exitHelper.triggerExit();
}
/**
* After screen orientation chage the getItem from the fragment page adapter is no null but it is not visible to the user
* so we have to use this workaround so when we call any method on the wanted fragment the fragment will respond.
* http://stackoverflow.com/a/7393477/2568492
*/
private ChatSDKBaseFragment getFragment(int index) {
return ((ChatSDKBaseFragment) getFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + index));
}
}
private void loadGoogleAds_banner() {
MobileAds.initialize(getApplicationContext(), "ID");
AdView aAdView = (AdView) findViewByID(R.id.googleAds);
AdRequest adRequest = new AdRequest.Builder().build();
aAdView.loadAd(adRequest);
}
}
Code we will be directed to when app crashes:
Backendless.Messaging.publish(channel, data.toString(), publishOptions, deliveryOptions, new AsyncCallback<MessageStatus>() {
@Override
public void handleResponse(MessageStatus response) {
if (DEBUG) Timber.v("Message published to channel: " + channel);
}
@Override
public void handleFault(BackendlessFault fault) {
if (DEBUG) Timber.v("Publish failed, " + fault.getMessage());
}
});
}
}
@Override
public Promise uploadFile(byte[] data, String name, String mimeType) {
return null;
}
}
来源:https://stackoverflow.com/questions/43766980/when-sending-message-app-crashes-with-this-error-it-works-fine-prior-to-admob