繁体   English   中英

尝试启动应用程序时显示 Android View InflateExeption

[英]Android View InflateExeption showing up when trying to start an app

我是 android 开发的新手,当我尝试启动它时,我的应用程序崩溃了

这是错误消息:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.cybermath/com.example.cybermath.modules.MainActivity}:android.view.InflateException:二进制 XML 文件第 25 行:尝试调用虚拟方法 'boolean java .lang.String.equals(java.lang.Object)' 在空对象引用上

这是回收站查看器中数据的 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);
    }
'''    

我的适配器公共类 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
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM