Android:自定义列表视图无法正常工作

Posted

技术标签:

【中文标题】Android:自定义列表视图无法正常工作【英文标题】:Android: Custom List View is not working properly 【发布时间】:2019-10-24 21:01:39 【问题描述】:

很抱歉没有提供正确的源代码。

基本上我在列表视图 xml 布局中拥有的是:

listleave.xml leaveSummary.xml 离开分贝 自定义适配器 申请休假

我正在创建一个具有列表视图的应用程序,当我运行我的应用程序时,我在 Locgat 中遇到错误。

Caused by:java.lang.NullPointerException: Attempt to invoke virtual 
method 'void 
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null 
object reference
at com.example.huzai.civilsoft1.ApplyLeave.loadDataInListView(ApplyLeave.java:258 )
 at com.example.huzai.civilsoft1.ApplyLeave.onCreate(ApplyLeave.java:84)

loadDataInListView 行中,当我不评论 l1.setAdapter(customAdapter); 时,应用程序崩溃,但当我评论时它可以正常工作。

不评论结果:l1.setAdapter(customAdapter);

    ApplyLeave 活动

    public class ApplyLeave extends AppCompatActivity 
    LeaveService myDb;
    //Declaration EditTexts
    EditText editstartDate;
    EditText   editendDate;
    DatePickerDialog datePickerDialog;
    CheckBox checkgoingAbroad;
    Spinner spinnertypess;
    Spinner editleavess;
    EditText editremarks;
    EditText editnoOfDays1;
    TextView noOfDays;
    TextView endd;
    Button btnApply;
    Button addfield;
    Button deletefield;
    LinearLayout parentlinearlayout;
    ListView l1;
    ArrayList<Leave> arrayList;
    CustomAdapter customAdapter;
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
     protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_leave);
    myDb = new LeaveService (this);
     checkgoingAbroad =(CheckBox) findViewById(R.id.GoingAbroad);
    spinnertypess = (Spinner)findViewById(R.id.types);
    editleavess = (Spinner)findViewById(R.id.Leaves);
    editremarks = (EditText)findViewById(R.id.reason);
    editnoOfDays1 = (EditText)findViewById(R.id.noOfDays);
    parentlinearlayout = 
    (LinearLayout)findViewById(R.id.parent_linear_layout);
    addfield = (Button)findViewById(R.id.addField);
    deletefield = (Button)findViewById(R.id.removeField);
    endd = (TextView)findViewById(R.id.date);
    l1 = (ListView)findViewById(R.id.leaveApplicationSummary);
    btnApply = (Button)findViewById(R.id.apply);
    AddData();
    arrayList = new ArrayList<>();
    loadDataInListView();
    
     //Created a method for loadDataInListView
    private void loadDataInListView() 
        arrayList = myDb.getAllData();
        customAdapter = new CustomAdapter(this,arrayList);
        l1.setAdapter(customAdapter);
        customAdapter.notifyDataSetChanged();
    
    private void AddData() 
        btnApply.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
            boolean isInserted = myDb.insertData("", checkgoingAbroad.toString(), spinnertypess.toString(), editleavess.toString(), editnoOfDays1.getText().toString(), editstartDate.getText().toString(), editendDate.getText().toString(), editremarks.getText().toString());
            if (isInserted == true)
    
                Toast.makeText(ApplyLeave.this, "Data Inserted Successfully",Toast.LENGTH_LONG).show();
             else 
                Toast.makeText(ApplyLeave.this, "Data Not Inserted",Toast.LENGTH_LONG).show();
            
        
    );
    

