Extension Development error - Cannot convert Java type 'com.google.appinventor.components.runtime.ComponentContainer' to Yail type

My Code -

public void Generate(String type, ComponentContainer labelForQuestion, ComponentContainer labelForAnswer) {
if (type == easy) {
//easy quiz code
int easy1 = (int) (Math.random() * 1001);
int easy2 = (int) (Math.random() * 1001);
String signarr = {"plus", "minus", "multiply"};
Random random = new Random();
int index = random.nextInt(signarr.length);
if (signarr[index] == plus) {
labelForQuestion.Text(Integer.toString(easy1 + "+" + easy2));
int answerofeasy = easy1 + easy2;
labelForQuestion.Text(Integer.toString(answerofeasy));
} else {
labelForQuestion.Text("Something Went Wrong");
}

} else {
    labelForAnswer.Text("hi");
}

}

error Log -

[javac] java.lang.IllegalArgumentException: Cannot convert Java type 'com.google.appinventor.components.runtime.ComponentContainer' to Yail type

How to fix this error

Component Container and Component are two different things.
I suggest you to check the sources of label and textbox.

1 Like

so what should i use for label

Your code is a miss.

means?

Have a look at this code, it will help you:

/**  ~~~~~
 * Created with the AppyBuilder Code Editor.
 * This is a template for basic Extension.
 * Modify this template to customize your extension.
 *
 * **** NOTE: DO NOT use a package name. 
 * **** The package name will be created for you automatically.
 * **** Adding a package name will cause a compile error
 */
import com.google.appinventor.components.runtime.Label;
import android.view.View;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;

import android.app.Activity;
import android.content.Context;

import android.util.Log;

import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.ComponentConstants;
//Added imports
import android.view.ViewGroup;// https://developer.android.com/reference/android/view/ViewGroup
import android.widget.TextView;// https://developer.android.com/reference/android/widget/TextView
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

@DesignerComponent(version = 1,  description = "This Extension was created with the AppyBuilder Code Editor.<br>" + 
                   "Create your own here:<br><a href='https://editor.appybuilder.com' target='_blank'>https://editor.appybuilder.com</a><br>",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,   iconName = "http://appyBuilder.com/extensions/icons/extension.png")
@SimpleObject(external = true)
public class EditingComponents extends AndroidNonvisibleComponent{
    private ComponentContainer container;
    private Context context; //Added
    private Activity activity; //Added
    /**
     * @param container container, component will be placed in
     */
    public EditingComponents(ComponentContainer container) {
        super(container.$form());
        this.container = container;
        context = (Context) container.$context(); //Added
        activity = (Activity) context; //Added
    }
  
  
  @SimpleFunction(description="Edit A Component")
  public void EditComponent(AndroidViewComponent comp, int stroke, int strokeColor, int bgcolor, float cornerRadius){

    View myView = (View)comp.getView();
    
   
    
   final GradientDrawable shape = new GradientDrawable();
    
    // when you chose a shape other than RECTANGLE, setCornerRadius(); function wont work, see this link:
    // https://developer.android.com/reference/android/graphics/drawable/GradientDrawable#setCornerRadius(float)
    shape.setShape(GradientDrawable.RECTANGLE);
    
    // the setColor is an argb int value, example: red color value = 0xffff0000
    shape.setColor(bgcolor);
    shape.setStroke(stroke, strokeColor);
    
  //shape.setStroke(stroke, Color.Red);
    shape.setCornerRadius(cornerRadius);
    
   // shape.setCornerRadii(new float[] { 200, 200, 100, 100, 50, 50, 25, 25});
    
    // here we are setting the shape drawable as a background for our component.
      myView.setBackgroundDrawable(shape);
    
    //see this link: https://developer.android.com/reference/android/view/View#setElevation(float)
 //   myView.setElevation(elevation);
    
    
    
  
    
 
   /*
   
   
    // this method here is to a click event and it will ovveride Kodual Click event for the component you are using now, When you use this Click event you can't use Kodular Click event for this Component and be aware that adding a GradientDrawable to this Component you are using disables any click or touch events that this component have in Kodular.
    
   
    myView.setOnClickListener(new View.OnClickListener() {
           // @Override
            public void onClick(View v) {
            	shape.setColor(0x80ff5032);
              //myView.invalidate();
            }
        });
       
     */
    


    
   }


  
  
  // Label Component extends AndroidViewComponent
  @SimpleFunction(description="Edit A Label")
  public void EditLabel(Label myLabel, String setText, int setBGColor){
    

      myLabel.Text(setText);
    
      // the setColor is an argb int value, example: red color value = 0xffff0000
      myLabel.BackgroundColor(setBGColor);
      // see BackgroundColor(); and Text(); functions here
      // https://github.com/mit-cml/appinventor-sources/blob/master/appinventor/components/src/com/google/appinventor/components/runtime/Label.java
    
    // any function in this file you can use it to manipulate the Label component.
    // you can manipulate the Button component also and other component that extends AndroidViewComponent.
    // be aware to import whatever component you want to manipulate as above in line number 10.

    
   }

  
  
  //this function is to add a TextView inside Horizontal or Vertical Arrangement.
  // see appinventor-sources for HVArrangement. 
   @SimpleFunction(description="Add A TextView")
  public void AddTextView(HVArrangement layout, String text, int color, int bgColor, int maxLines, int width, int height){
    // from Kodular set the background color for Horizontal or Vertical Arrangement you use here to None.
  
    // this is an ID assigned to the TextView inside the Linear Layout for ordering components.
    int id = 0;
    
    
    // myViewGroup now will act as our Horizontal or Vertical Arrangement that we assigned to the HVArrangement.
    ViewGroup myViewGroup = (ViewGroup)layout.getView();
    
    // here we initialized a Linear Layout inside myViewGroup to add the TextView to it.
    LinearLayout myLinearLayout = (LinearLayout)myViewGroup.getChildAt(0);
    
    // most Android widgets are constructed this way where you can say activity is our Screen1 for example.
    TextView myTextView = new TextView(activity);
    
    myTextView.setId(id);
    
    //see this link for a list of functions you can use:
    //https://developer.android.com/reference/android/widget/TextView
    myTextView.setText(text);
    
    // this will set our TextView to render all the text that it will show as a single line text.
    myTextView.setMaxLines(maxLines);
    
        // the setTextColor is an argb int value, example: red color value = 0xffff0000
    myTextView.setBackgroundColor(bgColor);

    // the setTextColor is an argb int value, example: red color value = 0xffff0000
    myTextView.setTextColor(color);
        
    //see this link: https://developer.android.com/reference/android/graphics/Color
 // myTextView.setTextColor(Color.RED);

    // here we initialized a set of rules for our TextView to follow like it's width and height.
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width , height);
    
    // here we assigned the above values to our TextView.
    myTextView.setLayoutParams(lp);

    // here we added or TextView to our Linear Layout following the rules we set earliar.
    myLinearLayout.addView(myTextView, id);
    
    // a refresh method to re-draw our Linear Layout so that our TextView shows on the screen.
    myLinearLayout.invalidate();
   }

  
}
1 Like

@Silver thank you very much. my extension compiled successfully

1 Like

You can only return a certain set of types from the Java side to the blocks. You can't return ComponentContainer directly, but if you make the object implement both Component and ComponentContainer it should work.

1 Like

Thanks sir for your reply. My issue is solved now

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