错误:(42,17)错误:找不到符号变量

时间:2015-12-16 03:55:31

标签: android

我有以下活动。在这项活动中,我有10张图片。图像随next和prev按钮而变化。但我得到这样的错误错误:(42,17)错误:找不到符号变量ImageViewPic。请帮我编写代码或编辑我的代码。

这是Java代码

 public class MainActivity extends AppCompatActivity {
        private ImageView img;
        private Button lbutton, rbutton;
        private int cur=0;
        final int [] images = {R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10};
        private View.OnClickListener lbuttonChangeImageListener,rbuttonChangeImageListener;

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

            img = (ImageView) findViewById(R.id.ImageViewPic);
            lbutton = (Button) findViewById(R.id.Pbutton);
            rbutton = (Button) findViewById(R.id.Nbutton);

            //View.OnClickListener lbuttonChangeImageListener = null;

            lbutton.setOnClickListener(lbuttonChangeImageListener);
            //View.OnClickListener rbuttonChangeImageListener = null;
            rbutton.setOnClickListener(rbuttonChangeImageListener);

            View.OnClickListener iButtonChangeImageListener = new View.OnClickListener() {
                public void onClick(View v) {
                    //Increase Counter to move to next Image
                    cur++;
                    cur = cur % images.length;
                    ImageViewPic.setImageResource(images[cur]);
                }
            };
            View.OnClickListener rbuttonChangeImageListener = new View.OnClickListener() {
                public void onClick(View v) {
                    //Increase Counter to move to next Image
                    cur--;
                    cur = (cur + images.length) % images.length;
                    ImageViewPic.setImageResource(images[cur]);
                }
            };

和XML

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="fill"
    android:orientation="vertical"
   >
    <ImageView
        android:id="@+id/ImageViewPic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="100"
        android:adjustViewBounds="true"
        android:background="#66FFFA"

         />
    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#33FFFA"
        >
        <Button
            android:id="@+id/Pbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Prev"
            android:background="#AA348C90"
            >
        </Button>
        <Button
            android:id="@+id/Nbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="nxt"
            android:background="#AA348C90"
            >

        </Button>

    </LinearLayout
       >
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

尝试 img.setImageResource(images[cur]);

而不是 ImageViewPic.setImageResource(images[cur]);

答案 1 :(得分:0)

尝试更改您的代码,如下所示:

public class MainActivity extends AppCompatActivity {
    private ImageView img;
    private Button lbutton, rbutton;
    private int cur=0;
    final int [] images = {R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10};
    private View.OnClickListener lbuttonChangeImageListener,rbuttonChangeImageListener;

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

        img = (ImageView) findViewById(R.id.ImageViewPic);
        lbutton = (Button) findViewById(R.id.Pbutton);
        rbutton = (Button) findViewById(R.id.Nbutton);

        //View.OnClickListener lbuttonChangeImageListener = null;

        lbutton.setOnClickListener(lbuttonChangeImageListener);
        //View.OnClickListener rbuttonChangeImageListener = null;
        rbutton.setOnClickListener(rbuttonChangeImageListener);

        View.OnClickListener rbuttonChangeImageListener = new View.OnClickListener() {
            public void onClick(View v) {
                //Increase Counter to move to next Image
                cur++;
                cur = cur % images.length;
                img.setImageResource(images[cur]);
            }
        };
        View.OnClickListener lbuttonChangeImageListener = new View.OnClickListener() {
            public void onClick(View v) {
                //Decrease Counter to move to Previous Image
                cur--;
                cur = (cur + images.length) % images.length;
                img.setImageResource(images[cur]);
            }
        };