评论结果://l1.setAdapter(customAdapter);

    CustomAdapter 类

    public class CustomAdapter extends BaseAdapter 
    
    Context context;
    
    ArrayList<Leave> arrayList;
    
    public CustomAdapter(Context context, ArrayList<Leave>arraylist) 
    
    this.context = context;
    
    this.arrayList = arraylist;
    
    
    @Override
    public int getCount() 
    
    return this.arrayList.size();
    
    
    @Override
    public Object getItem(int position) 
    
    return arrayList.get(position);
    
    
    @Override
    public long getItemId(int position) 
    
    return position;
    
    
    @SuppressLint("ViewHolder")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.leavesummary, null);
        TextView types = (TextView)convertView.findViewById(R.id.textViewtypes);
        TextView startDate = (TextView)convertView.findViewById(R.id.textViewstartDate);
        TextView endDate = (TextView)convertView.findViewById(R.id.textViewendDate);
        TextView leavetype = (TextView)convertView.findViewById(R.id.textViewleaveType);
        TextView noOfDays = (TextView)convertView.findViewById(R.id.textViewnoOfDays);
        TextView reason = (TextView)convertView.findViewById(R.id.textViewreason);
    
    
    
        Leave leave = arrayList.get(position);
        types.setText(leave.getTypes());
        startDate.setText(leave.getStartDate());
        endDate.setText(leave.getEndDate());
        leavetype.setText(leave.getLeaves());
        noOfDays.setText(leave.getNoOfDays());
        reason.setText(leave.getReason());
    
    return convertView;
           
           
    

    listleave.xml 这里我使用的是 ConstraintLayout

     <android.support.constraint.ConstraintLayout
    
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_
     android:layout_
     android:background="@drawable/background">
    
       <ListView
    
    android:layout_
    android:layout_
    android:id="@+id/leaveApplicationSummary">
    </ListView>
    
    </android.support.constraint.ConstraintLayout>
    

    leaveSummary.xml

         <android.support.constraint.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_
           android:layout_
          android:background="@drawable/background">
        <TextView
        android:layout_
        android:layout_
        android:text="Leave Application Summary"
        android:textColor="#080808"
        android:textSize="20dp"/>
    <TextView
    android:id="@+id/textView1"
    android:layout_
    android:layout_
    android:layout_marginStart="28dp"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="72dp"
    android:text="Applicant Name :"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
    android:id="@+id/textViewApplicantName"
    android:layout_
    android:layout_
    android:layout_marginStart="188dp"
    android:layout_marginLeft="188dp"
    android:layout_marginTop="72dp"
    android:text="Huzaifa  [001]"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
    android:id="@+id/textView2"
    android:layout_
    android:layout_
    android:layout_marginStart="28dp"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="116dp"
    android:text="Type of Leave :"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
    android:id="@+id/textViewtypes"
    android:layout_
    android:layout_
    android:layout_marginStart="180dp"
    android:layout_marginLeft="180dp"
    android:layout_marginTop="116dp"
    android:text="TextView"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
    android:id="@+id/textView3"
    android:layout_
    android:layout_
    android:layout_marginStart="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="164dp"
    android:text="Leave Start Date :"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
    android:id="@+id/textViewstartDate"
    android:layout_
    android:layout_
    android:layout_marginStart="188dp"
    android:layout_marginLeft="188dp"
    android:layout_marginTop="164dp"
    android:text="TextView"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
    android:id="@+id/textView4"
    android:layout_
    android:layout_
    android:layout_marginStart="28dp"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="208dp"
    android:text="Rejoining Date :"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
    android:id="@+id/textViewendDate"
    android:layout_
    android:layout_
    android:layout_marginStart="180dp"
    android:layout_marginLeft="180dp"
    android:layout_marginTop="208dp"
    android:text="TextView"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
    android:id="@+id/textView5"
    android:layout_
    android:layout_
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="248dp"
    android:text="Leave Type :"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
    android:id="@+id/textViewleaveType"
    android:layout_
    android:layout_
    android:layout_marginStart="180dp"
    android:layout_marginLeft="180dp"
    android:layout_marginTop="248dp"
    android:text="TextView"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
    android:id="@+id/textView6"
    android:layout_
    android:layout_
    android:layout_marginStart="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="288dp"
    android:text="Total Days Applied :"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
     <TextView
    android:id="@+id/textViewnoOfDays"
    android:layout_
    android:layout_
    android:layout_marginStart="216dp"
    android:layout_marginLeft="216dp"
    android:layout_marginTop="288dp"
    android:text="TextView"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
     <TextView
    android:id="@+id/textView7"
    android:layout_
    android:layout_
    android:layout_marginStart="28dp"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="332dp"
    android:text="Remarks :"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
     <TextView
    android:id="@+id/textViewreason"
    android:layout_
    android:layout_
    android:layout_marginStart="144dp"
    android:layout_marginLeft="144dp"
    android:layout_marginTop="332dp"
    android:text="TextView"
    android:textColor="#080808"
    android:textSize="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    <LinearLayout
    android:layout_
    android:layout_
    android:gravity="bottom"
    android:layout_margin="15dp"
    android:orientation="vertical">
    
     <LinearLayout
    android:layout_
    android:layout_>
    
        <Button
        android:id="@+id/delete"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:text="Delete" />
    
        <Button
        android:id="@+id/edit"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:text="Edit" />
       </LinearLayout>
         <LinearLayout
          android:layout_
          android:layout_>
    
            <Button
            android:id="@+id/back"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:text="Back" />
          </LinearLayout>
         </LinearLayout>
        </android.support.constraint.ConstraintLayout>
    

    apply_leave.xml

    <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_
      android:layout_
         android:background="@drawable/background"
          tools:context=".ApplyLeave">
    
        <LinearLayout
        android:layout_
        android:layout_
        android:id="@+id/parent_linear_layout"
          android:layout_margin="15dp"
         android:orientation="vertical">
    
    <LinearLayout
        android:layout_
        android:layout_>
    
        <CheckBox
            android:layout_
            android:layout_
            android:id="@+id/GoingAbroad"
            android:hint="Going Abroad"
            android:inputType="text"
            />
    </LinearLayout>
    <TextView
        android:layout_
        android:layout_
        android:text="Need Advance Payment?"/>
    <LinearLayout
        android:layout_
        android:layout_>
    
        <Spinner
            android:id="@+id/types"
            android:layout_
            android:layout_
            android:entries="@array/types"
            />
    </LinearLayout>
    <TextView
        android:layout_
        android:layout_
        android:text="Leave Type"/>
    <LinearLayout
        android:layout_
        android:layout_>
        <Spinner
            android:id="@+id/Leaves"
            android:layout_
            android:layout_
            android:entries="@array/leaves"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_
        android:layout_>
    
        <EditText
            android:layout_
            android:layout_
            android:id="@+id/noOfDays"
            android:hint="No. of Days"
            android:inputType="number"/>
    
    </LinearLayout>
    <LinearLayout
        android:layout_
        android:layout_>
    
        <Button
            android:layout_
            android:layout_
            android:id="@+id/addField"
            android:text="+"/>
        <Button
            android:layout_
            android:layout_
            android:id="@+id/removeField"
            android:text="-" />
    </LinearLayout>
    <LinearLayout
        android:layout_
        android:layout_>
        <TextView
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:text="Leave starts on" />
    
        <EditText
            android:id="@+id/startDate"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:clickable="true"
            android:editable="false"
            android:hint="Select" />
    </LinearLayout>
    <LinearLayout
        android:layout_
        android:layout_>
        <TextView
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:text="Rejoining will be on" />
    
        <EditText
            android:id="@+id/endDate"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:clickable="true"
            android:editable="false"
            android:hint="Select" />
    
    </LinearLayout>
    <EditText
        android:id="@+id/reason"
        android:layout_
        android:layout_
        android:hint="Remarks" />
    <TextView
        android:id="@+id/days"
        android:layout_
        android:layout_
        android:text="Total leave days = 0"/>
    <TextView
        android:id="@+id/date"
        android:layout_
        android:layout_
        android:text="Leave ends on = "/>
    <LinearLayout
        android:layout_
        android:layout_
        android:layout_margin="15dp"
        android:gravity="bottom">
    
        <Button
            android:id="@+id/cancelButton"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:text="Cancel" />
    
        <Button
            android:id="@+id/apply"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:text="Apply" />
           </LinearLayout>
           </LinearLayout>
           </LinearLayout>
    

    请告诉我哪里出错了。

【问题讨论】:

l1 应该是 RecyclerView,而不是 ListView。尝试使用 RecyclerView 而不是 ListView 再试一次 RecyclerView 应该放在哪里? 能否分享您的 XML 问题? 发布activity_apply_leave.xml 当然我会发布 xml 布局 【参考方案1】:

您将得到 null l1

在您的activity_apply_leave.xml 中确认您的ListView id。应该是leaveApplicationSummary

编辑:

您的 ListView 在 listleave.xml 中,但您在 Activity 中使用 activity_apply_leave.xml

将您的 ListView 从 listleave.xml 移动到 activity_apply_leave.xml

【讨论】:

我为列表视图发布了我的 xml 布局,请检查 好的,我在 apply_leave.xml 中移动了列表视图,它正在工作 @HuzaifaYusuf 现在这是一个新问题。

以上是关于Android:自定义列表视图无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

我的自定义搜索视图无法正常工作,当搜索输入为空时不显示原始列表

在android中使用自定义列表视图时无法设置左右对话

当按下 menuItem 以在 android 中显示用于列表视图的自定义适配器时,应用程序崩溃

片段中的自定义列表视图。未找到布局

使用自定义适配器自定义 Android ListView

Android - 将搜索栏添加到自定义列表视图和简单适配器