I am getting a error while using a floating button inside a card view and applying that card inside a recycler view

huangapple go评论64阅读模式
英文:

I am getting a error while using a floating button inside a card view and applying that card inside a recycler view

问题

以下是您提供的代码的翻译:

package com.example.cako;

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        String[] x = {"Aaran", "Aaren", "Aarez", "Aarman", "Aaron", "Aaron-James", "Aarron", "Aaryan", "Abhinav", "Ankit"};
        String[] image = {
            "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Aloo_Methi_%28Aaloo_Methi%29.JPG/180px-Aloo_Methi_%28Aaloo_Methi%29.JPG",
            "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Spicy_alloo_with_tadka_mirchi.jpg/180px-Spicy_alloo_with_tadka_mirchi.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg",
            "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg"
        };

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        RecyclerView horizontalRecyclerView = findViewById(R.id.recyclerViewHorizontal);
        horizontalRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        horizontalRecyclerView.setAdapter(new RecyclerVview(getApplicationContext(), x, image));

        RecyclerView verticalRecyclerView = findViewById(R.id.recyclerViewVertical);
        verticalRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        verticalRecyclerView.setAdapter(new RecyclerVview(getApplicationContext(), x, image));
    }
}

class RecyclerVview extends RecyclerView.Adapter<RecyclerVview.Holder> {
    Context context;
    String[] names;
    String[] images;

    public RecyclerVview(Context applicationContext, String[] x, String[] image) {
        context = applicationContext;
        names = x;
        images = image;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_for_horizontal_recyclerview, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.textViewForItemName.setText(names[position]);
        Glide.with(context).load(images[position]).into(holder.image);
        holder.floatingActionButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(context, "yesWorking", Toast.LENGTH_SHORT);
                return true;
            }
        });
        holder.textViewForPrice.setText("30$");
        holder.textViewForPieceInfo.setText("Single Piece Only.");
    }

    @Override
    public int getItemCount() {
        return names.length;
    }

    public class Holder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView textViewForItemName;
        FloatingActionButton floatingActionButton;
        TextView textViewForPrice;
        TextView textViewForPieceInfo;

        public Holder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.imageView);
            textViewForItemName = itemView.findViewById(R.id.textView4);
            floatingActionButton = itemView.findViewById(R.id.floatingActionButton);
            textViewForPieceInfo = itemView.findViewById(R.id.textViewForPieceInfo);
            textViewForPrice = itemView.findViewById(R.id.textViewforPrice);
        }
    }
}

我已经按照您的要求,将代码部分进行了翻译,不包含其他内容。如果您有任何其他问题或需要进一步的帮助,请随时告知。

英文:

Getting this error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cako, PID: 7165
android.view.InflateException: Binary XML file line #73 in com.example.cako:layout/layout_for_horizontal_recyclerview: Binary XML file line #73 in com.example.cako:layout/layout_for_horizontal_recyclerview: Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton
Caused by: android.view.InflateException: Binary XML file line #73 in com.example.cako:layout/layout_for_horizontal_recyclerview: Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:852)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at com.example.cako.RecyclerVview.onCreateViewHolder(FirstActivity.java:72)
at com.example.cako.RecyclerVview.onCreateViewHolder(FirstActivity.java:58)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
at android.view.View.measure(View.java:25466)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:763)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:426)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measureChildren(BasicMeasure.java:105)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:247)
at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:117)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1532)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1607)
at android.view.View.measure(View.java:25466)
at androidx.core.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1599)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at androidx.core.widget.NestedScrollView.onMeasure(NestedScrollView.java:585)
at android.view.View.measure(View.java:25466)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:763)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:426)
E/AndroidRuntime:     at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.measureChildren(BasicMeasure.java:105)
at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:247)
at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:117)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1532)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1607)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:747)
at android.view.View.measure(View.java:25466)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3397)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2228)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2486)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
at com.google.android.material.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:213)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:148)
at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:76)
at com.google.android.material.floatingactionbutton.FloatingActionButton.&lt;init&gt;(FloatingActionButton.java:212)
at com.google.android.material.floatingactionbutton.FloatingActionButton.&lt;init&gt;(FloatingActionButton.java:201)
... 83 more
I/Process: Sending signal. PID: 7165 SIG: 9

XML code for layout_for_horizontal_recyclerview.xml:-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;androidx.cardview.widget.CardView
android:id=&quot;@+id/cardView&quot;
android:layout_width=&quot;150dp&quot;
android:layout_height=&quot;200dp&quot;
app:cardBackgroundColor=&quot;@color/CardColor&quot;
app:cardCornerRadius=&quot;25sp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:id=&quot;@+id/constraint&quot;
android:layout_width=&quot;130dp&quot;
android:layout_height=&quot;180dp&quot;
android:layout_gravity=&quot;center&quot;
android:background=&quot;@android:color/white&quot;
android:padding=&quot;5dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/imageView&quot;
android:layout_width=&quot;90dp&quot;
android:layout_height=&quot;100dp&quot;
android:background=&quot;@android:color/holo_orange_light&quot;
android:foregroundGravity=&quot;center_horizontal&quot;
android:scaleType=&quot;fitXY&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView4&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:fontFamily=&quot;@font/playfair_display_bold_italic&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Great&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;12sp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/imageView&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textViewForPieceInfo&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginTop=&quot;5dp&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView4&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textViewforPrice&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:layout_marginTop=&quot;5dp&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textViewForPieceInfo&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;com.google.android.material.floatingactionbutton.FloatingActionButton
android:id=&quot;@+id/floatingActionButton&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginEnd=&quot;25dp&quot;
android:layout_marginBottom=&quot;20dp&quot;
app:fabSize=&quot;mini&quot;
app:layout_anchor=&quot;@id/cardView&quot;
app:layout_anchorGravity=&quot;bottom|end&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:srcCompat=&quot;@drawable/ic_baseline_add_24&quot; /&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

and my FirstActivity.java file which contains recycler view which imlements that card is:

package com.example.cako;
public class FirstActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
String []x={&quot;Aaran&quot;, &quot;Aaren&quot;, &quot;Aarez&quot;, &quot;Aarman&quot;, &quot;Aaron&quot;, &quot;Aaron-James&quot;, &quot;Aarron&quot;, &quot;Aaryan&quot;, &quot;Abhinav&quot;,&quot;Ankit&quot;};
String []image= {&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Aloo_Methi_%28Aaloo_Methi%29.JPG/180px-Aloo_Methi_%28Aaloo_Methi%29.JPG&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Spicy_alloo_with_tadka_mirchi.jpg/180px-Spicy_alloo_with_tadka_mirchi.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg&quot;,
&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg&quot;
};
Toolbar toolbar=findViewById(R.id.toolbar) ;
setSupportActionBar(toolbar);
RecyclerView horizontalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewHorizontal);
horizontalrecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));
horizontalrecyclerView.setAdapter(new RecyclerVview(getApplicationContext(),x,image));
RecyclerView verticalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewVertical);
verticalrecyclerView.setLayoutManager(new GridLayoutManager(this,2));
verticalrecyclerView.setAdapter(new RecyclerVview(getApplicationContext(),x,image));
}
}
class RecyclerVview extends RecyclerView.Adapter&lt;RecyclerVview.Holder&gt;{
Context co;
String[] name;
String[] image;
public RecyclerVview(Context applicationContext, String[] x, String[] image) {
co=applicationContext;
name=x;
this.image=image;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v=LayoutInflater.from(co).inflate(R.layout.layout_for_horizontal_recyclerview,parent,false);
return new Holder(v);
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {
holder.textViewForItemName.setText(name[position]);
Glide.with(co).load(image[position]).into(holder.image);
holder.floatingActionButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Toast.makeText(co,&quot;yesWorking&quot;,Toast.LENGTH_SHORT);
return true;
}
});
holder.textViewForPrice.setText(&quot;30$&quot;);
holder.textViewForPieceInfo.setText(&quot;Single Piece Only.&quot;);
}
@Override
public int getItemCount() {
return name.length;
}
public class Holder extends RecyclerView.ViewHolder{
ImageView image;
TextView textViewForItemName;
FloatingActionButton floatingActionButton;
TextView textViewForPrice;
TextView textViewForPieceInfo;
public Holder(@NonNull View itemView) {
super(itemView);
image=itemView.findViewById(R.id.imageView);
textViewForItemName=itemView.findViewById(R.id.textView4);
floatingActionButton=itemView.findViewById(R.id.floatingActionButton);
textViewForPieceInfo=itemView.findViewById(R.id.textViewForPieceInfo);
textViewForPrice=itemView.findViewById(R.id.textViewforPrice);
}
}
}

答案1

得分: 1

在适配器中使用的上下文存在问题。

// 水平RecyclerView适配
horizontalrecyclerView.setAdapter(new RecyclerVview(getApplicationContext(), x, image));
// 垂直RecyclerView适配
verticalrecyclerView.setAdapter(new RecyclerVview(getApplicationContext(), x, image));

您需要传入 Activity,而不是 ApplicationContext
ApplicationContext 没有应用程序主题。

英文:

There is an issue with the context used in the Adapter.

>horizontalrecyclerView.setAdapter(new RecyclerVview(getApplicationContext(),x,image));
> verticalrecyclerView.setAdapter(new RecyclerVview(getApplicationContext(),x,image));

You have to pass in the Activity, not an ApplicationContext.
The ApplicationContext hasn't the app theme.

huangapple
  • 本文由 发表于 2020年10月13日 02:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64323144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定