Ploblem Building My First Extention

This is my full code

package io.makeroid.berasouvik333.experiment;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;


@DesignerComponent(
  version = 1,
  description = "Extension made with Kodular IDE",
  category = ComponentCategory.EXTENSION,
  nonVisible = true,
  iconName = "https://ide.kodular.io/assets/logo.png")

@SimpleObject(external = true)
public final class experiment extends AndroidNonvisibleComponent {
  
  private int firstNumber;
  private int secondNumber;
  private int res;

  /**
   * Creates a new component
   */
  public experiment(ComponentContainer container) {
    super(container.$form());
  }
  
  @SimpleFunction (description = "This is an test example method")
  public void Calculate(int x,int y) {
      firstNumber = x;
      secondNumber = y;
      res = x+y; 

      GotResult(res); 
  }
  @SimpleEvent (description = "Test Event Block")
  public void GotResult(int result ) {
      EventDispatcher.dispatchEvent(this, "GotResult", result ); 
  }
  
  
    
}

When I compile it it gives me this error

http://0.ide.kodular.io/log/ac3859ce673934738e894cc105c6d57d.txt

@Abhijith_Dominic and @cian can you please help me…

1 Like

Use These Imports Instead of Importing each one Individually. Its much easier and does not increase the size of the extension much

import com.google.appinventor.components.annotations.* ;
import com.google.appinventor.components.runtime.* ;
import com.google.appinventor.components.common.* ;
import android.content.pm.* ;

3 Likes

import com.google.appinventor.components.runtime.EventDispatcher;

the error message tells you exact that you have not imported it…

2 Likes

There is a * at the end of those . This system deletes them

1 Like

Yes That’s true there is a * after the .

1 Like

In fairness no it does not. When I first started, I had no idea what library was used for the importing elements.

The most difficult thing I have found is finding what library is used, and EventDispatcher was very confusing for me to start.

1 Like

The error message is clear enough. All you have to do is read it.

3 Likes

Thanks @Mika @Abhijith_Dominic @cian :grinning::grinning:

Yes but is that an androids class, an appybuilder class, where is the class. How do you know from that message where you need to import from

import com.google.appinventor.components.runtime.EventDispatcher;

Where am I messing up?

You see what’s missing.
Then you search the files in the sources.

There is a github page with all app inventor sources.
There you can look where the file is located.

After you found the file, open it and copy the first line:
In this example for EventDispatcher file:
package com.google.appinventor.components.runtime;

Replace then package with import.
Then replace ; with .EventDispatcher;

Result:
import com.google.appinventor.components.runtime.EventDispatcher;

1 Like

That is not even remotely obvious to support a comment like this:

To add to that if people are using code from one of the other couple dozen libraries available on IDE, they may not know which library. Is it java.utils etc etc?

The point is, being dismissive does not help. For me, if EventDispatcher is so important, why is it not one of the native imports on IDE? So when I started building with IDE my assumption was that it EventDispatcher was not from AppyBuilder library, and I could not figure it out.

@Abhijith_Dominic is the one who helped me out.

If you think I am wrong, please see the small PM I just sent you.

2 Likes

My above answer should be clear enough how to know the correct import anyone need.

You only need that when you would use events.

For that I already wrote:

1 Like

Yes but how do you know what source your class is calling from?

Example:
EventDispatcher.XXX

The .XXX is then a call from the class “EventDispatcher” to the method “XXX” which is in the class “EventDispatcher”

It’s pretty hard for a beginner to understand how library and packages work. They will learn eventually. For experienced users like you It feels easy and simple but for a beginner it’s pretty hard. Your reply will help him out understand about classes now.

2 Likes

Since you’re learning I have a suggestion.
Instead of using an EventDispatcher I recommend:
Replacing “void” with “int” or “double” and “GotResult” with “return”

Something like this:
@SimpleFunction(description = “Add two numbers”)
public double Add(double num1, double num2) {
return num1+num2;
}

1 Like

Thanks @Ken for your suggestion … but, I already know that one which returns value directly from a method. I wanted to know how to make a Event which returns a value.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.