【问题标题】:my app in android studio is crashing when i come to the part recyclerView.setAdapter(adapter);当我来到 recyclerView.setAdapter(adapter); 部分时,我在 android studio 中的应用程序崩溃了
【发布时间】:2019-07-23 10:52:13
【问题描述】:

我正在尝试从我的在线数据库 backendless.com 获取 android studio 中的一些数据,但问题不在于在线服务,而是当我尝试膨胀数据时。我不知道它是在layaot文件中还是在java类中 这是我的 Java 活动 ViewMyBusiness.java

public class ViewMyBusiness extends AppCompatActivity {

    ListView recyclerView;
    private View mProgressView;
    private View mLoginFormView;
    private TextView tvLoad;

    ContactsAdapter adapter;



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

        recyclerView = findViewById(R.id.recycler_view);

        mLoginFormView = findViewById(R.id.login_form);
        mProgressView = findViewById(R.id.login_progress);
        tvLoad = findViewById(R.id.tvLoad);




       String whereClause = "userEmail = '" + BackendlessCall.user.getEmail() + "'";

        DataQueryBuilder queryBuilder = DataQueryBuilder.create();
        queryBuilder.setWhereClause(whereClause);
        queryBuilder.setGroupBy("companyName");

        showProgress(true);

        Backendless.Persistence.of(Contact.class).find(queryBuilder, new AsyncCallback<List<Contact>>() {
            @Override
            public void handleResponse(List<Contact> response) {

                adapter = new ContactsAdapter(ViewMyBusiness.this, response);
              //the crash is here
                 recyclerView.setAdapter(adapter);
                showProgress(false);
            }

            @Override
            public void handleFault(BackendlessFault fault) {
                Toast.makeText(ViewMyBusiness.this, "ERROR: " + fault.getMessage(), Toast.LENGTH_SHORT).show();
                showProgress(false);
            }
        });

    }

这是联系人适配器 java 类





public class ContactsAdapter extends ArrayAdapter<Contact> {

    private Context context;
    private List<Contact> contacts;

    public ContactsAdapter(Context context, List<Contact> list)
    {
        super(context, R.layout.row_layout, list);
        this.context = context;
        this.contacts = list;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

       convertView = inflater.inflate(R.layout.row_layout, parent, false);

        TextView tvChar = convertView.findViewById(R.id.tvChar);
        TextView tvCName = convertView.findViewById(R.id.tvCName);
        TextView tvBField = convertView.findViewById(R.id.tvBField);

        tvChar.setText(contacts.get(position).getCompanyName().toUpperCase().charAt(0) + "");
        tvCName.setText(contacts.get(position).getCompanyName());
        tvBField.setText(contacts.get(position).getBusinessField());

        return convertView;
    }
}

activity_view_my_business.xml 中列表视图的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".ViewMyBusiness">

    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="200dp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/tvLoad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center_horizontal"
        android:text="Loading...please wait..."
        android:textColor="@color/colorPrimary"
        android:textStyle="bold"
        android:visibility="gone" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/login_form"
        android:orientation="vertical">


        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:gravity="center_horizontal"
            android:text="Business"
            android:textSize="24sp"
            android:textStyle="bold" />

        <ListView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:listitem="@layout/row_layout"/>
    </LinearLayout>


</LinearLayout>


这是数据传递时会膨胀的视图文件 row_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/List"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tvChar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="A"
            android:textColor="@color/ListText"
            android:textSize="48sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="8"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvCName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="TextView"
                android:textColor="@color/ListText"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvBField"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="TextView"
                android:textColor="@color/ListText" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 请添加错误消息/堆栈跟踪。
  • 嗨,欢迎来到 Stack Overflow。你说你的应用程序崩溃了,你能提供给我们来自 Logcat 的错误堆栈跟踪吗?
  • 你设置adapter而不设置layoutmanager,在Recyclerview中需要指定layout manager,可以是线性网格也可以是交错的
  • 谢谢大家,问题已解决,在我的contact.class文件中,我的变量值为null

标签: java android listview crash android-arrayadapter


【解决方案1】:

您在该行中收到 NullPointerException,因为在该行中 -

recyclerView = findViewById(R.id.recycler_view);

您正在为 recyclerView 变量分配空值!

这是因为在activity布局文件中你定义了一个列表视图而不是RecyclerView,修改这部分

         <ListView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:listitem="@layout/row_layout"/>

到这部分xml代码-->

             <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:listitem="@layout/row_layout"/>

要使用 recyclerView 你必须添加这些 -->

实现'com.android.support:recyclerview-v7:28.0.0'

依赖于你的 build.gradle (app:module) 文件

这里是关于 RecyclerView 的官方文档 - Andorid RecyclerView

享受!!

【讨论】:

  • 谢谢这是问题的一部分,但主要问题出在我的contact.class文件中,我将变量值= null;问题已经解决了;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多