public class MainActivity extends AppCompatActivity { private ImageButton mTrueButton; private ImageButton mFalseButton; private ImageButton mNextButton; private ImageButton mPreviousButton; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate(Bundle) called"); setContentView(R.layout.activity_main); mNextButton = (ImageButton)findViewById(R.id.next_button); mNextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mCurrentIndex != 29) { mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length; updateQuestion(); } } }); updateQuestion(); mPreviousButton = (ImageButton) findViewById(R.id.previous_button); mPreviousButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ try{ mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length; updateQuestion(); if (mCurrentIndex < 0) throw new Exception(); } catch (Exception e){ mCurrentIndex = 0; updateQuestion(); } } }); updateQuestion(); mTrueButton = (ImageButton) findViewById(R.id.imageButtonTrue); mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkAnswer(true); } }); mFalseButton = (ImageButton) findViewById(R.id.imageButtonFalse); mFalseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkAnswer(false); } }); } } 

LOG-CAT: Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button

I do not understand why, all variables are made by ImageButton

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:baselineAligned="true" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:weightSum="1" tools:context="com.example.artek.stringconstant.MainActivity" android:background="#a82f2f"> <TextView android:id="@+id/question_text_view" android:layout_width="match_parent" android:layout_height="164dp" android:padding="24dp" android:layout_weight="0.36" android:layout_gravity="center_vertical" android:textSize="30sp" android:background="#6f6f6f" /> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_gravity="center_horizontal"> <ImageButton android:id="@+id/previous_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/arrow_left" android:contentDescription="@string/previous_button"/> <ImageButton android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow_right" android:contentDescription="@string/next_button" /> </LinearLayout> <RelativeLayout android:layout_width="352dp" android:layout_height="95dp" android:layout_weight="0.52"> <ImageButton android:layout_width="100dp" android:layout_height="95dp" android:id="@+id/imageButtonTrue" android:layout_alignParentTop="false" android:src="@drawable/true_button_press" android:layout_marginLeft="25dp" android:contentDescription="@string/true_button"/> <ImageButton android:layout_width="100dp" android:layout_height="95dp" android:id="@+id/imageButtonFalse" android:src="@drawable/false_button_press" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="27dp" android:layout_marginEnd="27dp" android:contentDescription="@string/false_button" /> </RelativeLayout> </LinearLayout> 

Writes that error on an empty line. This makes no sense

  • Add markup to the question. and a line on which error too designate. - YurySPb
  • Error on empty line? ... 0_o Try to clean the project (build => Clean), restart IDE ... - Yurii
  • Here they write that there may be a problem in Eclipse. The solution is to remove the markup and create it again with the same markup. Those. copy-paste - Yuriy SPb
  • Android Studio I use - Artem Zaichikov
  • And the library of support (AppCompat) which version do you have? If 23.2.0, then try to change it to 23.1.1. The latest version of this contains bugs. - Yuriy SPb

0