【问题标题】:SQLite android database error for textviewtextview的SQLite android数据库错误
【发布时间】:2019-10-17 04:24:57
【问题描述】:

我正在通过从数据库中获取详细信息来实现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)

RecycleView 适配器代码:

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;
    }

}

recycleviewer所在的xmlcode

<?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>

获取recycleview Layout的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>

谁能帮我了解错误并纠正我的错误

【问题讨论】:

标签: java android android-studio android-sqlite


【解决方案1】:

如果你的这段代码在数据库助手类中 -

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));

不仅仅是这样做 -

public List<String> getData(){
Cursor cursor1 =databse.rawQuery("select * from track_info where track_id1 = ? order by name desc",new String[]{pkg.getId()});

List<String> data = new ArrayList();
 while (cursor1.moveToNext()){
data.add(cursor1.getString(1))
}

return data
}

然后在您的活动或片段中使用此数据在 textview 上进行设置 -

  time_data1=findViewById(R.id.time_data1);
  time_data1.setText(cursor1.getString(1));

如果您在 recyclerview 中使用此数据而不是将列表传递给 recyclerview 适配器。

【讨论】:

    【解决方案2】:

    按照这个来解决:

    型号:

        public class TrackInfo implements Serializable {
            private String locationData;
            private String activityData;
            private String timeData;
    
            public TrackInfo(String location, String activity, String time) {
                this.locationData = location;
                this.activityData = activity;
                this.timeData = time;
            }
    
            public String getLocationData() {
                return locationData;
            }
    
            public String getActivityData() {
                return activityData;
            }
    
            public String getTimeData() {
                return timeData;
            }
        }
    

    活动

    public class TrackActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            ...
    
            List<TrackInfo> trackInfoList = getData();
    
            RecyclerView recyclerView = findViewById(R.id.recycleview);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(new RecycleviewAdapter(trackInfoList));
        }
    
        public List<TrackInfo> getData() {
            List<TrackInfo> trackInfoList = new ArrayList<>();
            Cursor cursor1 = databse.rawQuery("select * from track_info where track_id1 = ? order by name desc",new String[]{pkg.getId()});
    
            while (cursor1.moveToNext()) {
    
                String time = cursor1.getString(1);
                String location = cursor1.getString(2);
                String activity = cursor1.getString(3);
    
                trackInfoList.add(new TrackInfo(location, activity, time));
            }
    
            return trackInfoList;
        }
    }
    

    适配器:

        public class RecycleviewAdapter extends RecyclerView.Adapter<RecycleviewAdapter.RecyclerViewHolder>{
            private List<TrackInfo> trackInfoList;
    
            public RecycleviewAdapter(List<TrackInfo> tracks){
                trackInfoList= tracks;
    
            }
    
            class RecyclerViewHolder extends RecyclerView.ViewHolder{
    
                private TextView locationView, activityView, timeView;
    
                public RecyclerViewHolder(@NonNull View itemView) {
                    super(itemView);
    
                    locationView = itemView.findViewById(R.id.location_data);
                    activityView = itemView.findViewById(R.id.activity_data);
                    timeView = itemView.findViewById(R.id.time_data1);
                }
            }
    
            @NonNull
            @Override
            public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item,parent,false);
                return new RecyclerViewHolder(view);
            }
    
            @Override
            public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
                TrackInfo trackInfo = trackInfoList.get(position);
    
                holder.activityView.setText(trackInfo.getActivityData());
                holder.locationView.setText(trackInfo.getLocationData());
                holder.timeView.setText(trackInfo.getTimeData());
            }
    
            @Override
            public int getItemCount() {
                return trackInfoList.size();
            }
        }
    

    【讨论】:

    • 谢谢先生的回答,但是在之前的语句中已经初始化了time_data1=findViewById(R.id.time_data1);
    • 在初始化后添加一个日志,如 - Log.d(TAG, "is null "+(time_data1 == null) + " "+(location_data == null));确认它们不为空。
    • 感谢您的回复。是的,它显示 null 怎么办?@NareshNK
    • @BhavinThakar,请使用 xml 发布最少的代码以获得帮助。我们如何在不知道您在代码中做什么的情况下说出解决方案?
    • 你是在fragment的activity中调用上面的代码吗??发布更多代码,例如您在哪个部分使用此代码。
    【解决方案3】:

    更改此代码

    time_data1=findViewById(R.id.time_data1);
    

    time_data1=(TextView)findViewById(R.id.time_data1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多