我有一個像下面這樣的活動。在這個活動中,我有10個圖像。圖像隨着next和prev按鈕而改變。但我得到這樣的錯誤錯誤:(42,17)錯誤:無法找到符號變量ImageViewPic。請幫助我編寫代碼或編輯我的代碼。錯誤:(42,17)錯誤:無法找到符號變量
這裏是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>
請發佈你的錯誤logcat? – pRaNaY