Android 应用无法在某些设备上运行
Posted
技术标签:
【中文标题】Android 应用无法在某些设备上运行【英文标题】:Android app won't work in some of the devices 【发布时间】:2017-05-10 08:15:29 【问题描述】:我创建了一个显示 Firebase 子级的应用。它可以在我的手机(nexus 6)中运行,也可以在另一个(一加 3)中运行,但是当我将它安装在其他设备(如 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_
android:layout_>
<android.support.v7.widget.CardView
android:layout_
android:layout_
android:layout_marginBottom="5dp">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<TextView
android:id="@+id/ItemNametv"
android:layout_
android:layout_
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_
android:layout_
android:layout_margin="14dp"
android:contentDescription="Edit"
android:src="@mipmap/ic_mode_edit_black_18dp" />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal"
android:gravity="end">
<TextView
android:id="@+id/qtytv"
android:layout_
android:layout_
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_
android:layout_
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();
【问题讨论】:
您正在尝试将 ImageView 转换为 Button 从您的错误中可以看出。贴出相关代码 @KostasDrak 我已经用代码更新了问题,我没有按钮。 代码看起来不错,干净,重新构建然后再次在小米上运行...... @MuhammadFaisalHyder 谢谢,我重新构建了代码,它现在可以工作,一个小问题,我如何将其标记为答案,这个社区的新手。 很高兴为您提供帮助,我在下面发布了一个答案,您可以接受它:) cmets 不能被接受为答案。 【参考方案1】:有时,IDE 中会出现问题,即它们会弄乱分配的 ID,并且在某些情况下需要重建、清理甚至使缓存无效并重新启动项目需要。在这种情况下,代码看起来还可以,干净,重新构建,然后再次在小米上运行您的应用程序。
【讨论】:
你能帮我解决这个问题吗[问题] (***.com/questions/41316964/…)以上是关于Android 应用无法在某些设备上运行的主要内容,如果未能解决你的问题,请参考以下文章