当我在真实设备中打开活动时应用程序崩溃

Posted

技术标签:

【中文标题】当我在真实设备中打开活动时应用程序崩溃【英文标题】:App crashes when i open the activity in real device 【发布时间】:2017-09-10 17:08:26 【问题描述】:

我认为问题在于 xml 文件。应用程序在模拟器中运行良好,但在我尝试打开另一个活动时在真实设备上崩溃。下面是我的 xml 和 logcat。我正在从 firebase 检索数据,但是当我打开这个活动时,它只在手机上崩溃,但在模拟器上工作正常。下面的设计视图显示未能实例化一个或多个类。

android.content.res.Resources$NotFoundException
    at com.android.layoutlib.bridge.android.BridgeContext.obtainStyledAttributes(BridgeContext.java:681)
    at android.content.res.Resources_Theme_Delegate.obtainStyledAttributes(Resources_Theme_Delegate.java:71)
    at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java:1436)
    at android.content.Context.obtainStyledAttributes(Context.java:587)
    at android.support.v7.widget.TintTypedArray.obtainStyledAttributes(TintTypedArray.java:59)
    at android.support.design.widget.CollapsingTextHelper.setExpandedTextAppearance(CollapsingTextHelper.java:231)
    at android.support.design.widget.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.java:197)
    at android.support.design.widget.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.java:143)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:475)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:262)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:220)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:186)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:334)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:345)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:245)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:858)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:861)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:368)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:567)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:549)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:863)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:549)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$1(RenderTask.java:680)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

XML 代码

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:context="com.food.sheenishere.stark.FoodDetail"
    >



    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_
        android:layout_
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        >
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing"
            android:layout_
            android:layout_
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="#0e0d0e"
            app:expandedTitleTextAppearance="@android:color/transparent"
            >

            <ImageView
                android:id="@+id/img_food"
                android:layout_
                android:layout_
                android:contentDescription="@null"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:title="Food Name"
                android:layout_
                android:layout_
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="parallax"

                tools:ignore="HardcodedText">


            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btnCart"
        android:src="@drawable/ic_shopping_cart_black_24dp"
        android:backgroundTint="@android:color/white"
        android:layout_
        android:layout_
        app:elevation="6dp"
        app:pressedTranslationZ="12dp"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom|right|end"
        app:useCompatPadding="true"
        tools:ignore="UnusedAttribute,VectorDrawableCompat" />

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:clipToPadding="false"
        android:layout_
        android:layout_
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >


        <LinearLayout
            android:orientation="vertical"
            android:layout_
            android:layout_>


            <android.support.v7.widget.CardView
                android:layout_
                android:layout_
                app:cardElevation="5dp"
                app:cardUseCompatPadding="true"

                >

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_
                    android:layout_>

                    <TextView
                        android:id="@+id/food_name"
                        android:layout_marginTop="8dp"
                        android:padding="12dp"
                        android:text="Food Name"
                        android:textColor="@color/colorPrimary"
                        android:textSize="20sp"
                        android:textStyle="bold"
                        android:layout_
                        android:layout_
                        tools:ignore="HardcodedText" />
                    <LinearLayout
                        android:id="@+id/layout_price"
                        android:orientation="horizontal"
                        android:layout_
                        android:layout_>


                        <ImageView
                            android:src="@drawable/ic_attach_money_black_24dp"
                            android:layout_weight="1"
                            android:layout_
                            android:layout_
                            tools:ignore="ContentDescription,VectorDrawableCompat" />

                        <TextView
                            android:id="@+id/food_price"
                            android:text="1000"
                            android:textColor="@color/colorPrimary"
                            android:textSize="18sp"
                            android:textStyle="bold"
                            android:layout_
                            android:layout_weight="9"
                            android:layout_
                            tools:ignore="HardcodedText" />
                    </LinearLayout>

                    <com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
                        android:layout_
                        android:layout_
                        android:id="@+id/number_button"
                        android:layout_marginTop="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginBottom="18dp"
                        app:textSize="8sp"
                        app:backGroundColor="#0e0d0e"
                        app:initialNumber="1"
                        app:finalNumber="20"

                        tools:ignore="RtlHardcoded,SmallSp">

                    </com.cepheuen.elegantnumberbutton.view.ElegantNumberButton>
                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                app:cardElevation="5dp"
                app:cardUseCompatPadding="true"
                android:layout_
                android:layout_>

                <TextView
                    android:id="@+id/food_description"
                    android:layout_marginTop="12dp"
                    android:lineSpacingMultiplier="1.5"
                    android:padding="12dp"
                    android:text="Description"
                    android:textColor="@android:color/black"
                    android:textSize="14sp"
                    android:layout_
                    android:layout_
                    tools:ignore="HardcodedText" />

            </android.support.v7.widget.CardView>
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

FoodDetail.java

    package com.food.sheenishere.stark;

import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.food.sheenishere.stark.Model.Food;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;

public class FoodDetail extends AppCompatActivity 


    TextView food_name,food_price,food_description;
    ImageView food_image;
    CollapsingToolbarLayout collapsingToolbarLayout;
    FloatingActionButton btnCart;
    ElegantNumberButton numberButton;

    String foodId="";
    FirebaseDatabase database;
    DatabaseReference foods;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);

        //firebase
        database=FirebaseDatabase.getInstance();
        foods=database.getReference("Foods");

        //init view
        numberButton=(ElegantNumberButton)findViewById(R.id.number_button);
        btnCart=(FloatingActionButton)findViewById(R.id.btnCart);

        food_description=(TextView)findViewById(R.id.food_description);
        food_name= (TextView)findViewById(R.id.food_name);
        food_price= (TextView)findViewById(R.id.food_price);
        food_image=(ImageView)findViewById(R.id.img_food);


        collapsingToolbarLayout=(CollapsingToolbarLayout)findViewById(R.id.collapsing);
        collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
        collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);


        //get food id from intent

        if (getIntent() !=null)
            foodId =getIntent().getStringExtra("FoodId");
        if (!foodId.isEmpty())
        
            getDetailFood(foodId);
        
    

    private void getDetailFood(String foodId) 
        foods.child(foodId).addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) 
                Food food =dataSnapshot.getValue(Food.class);
                //set image
                Picasso.with(getBaseContext()).load(food.getImage())
                        .into(food_image);
                food_price.setText(food.getPrice());

                food_name.setText(food.getName());

                food_description.setText(food.getDescription());
            

            @Override
            public void onCancelled(DatabaseError databaseError) 

            
        );
    

【问题讨论】:

在 FoodDetail.java:34,你是如何放大视图的? @AndrewChiLam 我也用 java 代码编辑了我的问题 ,检查库导入是否正确 编译'com.cepheuen.elegant-number-button:lib:1.0.2' @AndrewChiLam 先生,我已经编辑了我的 logcat 错误 【参考方案1】:

而不是这个

android:backgroundTint="@android:color/white"

写这个

app:backgroundTint="@android:color/white"

【讨论】:

以上是关于当我在真实设备中打开活动时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

当我尝试实现滚动条时,我的应用程序崩溃了

应用程序在模拟器上运行良好,但在真实设备上崩溃

iOS 应用程序在真实设备上崩溃。错误:dyld`__abort_with_payload

Android Camerax 在真实设备上崩溃

如何获取 iPhone 应用程序的崩溃报告

Play商店预发布报告中的Android应用程序崩溃但在真实设备中工作