1
問題はxmlファイルにあると思います。アプリケーションはエミュレータでうまく動作しますが、別のアクティビティを開こうとすると実際のデバイスがクラッシュします。以下は私のxmlとlogcatです。私はfirebaseからデータを取得していますが、私がこのアクティビティを開いたときには電話でしかクラッシュしませんが、エミュレータで正常に動作します。以下のデザインビューは、1つまたは複数のクラスのインスタンス化に失敗したことを示しています。実際のデバイスでアクティビティを開くとアプリケーションがクラッシュする
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_width="match_parent"
android:layout_height="match_parent"
tools:context="com.food.sheenishere.stark.FoodDetail"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing"
android:layout_width="match_parent"
android:layout_height="350dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#0e0d0e"
app:expandedTitleTextAppearance="@android:color/transparent"
>
<ImageView
android:id="@+id/img_food"
android:layout_width="match_parent"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="?attr/actionBarSize"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="HardcodedText" />
<LinearLayout
android:id="@+id/layout_price"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/ic_attach_money_black_24dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
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_width="0dp"
android:layout_weight="9"
android:layout_height="wrap_content"
tools:ignore="HardcodedText" />
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:layout_width="100dp"
android:layout_height="30dp"
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_width="match_parent"
android:layout_height="wrap_content">
<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_width="wrap_content"
android:layout_height="wrap_content"
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) {
}
});
}
}
は、どのようにビューを膨張していますか? –
こちらをご覧くださいhttps://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-itそして、クラッシュするJavaを提供する必要があります –
@AndrewChiLam私はJavaコードで私の質問を編集しました –