Android应用程序无法在某些设备上运行

时间:2016-12-25 13:29:32

标签: android android-layout android-intent android-activity android-actionbar

我创建了一个显示firebase子项的应用程序。它在我的手机(nexus 6)中工作,在另一个(一加三)中工作,但是当我在其他设备上安装它时,例如redmi note 3,应用程序强制关闭,我不知道为什么会发生这种情况。

logcat的:

java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView cannot be cast to android.widget.Button
    at com.fayaz.uniabex.ItemDetailsAdapter.getView(ItemDetailsAdapter.java:58)
    at android.widget.AbsListView.obtainView(AbsListView.java:2346)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1281)
    at android.widget.ListView.onMeasure(ListView.java:1188)
    at android.view.View.measure(View.java:18811)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18811)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
    at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
    at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1192)
    at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:733)
    at android.view.View.measure(View.java:18811)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
    at android.view.View.measure(View.java:18811)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18811)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.view.View.measure(View.java:18811)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18811)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
    at android.view.View.measure(View.java:18811)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2127)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1243)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1479)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6045)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
    at android.view.Choreographer.doCallbacks(Choreographer.java:672)
    at android.view.Choreographer.doFrame(Choreographer.java:608)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5442)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

这是我的git存储库Uniabex app

这是卡片视图布局,其中没有按钮,因为我昨天删除了按钮并使用ImageViews进行了更新

list_item.xml

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

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/ItemNametv"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="start"
                android:padding="16dp"
                android:textStyle="bold"
                android:textSize="18sp"
                android:text="@string/item_nam" />

            <ImageView
                android:id="@+id/EditItem"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_margin="14dp"
                android:contentDescription="Edit"
                android:src="@mipmap/ic_mode_edit_black_18dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="end">

            <TextView
                android:id="@+id/qtytv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="left"
                android:padding="16dp"
                android:textSize="16sp"
                android:text="@string/qty" />

            <ImageView
                android:id="@+id/DeleteItem"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:layout_margin="14dp"
                android:contentDescription="Delete"
                android:src="@mipmap/ic_delete_black_18dp" />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

ItemDetailsAdapter.java

package com.fayaz.uniabex;

    /**
     * Created by Fayaz on 21/12/2016.
     */

    import android.widget.BaseAdapter;

    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.support.v7.app.AlertDialog;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;

    import java.util.ArrayList;

public class ItemDetailsAdapter extends BaseAdapter {
private ArrayList<Item> arrayListItem;
private Context context;
private LayoutInflater inflater;

public ItemDetailsAdapter(Context context, ArrayList<Item> arrayListItem) {
    this.context = context;
    this.arrayListItem = arrayListItem;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return arrayListItem.size();
}

@Override
public Object getItem(int position) {
    return arrayListItem.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = convertView;
    Holder holder;
    if (v == null) {
        v = inflater.inflate(R.layout.list_item, null);
        holder = new Holder();
        holder.ItemName = (TextView) v.findViewById(R.id.ItemNametv);
        holder.qty = (TextView) v.findViewById(R.id.qtytv);
        holder.EditItem = (ImageView) v.findViewById(R.id.EditItem);
        holder.DeleteItem = (ImageView) v.findViewById(R.id.DeleteItem);
        v.setTag(holder);
    } else {
        holder = (Holder) v.getTag();
    }

    holder.ItemName.setText(arrayListItem.get(position).getItem());
    holder.qty.setText(arrayListItem.get(position).getQty());
    holder.EditItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context,AddOrUpdateItem.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("Position", position);
            context.getApplicationContext().startActivity(intent);
        }
    });
    holder.DeleteItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ShowConfirmDialog(context, position);
        }
    });
    return v;
}

class Holder {
    TextView ItemName,qty;
    ImageView DeleteItem, EditItem;
}

public static void ShowConfirmDialog(Context context, final int position) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    alertDialogBuilder
            .setMessage("Are you sure you want to delete this entry?")
            .setCancelable(true)
            .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    MainActivity.getInstance().deleteItem(position);
                }
            })
            .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}
}

1 个答案:

答案 0 :(得分:0)

有时,IDE会出现问题,即它们会使指定的ID混乱,需要重建项目需求,清理甚至使缓存无效,并在某些情况下重新启动。在这种情况下,代码看起来还不错,干净,重新构建然后再次在小米上运行你的应用程序。