java - Android View InflateException 在尝试启动应用程序时出现

标签 java android xml android-recyclerview

我是 Android 开发新手,当我尝试启动应用程序时,我遇到了应用程序崩溃的问题

这是错误消息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cybermath/com.example.cybermath.modules.MainActivity}: android.view.InflateException: Binary XML file line #25: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

这是回收器查看器中数据的 XML 文件:

'''    
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="100"
    android:gravity="center_vertical"
    android:background="@color/colorPrimaryDark"
    >

    <TextView
        android:id="@+id/account_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="70"
        android:background="@color/colorPrimary"
        android:lines="1"
        android:padding="10dp"
        android:text="@string/app_name"
        android:textColor="@color/colorText" >

    </TextView>

    <TextView
        android:id="@+id/account_progress"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:background="@color/colorPrimary"
        android:text="Enter"
        android:textSize="12sp"
        android:textColor="@color/colorText"
        >

    </TextView>
'''     

这是 MainActivity 的 XML:

<androidx.constraintlayout.widget.ConstraintLayout 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="MainActivity">


    <androidx.recyclerview.widget.RecyclerView

        android:layout_width="416dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/informationView"
        android:id="@+id/recyclerView">

    </androidx.recyclerview.widget.RecyclerView>

    <!--<TextView
        android:id="@+id/informationView"
        android:layout_width="408dp"
        android:layout_height="50dp"
        android:foregroundTint="#00050000"
        android:paddingStart="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        android:text="Choose an account"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.333"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </TextView>-->

</androidx.constraintlayout.widget.ConstraintLayout>

这是 MainActivity 的代码:

'''    
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    // UI
    private RecyclerView mRecyclerView;
    // Variables
    @NonNull
    private ArrayList<Account> mAccounts = new ArrayList<>();
    private AccountsRecyclerAdapter mAccountsRecyclerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_account_list);
        mRecyclerView = findViewById(R.id.recyclerView);


        initRecyclerView();
        insertFakeAccounts();
    }

    private void insertFakeAccounts(){
        for(int i = 0; i < 1000; i++){
            Account account = new Account();
            account.setName("title #" + i);
            mAccounts.add(account);
        }
        mAccountsRecyclerAdapter.notifyDataSetChanged();
    }

    private void initRecyclerView(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAccountsRecyclerAdapter = new AccountsRecyclerAdapter(mAccounts);
        mRecyclerView.setAdapter(mAccountsRecyclerAdapter);
    }
'''    

我的适配器公共(public)类 AccountsRecyclerAdapter 扩展了

RecyclerView.Adapter<AccountsRecyclerAdapter.ViewHolder> {

    private ArrayList<Account> mAccounts = new ArrayList<>();

    public AccountsRecyclerAdapter(ArrayList<Account> accounts) {
        this.mAccounts = accounts;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_account_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AccountsRecyclerAdapter.ViewHolder viewHolder, int i) {

        viewHolder.name.setText(mAccounts.get(i).getName());


    }

    @Override
    public int getItemCount() {
        return mAccounts.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.account_name);
        }
    }

最佳答案

有些如果不符合要求可能会导致异常。检查条件为

if(yourValue !=null && yourValue.equals("desired value"){
//do your stuff
}

关于java - Android View InflateException 在尝试启动应用程序时出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60430892/

相关文章:

android - 如何在 NestedScrollView 中使用 RecyclerView?

xml - 如何在不让收件人更改他/她的模式的情况下向 XML 文档添加字段

c# - 在 C# 中如何对 XML 进行编码以在页面的 JavaScript 部分将其输出到 JSON 中

java - 泽西客户端 : How to add a list as query parameter

android - Build.Gradle同步期间的问题查找属性

android - 将 @ActivityScoped 对象注入(inject)到 Fragment 中

jquery - 使用 jQuery 解析 XML

java - Spring boot mongodb应用程序配置

java - 终于在 Java Junit 4 中

java - playframework 上的 JPA 枚举查询错误