简体   繁体   English

编码时没有错误显示,但在我登录主页后会崩溃。 logcat 显示错误在 inflate 行中

[英]There is no error show in coding but it will crash after I login to my home page. The logcat shows that the error is in the inflate line

There is no error show in coding but it will crash after I login to my home page.编码时没有错误显示,但在我登录主页后会崩溃。 The logcat shows that the error is in the inflate line. logcat 显示错误在 inflate 行中。

Here is my code:这是我的代码:

package com.example.jj.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.jj.R;
import com.example.jj.adapter.PostAdapter;
import com.example.jj.model.Post;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends Fragment {

    private RecyclerView recyclerViewPosts;
    private PostAdapter postAdapter;
    private List<Post> postList;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        

        recyclerViewPosts = view.findViewById(R.id.recycle_view_post);
        recyclerViewPosts.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setStackFromEnd(true);
        linearLayoutManager.setReverseLayout(true);
        recyclerViewPosts.setLayoutManager(linearLayoutManager);
        postList = new ArrayList<>();
        postAdapter = new PostAdapter(getContext(), postList);
        recyclerViewPosts.setAdapter(postAdapter);

        readPosts();

        return view;
    }

    private void readPosts() {

        FirebaseDatabase.getInstance().getReference().child("Posts").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                postList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    Post post = snapshot.getValue(Post.class);

                    postList.add(post);
                }

                postAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

    }
}

and also my xml file here:还有我的 xml 文件:

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

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bar"
        android:background="?android:attr/windowBackground"
        tools:ignore="DuplicateIds">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toolbar"
            android:background="?android:attr/background">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="180dp"
                    android:layout_height="30dp"
                    android:layout_centerHorizontal="true"
                    android:layout_marginStart="-20dp"
                    android:src="@drawable/logo"/>

            </RelativeLayout>

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bar"
        tools:ignore="DuplicateIds">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:id="@+id/recycle_view_post"/>


        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</RelativeLayout>

How the error code looks like:错误代码的样子:

Process: com.example.jj, PID: 8988
    android.view.InflateException: Binary XML file line #18 in com.example.jj:layout/fragment_home: Binary XML file line #18 in com.example.jj:layout/fragment_home: Error inflating class <unknown>
    Caused by: android.view.InflateException: Binary XML file line #18 in com.example.jj:layout/fragment_home: Error inflating class <unknown>
    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.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at com.example.jj.fragment.HomeFragment.onCreateView(HomeFragment.java:38)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
        at android.app.Activity.performStart(Activity.java:8018)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        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.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x10100d4 a=-1}
        at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:997)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:981)
        at android.view.View.<init>(View.java:5535)
        at android.view.ViewGroup.<init>(ViewGroup.java:697)
        at android.view.ViewGroup.<init>(ViewGroup.java:693)
        at androidx.appcompat.widget.Toolbar.<init>(Toolbar.java:237)
        at androidx.appcompat.widget.Toolbar.<init>(Toolbar.java:233)

Is there any solution for this as I have already check some similar question but looks like those solutions are not working for me...是否有任何解决方案,因为我已经检查了一些类似的问题,但看起来这些解决方案对我不起作用......

follow this link android.view.InflateException: Binary XML file: Error inflating class fragment点击此链接android.view.InflateException: Binary XML file: Error inflating class fragment

Your NestedScrollView and AppBarLayout have the same id.您的 NestedScrollView 和 AppBarLayout 具有相同的 ID。

android:id="@+id/bar"

Change the id and it may resolve the issues.更改 id,它可能会解决问题。

After reproducing the error, change the background color of the toolbar something like below:重现错误后,更改工具栏的背景颜色,如下所示:

<androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
         <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="180dp"
                android:layout_height="30dp"
                android:layout_centerHorizontal="true"
                android:layout_marginStart="-20dp"
                android:src="@drawable/ic_info" />
        </RelativeLayout>
 </androidx.appcompat.widget.Toolbar>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 firebase 实时数据库未存储数据且 logcat 未显示错误 - firebase realtime database is not storing data and the logcat shows no error 我的 AuthContext 工作了一段时间,并显示了显示名称,然后在页面刷新后出现此错误 - My AuthContext works for a while, and shows the displayname, then i get this error once the page refresh React Azure AD登录后跳转到首页 - Redirect to home page after React Azure AD login 重新加载私有路由页面后,用户将被重定向到登录页面。 [反应路由器] - after reloading the page of a private route, the user is redirected to the login page. [react router] 在 flutter 中通过身份验证后将用户从登录/注册页面导航到主页的正确方法 - Proper way of navigating user from login/signup page to home page after authentication in flutter Angular 应用程序在 Azure 部署后显示错误 - Angular app shows error after Azure deploy 我正在尝试处理登录页面中的异常,但它不起作用 - I am trying to handle exceptions in my login page but its not working 如何在以角度登录后重定向到主页,而不是在代码 aas 下工作,因为它应该是 - How to rediect to home page after login in angular, not working beow code aas it shoud be 图像不显示在图像视图中并显示以下错误 - Image Doesn't show in Image view and shows following error 注销后出错并尝试在 Firebase DB 上再次登录 - Error after logout and try to login again on Firebase DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM