-1
kotlin array
Converti um código de java para kotlin, porém está com um erro que não consigo resolver
Here is the java code
public class IntroActivity extends AppCompatActivity {
private ViewPager viewPager;
private Button btnNext;
private ImageView[] vectorImage;
private LinearLayout containerDots;
private Drawable active;
private Drawable inactive;
private SliderFragmentAdapter adapterFragment;
private int lengthSlide;
private Button btnSkip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
initViews();
configSlideAdapter();
configNextSlideButton();
configVetorImgSlid();
configSkipOnboardingButton();
}
private void configSkipOnboardingButton() {
btnSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finishOnboardin();
}
});
}
private void finishOnboardin() {
finish();
Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
startActivity(intent);
}
private void configVetorImgSlid() {
lengthSlide = adapterFragment.getCount();
vectorImage = new ImageView[lengthSlide];
prepareDots();
}
private void prepareDots() {
for (int i = 0; i < lengthSlide; i++) {
vectorImage[i] = new ImageView(this);
vectorImage[i].setImageDrawable(inactive);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(16, 0, 16, 0);
containerDots.addView(vectorImage[i], layoutParams);
}
vectorImage[0].setImageDrawable(active);
}
private void configSlideAdapter() {
adapterFragment = new SliderFragmentAdapter(getSupportFragmentManager(), 4);
viewPager.setAdapter(adapterFragment);
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < lengthSlide; i++) {
vectorImage[i].setImageDrawable(inactive);
}
vectorImage[position].setImageDrawable(active);
}
@Override
public void onPageScrollStateChanged(int state) {}
});
}
private void initViews() {
viewPager = findViewById(R.id.viewPage);
btnNext = findViewById(R.id.btn_next);
containerDots = findViewById(R.id.container_dots);
active = ContextCompat.getDrawable(this, R.drawable.active_dot);
inactive = ContextCompat.getDrawable(this, R.drawable.inactive_dots);
btnSkip = findViewById(R.id.btn_skip);
}
private void configNextSlideButton() {
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int currentSlide = viewPager.getCurrentItem();
viewPager.setCurrentItem(currentSlide + 1);
if (currentSlide == (lengthSlide - 1)) {
finishOnboardin();
}
}
});
}
}
Código em Kotlin
Converted stays this way
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import br.com.brq.brqmotors.onboarding.Adapter.ViewPagerAdapter
class OnboardingActivity : AppCompatActivity() {
private var viewPager: ViewPager? = null
private var btnNext: Button? = null
private var vectorImage: Array<ImageView>? = null
private var containerDots: LinearLayout? = null
private var active: Drawable? = null
private var inactive: Drawable? = null
private var adapterFragment: ViewPagerAdapter? = null
private var lengthSlide: Int = 0
private var btnSkip: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(br.com.brq.brqmotors.R.layout.activity_onboarding)
initViews()
configSlideAdapter()
configNextSlideButton()
configVetorImgSlid()
configSkipOnboardingButton()
}
private fun configSkipOnboardingButton() {
btnSkip!!.setOnClickListener { finishOnboarding() }
}
private fun finishOnboarding() {
finish()
val intent = Intent(applicationContext, javaClass::class.java)
startActivity(intent)
}
private fun configVetorImgSlid() {
lengthSlide = adapterFragment.getCount()
vectorImage = arrayOfNulls<ImageView>(lengthSlide)
prepareDots()
}
private fun prepareDots() {
for (i in 0 until lengthSlide) {
vectorImage?.set(i, ImageView(this))
vectorImage!![i].setImageDrawable(inactive)
val layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
layoutParams.setMargins(16, 0, 16, 0)
containerDots!!.addView(vectorImage!![i], layoutParams)
}
vectorImage!![0].setImageDrawable(active)
}
private fun configSlideAdapter() {
adapterFragment = ViewPagerAdapter(supportFragmentManager, 4)
viewPager!!.adapter = adapterFragment
viewPager!!.addOnPageChangeListener(object : OnPageChangeListener {
override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {
}
override fun onPageSelected(position: Int) {
for (i in 0 until lengthSlide) {
vectorImage!![i].setImageDrawable(inactive)
}
vectorImage!![position].setImageDrawable(active)
}
override fun onPageScrollStateChanged(state: Int) {}
})
}
private fun initViews() {
viewPager = findViewById(br.com.brq.brqmotors.R.id.viewPagerPrincipal)
btnNext = findViewById(br.com.brq.brqmotors.R.id.btn_proximo)
containerDots = findViewById(br.com.brq.brqmotors.R.id.container_pontos)
btnSkip = findViewById(br.com.brq.brqmotors.R.id.btn_pular)
}
private fun configNextSlideButton() {
btnNext!!.setOnClickListener(View.OnClickListener {
val currentSlide = viewPager!!.currentItem
viewPager!!.currentItem = currentSlide + 1
if (currentSlide == lengthSlide - 1) {
finishOnboarding()
}
})
}
}
The error is found here
private fun configVetorImgSlid() {
lengthSlide = adapterFragment.getCount()
vectorImage = arrayOfNulls<ImageView>(lengthSlide)
prepareDots()
}