0

Всем привет ! Хочу перейти на другое активити через нажатие элемента CardView. На одном активити я создал 3 cardview без использования RecyclerView, потому что на этой активити мне нужны всего лишь 3 кнопки. Я создавал кнопку с айдишкой карточки, только приложение вылетало. Что делать?

Вот джава код

public class CpuActivity extends AppCompatActivity {
Button myArduino;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cpu);
myArduino = (Button) findViewById(R.id.cv1);
myArduino.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intentLoadArduinoActivity = new Intent(CpuActivity.this, 
ArduinoActivity.class);
startActivity(intentLoadArduinoActivity);
}
});
}
}

вот хмл код

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<android.support.v7.widget.CardView
 android:id="@+id/cv1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">

 <RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:padding="16dp">

<ImageView
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_alignParentLeft="true"
   android:layout_alignParentTop="true"
                android:src="@drawable/arduino"
                android:id="@+id/imageView3" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="50sp"
                android:text="Arduino"
                android:textColor="@color/cardview_dark_background"
                android:layout_below="@+id/person_age"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="11dp"
                android:layout_marginEnd="11dp" />

            <TextView
                android:id="@+id/person_age"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/person_name"
                android:layout_toRightOf="@+id/person_photo" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>


    <android.support.v7.widget.CardView
        android:id="@+id/cv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

            <ImageView
                android:layout_width="100sp"
                android:layout_height="100sp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/vex"
                android:id="@+id/imageView" />

            <TextView
                android:id="@+id/person_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="60sp"
                android:text="VEX"
                android:textColor="@color/cardview_dark_background"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="47dp"
                android:layout_marginEnd="47dp" />

            <TextView
                android:id="@+id/person_age"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/person_name"
                android:layout_toRightOf="@+id/person_photo" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>


    <android.support.v7.widget.CardView
        android:id="@+id/cv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

            <ImageView
                android:layout_width="100sp"
                android:layout_height="100sp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/legoev3" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="60sp"
                android:text="LEGO"
                android:textColor="@color/cardview_dark_background"
                android:layout_below="@+id/person_age"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="34dp"
                android:layout_marginEnd="34dp" />

            <TextView
                android:id="@+id/person_age"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/person_name"
                android:layout_toRightOf="@+id/person_photo" />







 </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>
1
  • Не забывайте прикладывать логи ошибки и точную строку их возникновения. В данном случая и без них можно, но в большинстве остальных вы не получите ответа без этой информации
    – ЮрийСПб
    22 июл 2017 в 11:06

2 ответа 2

0

У вас ClassCastException. В разметке у вас CardView, а вы пытаетесь его скастовать к Button.

Вам надо указывать в Java коде тот же класс, который используется в разметке. Т.е. вместо

Button myArduino;
myArduino = (Button) findViewById(R.id.cv1);

Должно быть

CardView myArduino;
myArduino = (CardView) findViewById(R.id.cv1);
0

у тебя R.id.cv1 привязано к CardView, а не кнопке. попробуй сделать так:

CardView myArduino = (CardView) findViewById(R.id.cv1);
myArduino.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intentLoadArduinoActivity = new Intent(CpuActivity.this, 
ArduinoActivity.class);
startActivity(intentLoadArduinoActivity);
}
});

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.