Why this java code not working?

    package com.ads.interads;  
  
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.*;
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;
  
import com.google.android.gms.ads.AdListener;  
import com.google.android.gms.ads.AdRequest;  
import com.google.android.gms.ads.InterstitialAd;  
  
@DesignerComponent(version = 1, // Update version here, You must do for each new release to upgrade your extension
                   description = "interads",
                   category = ComponentCategory.EXTENSION,
                   nonVisible = true,
                   iconName = "images/extension.png") // Change your extension's icon from here; can be a direct url
@SimpleObject(external = true)
public class InterstitialAdsActivity extends AppCompatActivity {  
    InterstitialAd mInterstitialAd;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_interstitial_ads);  
        mInterstitialAd = new InterstitialAd(this);  
  
        // set the ad unit ID  
        mInterstitialAd.setAdUnitId(getString);  
  
        AdRequest adRequest = new AdRequest.Builder().build();  
  
        // Load ads into Interstitial Ads  
        mInterstitialAd.loadAd(adRequest);  
  
        mInterstitialAd.setAdListener(new AdListener() {  
            public void onAdLoaded() {  
                showInterstitial();  
            }  
        });  
    }  

  @Override
    private void showInterstitial() {  
        if (mInterstitialAd.isLoaded()) {  
            mInterstitialAd.show();  
        }  
    }  
  
  
} 

please help me
code not working

There are a number of things wrong with your code. You may want to start by following the example in the extensions document first to understand how to build a simple extension before tackling one with lots of extra constraints.

  1. Extensions must be a subclass of AndroidNonvisibleComponent
  2. AAR support is not available for extensions, so your references to R.layout will all fail to compile
  3. I have no idea where getString is coming from, so my guess is you'll get a compiler error on that token as well
1 Like

can you help me please.
Please Re-Edit this Java file.
it will give me an idea.
how to make a ad extension.

please sir
it's my humble request.

can you please help me

I have no idea about Mr E.W. Patton but I think asking for modification is not good.
He has already mentioned steps to fix the issue.

3 Likes

yes but i am new . so help me
please re-edit my java file

The best way to learn is to try yourself.

You’ve been told what to do. Please follow the steps outlined above. If they don’t work then show what you’ve done and someone may help you further.

3 Likes

really i don't have any idea about java.
please re-edit this

This is why it’s so important for you to do it yourself. It’s the only way you will learn.

If you don't know java you should consider learning Java first then build some basic extensions.

1 Like

but it will give me an idea.
please help me

You’ve been given help in post #2. You are refusing to do anything yourself. At this point you are now spamming by asking for help over and over again.

1 Like

I am also new to java and extension development.

Some steps which I followed.

  • Daily watch tutorials and guides on youtube, approx 1 hour.
  • Trying to build extension!
  • Results - If fail
  • Do some research in google/stackoverflow
  • If it doesn't help me
  • Get help from others (not to ask full)
  • Else - Next code

You know today I took more than 2 hours solving problems in my BinaryTools extension, finally passed! :relaxed:

what to use in that case

I don't know. As the others have stated, you may want to start simple first and learn how to build a component and an extension for App Inventor before getting too complicated. As I mentioned in my earlier statements, you likely aren't going to be able to make this work due to issue #2.

3 Likes

thanks, i have learn many things.
but i am still getting error.

please check my Modified code

package com.ads.inter;  

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.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.util.MediaUtil;
import com.google.appinventor.components.runtime.*;
  
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.*;
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;
  
import com.google.android.gms.ads.AdListener;  
import com.google.android.gms.ads.AdRequest;  
import com.google.android.gms.ads.InterstitialAd;  
  
@DesignerComponent(version = 1, // Update version here, You must do for each new release to upgrade your extension
               description = "interads",
               category = ComponentCategory.EXTENSION,
               nonVisible = true,
               iconName = "images/extension.png") // Change your extension's icon from here; can be a direct url
@SimpleObject(external = true)
public class interads extends AndroidNonvisibleComponent {  
InterstitialAd mInterstitialAd;  
  
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_interstitial_ads);  
    mInterstitialAd = new InterstitialAd(this);  
  
    // set the ad unit ID  
@SimpleFunction
public String SetAdUnit(String UnitId) {
    mInterstitialAd.setAdUnitId(UnitId);  
 }
  
    AdRequest adRequest = new AdRequest.Builder().build();  
  
    // Load ads into Interstitial Ads  
 @SimpleFunction
public String LoadAd{
    mInterstitialAd.loadAd(adRequest);  
  
    @SimpleEvent
	public void onAdLoaded{  
           EventDispatcher.dispatchEvent(this, "onAdLoaded");  
        }    
}  

  @Override
private void showInterstitial{  
    if (mInterstitialAd.isLoaded()) {  
        mInterstitialAd.show();  
    }  
}  
  
  
}

You have referred my code right? :grinning: :grinning:

1 Like

???

You didn't return anything. If you don't want to return anything, add void keyword.
Secondly, many of your method don't have () parameters

Thirdly, to make a proper event,

@SimpleEvent(description = "Description here")
public void onAdLoaded(){
   EventDispatcher.dispatchEvent(this, "onAdLoaded");
}

@SimpleFunction(description = "") 
public void LoadAd() {
  //Your code
  onAdLoaded();
}
1 Like

He had asked me for source of old binary extension...

But this is an ad extension.

1 Like