Я реализую recyclerView
, получая данные из базы данных, но получаю ошибку. Я новичок в разработке приложений для Android, поэтому, сталкиваясь с проблемами, пожалуйста, помогите исправить мою ошибку
Код выглядит следующим образом:
Cursor cursor1 =databse.rawQuery("select * from track_info where track_id1 = ? order by name desc",new String[]{pkg.getId()});
while (cursor1.moveToNext()){
location_data=findViewById(R.id.location_data);
activity_data=findViewById(R.id.activity_data);
time_data1=findViewById(R.id.time_data1);
time_data1.setText(cursor1.getString(1));
location_data.setText(cursor1.getString(2));
Ошибка наведения на строке time_data1.setText(cursor1.getString(1));
Ошибка:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.randomfile.Track$1.onClick(Track.java:72)
Код RecycleViewadapter:
package com.example.fedexfinal;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class RecycleviewAdapter extends RecyclerView.Adapter<RecycleviewAdapter.RecyclerViewHolder>{
private Context mContext;
public RecycleviewAdapter(Context context){
mContext= context;
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder{
public RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
}
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view=inflater.inflate(R.layout.recycle_item,parent,false);
return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}
xmlcode, в который помещается recycleviewer
<?xml version="1.0" encoding="utf-8"?>
<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=".Track">
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<EditText
android:id="@+id/title_track"
android:layout_width="363dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="18dp"
android:layout_marginLeft="18dp"
android:layout_marginTop="38dp"
android:background="#ffffff"
android:backgroundTint="#ffffff"
android:hint="Enter your Tracking Number"
android:inputType="text"></EditText>
<Button
android:id="@+id/trackbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="99dp"
android:layout_marginEnd="161dp"
android:layout_marginRight="161dp"
android:text="Track"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="168dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Shipment Details"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textdata2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="207dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Dimension"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textdata3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="243dp"
android:ems="10"
android:inputType="text"
android:text="Weight"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="408dp"
android:layout_height="453dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="0dp" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
xmlcode для получения макета повторного просмотра (recycle_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<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">
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:text="Location" />
<TextView
android:id="@+id/location_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="71dp"
android:layout_marginLeft="71dp"
android:layout_marginTop="11dp"
android:text="TextView" />
<TextView
android:id="@+id/activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:text="Activity" />
<TextView
android:id="@+id/activity_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="71dp"
android:layout_marginLeft="71dp"
android:layout_marginTop="30dp"
android:text="TextView" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:text="Date/Time" />
<TextView
android:id="@+id/time_data1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="71dp"
android:layout_marginLeft="71dp"
android:layout_marginTop="50dp"
/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Может ли кто-нибудь помочь мне узнать об ошибке и исправить мою ошибку