Java Question ba.raiseEvent not firing...

DevBaby

Active Member
Licensed User
Longtime User
I have used the ba.raiseEvent from a library before with success. However this time the code executes (no errors) but nothing happens.

I am using an @ActivityOjbect for the first time, I am not sure if this has anything to do with it.
 

warwound

Expert
Licensed User
Longtime User
Are you trying to raise an event from the main UI thread or from within another thread?
If you're trying to raise an event from within another thread then you need to use the syntax:

B4X:
ba.raiseEventFromDifferentThread(Sender instance here, null, ATaskId, EventName here, true, new Object[] { Your values });

The Boolean parameter (true in this example) tells raiseEventFromDifferentThread whether to raise an exception if the event Sub does not exist.
That's another obvious thing to check - whether the event Sub exists - check that whether or not you're raising an event from the main UI thread.
When you initialize the object do you convert the event name parameter to lower case?
Try logging the complete eventname that you're raising to see exactly what Sub it's trying to call.

Martin.
 

DevBaby

Active Member
Licensed User
Longtime User
Hi,

I am not sure what thread I am trying to raise the event from (Main UI vs. another).

When I use ba.raiseEventFromDifferentThread(Sender instance here, null, ATaskId, EventName here, true, new Object[] { Your values });, I get the exception thrown for a missing event, but I definitely have the event included in the main module of B4A.
 

DevBaby

Active Member
Licensed User
Longtime User
Thanks for your help, the code is below. For easy reference, the call private void showfeed is at the bottom of the sdk code block. I am also using an AsyncFacebookRunner asyncRunner; to run the calls on a separate thread.


B4A event code...


B4X:
Sub fbfeedcomplete(answer As String)

   Msgbox("feed complete","msg")

End Sub


Sdk code....


B4X:
package com.facebook.android;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

import com.facebook.android.Facebook.DialogListener;



@ActivityObject
//@DependsOn(values={"facebooksdk"})
@Permissions(values={"android.permission.INTERNET"})
@ShortName("fbtest")
@Version(1.0f)

public class fbshare {

   Facebook fb;
   SharedPreferences sp;
   String FbPicture;
   String FbLink;
   String FbName;
   String FbCaption;
   String FbDescription;
   
   String DialogType;
   
   AsyncFacebookRunner asyncRunner;


   public void FbConnect(final BA ba, String App_ID, String sType, String sPicture, String sLink, String sName, String sCaption, String sDescription, Drawable pic) {
      
      FbPicture = sPicture;
      FbLink = sLink;
      FbName = sName;
      FbCaption = sCaption;
      FbDescription = sDescription;

      DialogType = sType;

      
      Utility.closeimage = pic; 
      
      fb = new Facebook(App_ID);
      asyncRunner = new AsyncFacebookRunner(fb); 
      
      sp = ba.context.getSharedPreferences("amapp",Context.MODE_PRIVATE); 
      
      String Access_Token = sp.getString("Access_Token", null);
      long Expires = sp.getLong("Expires", 0);
            
      if(Access_Token != null) {
         fb.setAccessToken(Access_Token);
      }
      
      if(Expires != 0) {
         fb.setAccessExpires(Expires);
      }

      if(fb.isSessionValid()) {
      
         // code for feed dialog
         
         if(DialogType == "feed"){
            
            showfeed(ba);
            
         }else{
            
            apprequest(ba);
         }
         
      }else{
         
         // Log into FB
         fb.authorize(ba.activity, new String[] {"publish_stream","read_friendlists"},Facebook.FORCE_DIALOG_AUTH, new DialogListener() { 
         
            @Override
            public void onCancel() {
               Log.d("B4A", "DialogListener onCancel");
               
            }
            
            @Override
            public void onComplete(Bundle values) {
               Log.d("B4A", "DialogListener onComplete");
               Editor editor = sp.edit(); 
               editor.putString("Access_Token", fb.getAccessToken());
               editor.putLong("Expires", fb.getAccessExpires());
               editor.commit();
            
               if(DialogType == "feed"){
                  Log.d("B4A", "DialogListener showfeed");
                  showfeed(ba);
                  
               }else{
                  
                  Log.d("B4A", "DialogListener apprequest");
                  apprequest(ba);
               }
               
            }

            @Override
            public void onError(DialogError derror) {
         
               Log.d("B4A", "Authorize OnError - " + derror.toString());
               //ba.raiseEvent(this, "fberror", new Object[] { derror.toString() });
               
            }
            
            @Override
            public void onFacebookError(FacebookError ferror) {
            
               Log.d("B4A", "Authorize OnFacebookError - " + ferror.toString() );
               //ba.raiseEventFromDifferentThread(Sender instance here, null, ATaskId, EventName here, true, new Object[] { Your values });
               //ba.raiseEvent(this, "fbrrror", new Object[] { ferror.toString() });
               
            }
            
         
         });   

      }
      
   }   
   
