(R.dimen.padding_medium) Cannot be allowed or is not a field?

I am trying to create a new gallery style app since Gallery has been devalued.

The main problem that I am facing is that when I try to run the following code, I cannot reap the error "dimen cannot be resolved or is not a field". I read some websites and some posts here that say it is related to import "import com.example.test.R;" however, even so, the error persists, if someone can help, it will be awesome.

Gall.java:

package com.example.test; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; public class Gall extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gall); ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); ImgAdapt adapter = new ImgAdapt(this); viewPager.setAdapter(adapter); } } 

activity_gall.xml:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> 

ImgAdapt.java:

 package com.example.test; import android.content.Context; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; public class ImgAdapt extends PagerAdapter { Context context; private int[] GalImages = new int[] { R.drawable.one, R.drawable.two, R.drawable.three }; ImgAdapt(Context context){ this.context=context; } public int getCount() { return GalImages.length; } public boolean isViewFromObject(View view, Object object) { return view == ((ImageView) object); } public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(context); int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium); imageView.setPadding(padding, padding, padding, padding); imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); imageView.setImageResource(GalImages[position]); ((ViewPager) container).addView(imageView, 0); return imageView; } public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView((ImageView) object); } } 

Lots of code, sorry if this is a vague question, but "dimen" in the line

 int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium); 

- the only part that gives me an error.

+6
source share
2 answers

I think I'm late to answer, but may be useful for future reference.

You must define padding_medium in the res-> values-> dens.xml file.

For instance:

 <dimen name="padding_medium">5dp</dimen> 
+15
source

Today I encountered a similar error, and the solution was added

 import com.yourpackagename.R; 
0
source

Source: https://habr.com/ru/post/944830/


All Articles