错误:将Android Studio从0.6.1更新到0.8.9后没有合适的构造函数

时间:2014-10-11 12:03:51

标签: java android android-studio

我有这个错误:

Error:(78, 56) error: no suitable constructor found for ImageViewTouch(Viewpager_activity)
constructor ImageViewTouch.ImageViewTouch(Context,AttributeSet,int) is not applicable
(actual and formal argument lists differ in length)
constructor ImageViewTouch.ImageViewTouch(Context,AttributeSet) is not applicable
(actual and formal argument lists differ in length)

但是这个错误我只是在将Android Studio从0.6.1更新到0.8.9之后才出来,我在升级之前没有那个错误,事实上,我在下面的代码在更新之前工作得很好。 最多,我在Nexus 5上使用该代码的应用程序,并没有任何问题。

Viewpager_layout.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#00A600"
        android:id="@+id/layoutesterno">

        <com.ruar.imagegallery.app.ImageViewTouchViewPager
            android:id="@+id/imagepager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/relativeLayout" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_gravity="bottom"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/relativeLayout"
            android:background="#008A00">

            <ImageButton
                android:layout_width="60dp"
                android:layout_height="fill_parent"
                android:id="@+id/saveBtn"
                android:background="@drawable/save"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <ImageButton
                android:layout_width="60dp"
                android:layout_height="62dp"
                android:id="@+id/shareBtn"
                android:background="@drawable/share"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_centerInParent="true"
                android:layout_alignTop="@+id/saveBtn"/>

            <ImageButton
                android:layout_width="70dp"
                android:layout_height="62dp"
                android:id="@+id/setasBtn"
                android:background="@drawable/setaswallpaper"
                android:layout_alignTop="@+id/saveBtn"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

        </RelativeLayout>

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/info"
        android:background="@drawable/info"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/bugimg"
        android:background="@drawable/bug"
        android:layout_below="@+id/info"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="20dp"
        android:layout_alignLeft="@+id/info"
        android:layout_alignStart="@+id/info" />

</RelativeLayout>



Viewpager_activity.java

public class Viewpager_activity extends Activity
{
    int position;
    int img_id;
    Grid_adapter grid_adapter;
    String nomeimmagine;
    int i;
    final List<ImageViewTouch> images = new ArrayList<ImageViewTouch>();
    public static  String[][] galleryArrayThumb = ImmaginiThumb.thumbs;

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

            ImageLoader_Grid imageLoader = new ImageLoader_Grid(this.getApplicationContext());

            final Intent intent = getIntent();
            img_id = (Integer) intent.getExtras().get("img");
            position = (Integer) intent.getExtras().get("id");

            grid_adapter = new Grid_adapter(this, galleryArrayThumb[img_id]);
            nomeimmagine = LazyAdapter.titoli[img_id];
            setTitle(nomeimmagine);

            for (i = 0; i < grid_adapter.getCount(); i++)
            {
                 final ImageViewTouch imageViewTouch = new ImageViewTouch(this);

                 Picasso.with(Viewpager_activity.this)
                        .load(galleryArrayThumb[img_id][i])
                        .fit()
                        .centerInside()
                        .placeholder(R.drawable.loading)
                        .into(imageViewTouch);

                //imageLoader.DisplayImage(galleryArrayThumb[img_id][i], imageViewTouch);
                images.add(imageViewTouch);
            }

            ViewPager viewPager = (ImageViewTouchViewPager) findViewById(R.id.imagepager);
            viewPager.setAdapter(new Viewpager_adapter(images));
            viewPager.setCurrentItem(position);
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
            {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
                {
                    try
                    {
                        trimCache(Viewpager_activity.this);
                        Log.i("VP_cache", "Cache liberata");
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();

                    }
                }

                @Override
                public void onPageSelected(int position1) {
                    position = position1;
                }

                @Override
                public void onPageScrollStateChanged(int state) {}
            });
}//fine viewpager_activity



错误在这一行,列是写“this”的地方,也就是ImageViewTouch的上下文:

final ImageViewTouch imageViewTouch = new ImageViewTouch(this);

我也试过这些代码:

final ImageViewTouch imageViewTouch = new ImageViewTouch(Viewpager_activity.this);
final ImageViewTouch imageViewTouch = new ImageViewTouch(getApplicationContext());

但它没有改变任何东西,我仍然得到那个错误

另一个错误是:

Error:(36, 41) error: no suitable constructor found for ImageViewTouch(Context)
constructor ImageViewTouch.ImageViewTouch(Context,AttributeSet,int) is not applicable
(actual and formal argument lists differ in length)
constructor ImageViewTouch.ImageViewTouch(Context,AttributeSet) is not applicable
(actual and formal argument lists differ in length)


而java代码就是这个

Viewpager_adapter.java

public class Viewpager_adapter extends PagerAdapter
{
    private List<ImageViewTouch> imageViewTouches;

    public Viewpager_adapter(List<ImageViewTouch> imageViewTouches)
    {
        this.imageViewTouches = imageViewTouches;
    }

    @Override
    public int getCount() {
        return imageViewTouches.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        ImageViewTouch imageViewTouch = new ImageViewTouch(container.getContext());
        imageViewTouch = (ImageViewTouch) imageViewTouches.get(position);
        imageViewTouch.setScaleType(ImageViewTouch.ScaleType.MATRIX);

        container.addView(imageViewTouch);

        return imageViewTouch;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ImageViewTouchViewPager pager = (ImageViewTouchViewPager) container;
        View recycledView = (View) object;
        pager.removeView(recycledView);
    }
}



误差就是这样:

ImageViewTouch imageViewTouch = new ImageViewTouch(container.getContext());



更具体地说,代码字符串的这一部分:

new ImageViewTouch(container.getContext());


我在更新Android Studio之前重复了一切顺利。 在此先感谢,如果您需要其他代码,请

1 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

在ImageViewTouch类中,两个重载的构造函数定义如下:

ImageViewTouch(Context,AttributeSet) and ImageViewTouch(Context,AttributeSet,int)

您正在尝试使用此ImageViewTouch类中未定义的构造函数初始化ImageViewTouch对象:

ImageViewTouch(Context)

因此,尝试使用一个重载的构造函数,如:

new ImageViewTouch(container.getContext(),null,0) or  new ImageViewTouch(container.getContext(),null)

在ViewPager中:

ImageViewTouch imageViewTouch = new ImageViewTouch(container.getContext(),null); or ImageViewTouch imageViewTouch = new ImageViewTouch(container.getContext(),null,0);