Resources$NotFoundException while retrieving data from SQLite and displaying on Custom list view

-2

I'm trying to retrieve data stored in the database and show it onto custom ListView. Whenever I call FenceActivity.class to view data app gets crashed.

FenceActivity

public class FenceActivity extends AppCompatActivity {
    List<Fence> fenceList;
    SQLiteDatabase sqLiteDatabase;
    ListView listViewFences;
    FenceAdapter fenceAdapter;
    DataBaseHelper dataBaseHelper;

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

        listViewFences = findViewById(R.id.fencesListView);
        fenceList = new ArrayList<>();

        showFencesFromDatabase();
    }

    public void showFencesFromDatabase(){

        dataBaseHelper = new DataBaseHelper(this);
        Cursor cursor = dataBaseHelper.getAllData();

        if(cursor.moveToFirst()){
            do{
                fenceList.add(new Fence(cursor.getInt(0),cursor.getDouble(1),cursor.getDouble(2),cursor.getInt(3)));
            }while (cursor.moveToNext());
        }
        cursor.close();

        fenceAdapter = new FenceAdapter(FenceActivity.this,R.layout.list_layout_fences,fenceList);

        listViewFences.setAdapter(fenceAdapter);
    }

}

I use Fence Activity to retrieve Data using DatabaseHelper class from the database.

Fence Adapter

public class FenceAdapter extends ArrayAdapter<Fence> {

    Context context;
    int listLayoutRes;
    List<Fence> fenceList;

    public FenceAdapter(Context context, int listLayoutRes, List<Fence> fenceList){
        super(context,listLayoutRes,fenceList);
        this.context = context;
        this.listLayoutRes=listLayoutRes;
        this.fenceList = fenceList;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(listLayoutRes,null);

        Fence fence = fenceList.get(position);
        TextView textViewSno = view.findViewById(R.id.textViewSnoLabel);
        TextView textViewLat = view.findViewById(R.id.textViewLatitudeValue);
        TextView textViewLon = view.findViewById(R.id.textViewLongitudeValue);
        TextView textViewRadi = view.findViewById(R.id.textViewRadiusValue);

        textViewSno.setText(fence.getSno());
        textViewLat.setText(String.valueOf(fence.getLat()));
        textViewLon.setText(String.valueOf(fence.getLon()));
        textViewRadi.setText(fence.getRadius());

        Button buttonDel = view.findViewById(R.id.buttonDeleteFence);
        return view;
    }
}

This my adapter class to inflate listview.

Fence

public class Fence {

    int radius,sno;
    double lat,lon;

    public Fence( int sno,double lat, double lon,int radius) {
        this.radius = radius;
        this.sno = sno;
        this.lat = lat;
        this.lon = lon;
    }

    public int getRadius() {
        return radius;
    }

    public int getSno() {
        return sno;
    }

    public double getLat() {
        return lat;
    }

    public double getLon() {
        return lon;
    }
}

Function I'm Accessing From DatabaseHelper class

public Cursor getAllData() {
        String query = "SELECT * FROM " + TABLE_NAME;
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery(query, null);
        return cursor;
    }

This my simple Fence class with getters and a constructor.

Stack Trace

2019-06-29 05:18:19.364 7816-7816/com.abhishakkrmalviya.fencetest E/SchedPolicy: set_timerslack_ns write failed: Operation not permitted
2019-06-29 05:18:19.448 7816-7816/com.abhishakkrmalviya.fencetest E/lviya.fencetes: Invalid ID 0x00000001.
2019-06-29 05:18:19.450 7816-7816/com.abhishakkrmalviya.fencetest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.abhishakkrmalviya.fencetest, PID: 7816
    android.content.res.Resources$NotFoundException: String resource ID #0x1
        at android.content.res.Resources.getText(Resources.java:360)
        at android.content.res.MiuiResources.getText(MiuiResources.java:97)
        at android.widget.TextView.setText(TextView.java:5837)
        at com.abhishakkrmalviya.fencetest.FenceAdapter.getView(FenceAdapter.java:39)
        at android.widget.AbsListView.obtainView(AbsListView.java:2399)
        at android.widget.ListView.makeAndAddView(ListView.java:2126)
        at android.widget.ListView.fillDown(ListView.java:851)
        at android.widget.ListView.fillDown(ListView.java:833)
        at android.widget.ListView.fillFromTop(ListView.java:921)
        at android.widget.ListView.layoutChildren(ListView.java:1900)
        at android.widget.AbsListView.onLayout(AbsListView.java:2198)
        at android.view.View.layout(View.java:20726)
        at android.view.ViewGroup.layout(ViewGroup.java:6198)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
        at android.view.View.layout(View.java:20726)
        at android.view.ViewGroup.layout(ViewGroup.java:6198)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20726)
        at android.view.ViewGroup.layout(ViewGroup.java:6198)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:20726)
        at android.view.ViewGroup.layout(ViewGroup.java:6198)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20726)
        at android.view.ViewGroup.layout(ViewGroup.java:6198)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:20726)
        at android.view.ViewGroup.layout(ViewGroup.java:6198)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:774)
        at android.view.View.layout(View.java:20726)
        at android.view.ViewGroup.layout(ViewGroup.java:6198)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2856)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2383)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1523)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7395)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1065)
        at android.view.Choreographer.doCallbacks(Choreographer.java:877)
        at android.view.Choreographer.doFrame(Choreographer.java:808)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1051)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6806)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

What changes can I make to prevent my app from getting crashed?

java
android
listview
android-sqlite
asked on Stack Overflow Jun 28, 2019 by Abhishak Kumar Malviya • edited Jun 29, 2019 by Gustavo

1 Answer

1

It looks like it can't find a resource by the ID that you passed it. This must be the layout id of your list item view. Rather than passing that id in the constructor of your fenceAdaptor, you should just use code more like this in the getView method of the adaptor:

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.list_layout_fences, null);
    }

I see you are also inflating your own local view, but you should be inflating the convertView that was passed into the getView method.

answered on Stack Overflow Jun 28, 2019 by Michael Dougan

User contributions licensed under CC BY-SA 3.0