Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException: android.support.v7.widget.LinearLayoutCompat$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

I have created an alert dialog with custom layout, it is working fine but when I try to set the single choice item to alertDialogBuilder it gives ClassCastException: android.support.v7.widget.LinearLayoutCompat$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams error

    View viewDialog = LayoutInflater.from(this).inflate(R.layout.dialog_verify, null,false);

    alertDialogBuilder.setView(viewDialog);

    final TextView tvDialogTitle = (TextView) viewDialog.findViewById(R.id.dialog_textview_title);

    final Button btnCancel = (Button) viewDialog.findViewById(R.id.dialog_btn_cancel);
    final Button btnVerify = (Button) viewDialog.findViewById(R.id.dialog_btn_verify);
    btnCancel.setVisibility(View.GONE);
    btnVerify.setText("cancel");
    btnVerify.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));
    btnVerify.setBackgroundResource(R.drawable.round_white);


    //set single layout structure
    alertDialogBuilder.setSingleChoiceItems(arrayReportType, selectedIndex, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            selectedIndex=which;
            tvReportStatus.setText(arrayReportType[selectedIndex]);
            dialog.dismiss();
        }
    });
    alertDialogBuilder.setCancelable(true);
    Typeface font_bold = Typeface.createFromAsset(getAssets(), Constants.FONT_BOLD);
    tvDialogTitle.setTypeface(font_bold);
    tvDialogTitle.setText("Summary");
    final AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();

UPDATE: XML FILE from which I'm inflating the alert Dialog

    <TextView
        android:id="@+id/dialog_textview_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_marginBottom="20dp"/>


<LinearLayout
    android:id="@+id/dialog_ll_buttons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="15dp"
    android:paddingRight="15dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button

            android:fontFamily="sans-serif-light"
            android:id="@+id/dialog_btn_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="16dp"
            android:background="@drawable/round_white"
            android:text="Cancel"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="@color/colorAccent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <Button
            android:fontFamily="sans-serif-light"
            android:id="@+id/dialog_btn_verify"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="16dp"
            android:background="@drawable/round_blue"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="@color/white" />
    </LinearLayout>

   </LinearLayout>

import related to question

import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import org.json.JSONException;
import org.json.JSONObject;
like image 364
rookieDeveloper Avatar asked Jan 07 '17 05:01

rookieDeveloper


1 Answers

ClassCastException

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance .

Don't

import android.support.v7.app.AlertDialog;

DO

import android.app.AlertDialog

NOTE

You are cally support.v7 library .Call Native Library .

like image 123
IntelliJ Amiya Avatar answered Sep 26 '22 01:09

IntelliJ Amiya