   private void apprequest(final BA ba){

      Bundle params =  new Bundle();
      params.putString("title", FbCaption);
      params.putString("message", FbDescription);
      
      fb.dialog(ba.context, "apprequests", params, new DialogListener() {

         @Override
         public void onCancel() {
            Log.d("B4A", "DialogListener onCancel");
         }
         
         @Override
         public void onComplete(Bundle values) {
            
            int userNum = values.keySet().size() - 1;
            Log.d("B4A", "AppRequestComplete onComplete -" + userNum);

            ba.raiseEventFromDifferentThread(this, null, 0, "fbapprequestscomplete", true, new Object[] { userNum });
            //ba.raiseEvent(this, "fbapprequestcomplete", new Object[] { userNum });
            
         }

         @Override
         public void onError(DialogError derror) {
            Log.d("B4A", "AppRequest onError - " + derror.toString());
            //ba.raiseEventFromDifferentThread(this, null, 0, "fberror", true, new Object[] { "Showfeed onFacebookError - " + derror.toString() });
            //ba.raiseEvent(this, "FbError", new Object[] { derror.toString() });

         }
         
         @Override
         public void onFacebookError(FacebookError ferror) {
            Log.d("B4A", "AppRequest onFacebookError - "  + ferror.toString());
            //ba.raiseEventFromDifferentThread(this, null, 0, "fberror", true, new Object[] { "Showfeed onFacebookError - " + ferror.toString() });
            //ba.raiseEvent(this, "FbError", new Object[] { ferror.toString() });
            
         }
         
      });
      
   }
   
   private void showfeed(final BA ba){

      Bundle params =  new Bundle();
      params.putString("link", FbLink);
      params.putString("caption", FbCaption);
      params.putString("picture", FbPicture);
      params.putString("description", FbDescription);
      params.putString("name", FbName);
      
      fb.dialog(ba.context, "feed", params, new DialogListener() {
         
         @Override
         public void onCancel() {
            Log.d("B4A", "DialogListener onCancel");
         }
         
         @Override
         public void onComplete(Bundle values) {
            Log.d("B4A", "DialogListener onComplete");
            ba.raiseEventFromDifferentThread(this, null, 0, "fbfeedcomplete", true, new Object[] { "OK" });
            //ba.raiseEvent(this, "fbfeedcomplete", new Object[] { "OK" });
      
         }

         @Override
         public void onError(DialogError derror) {
            Log.d("B4A", "Showfeed onError - " + derror.toString());
            //ba.raiseEventFromDifferentThread(this, null, 0, "fberror", true, new Object[] { "Showfeed onError - " + derror.toString() });
            //ba.raiseEvent(this, "FbError", new Object[] { derror.toString() });
            
         }
         
         @Override
         public void onFacebookError(FacebookError ferror) {
            Log.d("B4A", "Showfeed onFacebookError - " + ferror.toString());
            //ba.raiseEventFromDifferentThread(this, null, 0, "fberror", true, new Object[] { "Showfeed onFacebookError - " + ferror.toString() });
            //ba.raiseEvent(this, "FbError", new Object[] { ferror.toString() });
            
         }
         
      });
      
   }
   

}
 
Last edited:

DevBaby

Active Member
Licensed User
Longtime User
This worked! Thanks to you both, Erel and Warwound, for your help with this.
 
Top