0

I only have a page for subscribed users. I try to use Revenuecat to understand if users are subscribed and to make purchases. But I guess I'm making mistake.

I want to make purchases below but I get an error.

'''
public class VipPreferences extends AppCompatActivity {

ImageView back, threemonthsvip;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vip_preferences);
    getSupportActionBar().hide();

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectLeakedClosableObjects()
            .penaltyLog()
            .penaltyDeath()
            .build());
    Purchases.setDebugLogsEnabled(true);
    Purchases.configure(this, "api_key");

    back=(ImageView)findViewById(R.id.back);
    threemonthsvip=(ImageView)findViewById(R.id.threemonthsVip);

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            VipPreferences.super.onBackPressed();
        }
    });

    threemonthsvip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Purchases.getSharedInstance().purchasePackage(this,package,new MakePurchaseListener() {
                @Override
                public void onCompleted(@NonNull Purchase purchase, @NonNull PurchaserInfo purchaserInfo) {
                    if (purchaserInfo.getEntitlements().get("Vip").isActive()) {
                        // Unlock that great "pro" content
                        Toast.makeText(VipPreferences.this, "Congratulations! Your payment is successful.", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(VipPreferences.this,Vip.class);
                        startActivity(intent);
                    }
                }

                @Override
                public void onError(@NonNull PurchasesError error, boolean userCancelled) {
                    Toast.makeText(VipPreferences.this, "Something were wrong", Toast.LENGTH_SHORT).show();
                }
            });
        }
    });
}

} '''

Error Image

Below I check the subscription status in setOnClickListener and try to I'm trying to open purchase options if not subscribed, opens the activity if subscriber. But when I click while the application is running, the application closes directly.

''' public class MainActivity extends AppCompatActivity {

ImageView showFree,showVip;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().hide();
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectLeakedClosableObjects()
            .penaltyLog()
            .penaltyDeath()
            .build());
    Purchases.setDebugLogsEnabled(true);
    Purchases.configure(this, "api_key");

    showFree=(ImageView)findViewById(R.id.showFree);
    showVip=(ImageView)findViewById(R.id.showVip);

    showFreeTips.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(MainActivity.this,Free.class);
            startActivity(intent);
        }
    });

    showVip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Purchases.getSharedInstance().getPurchaserInfo(new ReceivePurchaserInfoListener() {
                @Override
                public void onReceived(@NonNull PurchaserInfo purchaserInfo) {
                    if (purchaserInfo.getEntitlements().get("Vip").isActive()) {
                        // Grant user "pro" access
                        Intent intent=new Intent(MainActivity.this,Vip.class);
                        startActivity(intent);
                    }else  {
                        Intent intent=new Intent(MainActivity.this,VipPreferences.class);
                        startActivity(intent);
                    }
                }

                @Override
                public void onError(@NonNull PurchasesError error) {

                }
            });
        }
    });
}

'''

Bora
  • 1

1 Answers1

0

The first issue you're facing is due to the fact that you are referencing a variable package when calling purchasePackage. package is a reserved Java keyword and can't be used to name variables. You can see a list of reserved Java words here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

For the second issue, it looks like you are getting a runtime exception. It would be helpful for you to look at the Android Logcat in Android Studio https://developer.android.com/studio/debug/am-logcat to figure out what's causing the app to close.

Cesar
  • 682
  • 3
  • 15