Trying to populate a list view in android using a backendless table

The main request is for advice on the backendless code required to populate an activity in android studio using a table from backendless.

In this example there is a separate activity in which staff will post messages and these will automatically populate a table called ‘Message’.
There is a separate activity which when clicked on I would like to then collect the ‘Message’ table and then it is displayed in a list view.

It seems to work fine when hard coded so suspect I am doing something wrong when trying to access the table from backendless.

I think I am missing a where clause, the only tutorial example I can find uses a where clause that specifically makes it so the user can only see their own ‘Message’ they will have posted where as I want it so that all users can see all items in the ‘Message’ table (and currently it is in “date” (a column of the Message table) order).

Is there a generic where clause that shows all the table to all users?

Issues like having a custom message class, having an adapter to set the class values and its layout have been done i believe as other activities use these without issues.

I will copy the code in the activity in question (called MessageBoard), when from the MainActivity the MessageBoard is accessed (via a button) an error “app name, has stopped working” comes up when testing it.

code :
public class MessageBoard extends AppCompatActivity {

private android.view.View mProgressView;
private View mLoginFormView;
private TextView tvLoad;

ListView lvList;
MessageAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    lvList = findViewById(R.id.lvList);

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

   DataQueryBuilder queryBuilder = DataQueryBuilder.create();
   queryBuilder.setGroupBy("date");

    showProgress(true);
    tvLoad.setText("loading announcements please wait");


    Backendless.Persistence.of(Message.class).find(queryBuilder, new AsyncCallback<List<Message>>()
    {
        @Override
        public void handleResponse(List<Message> response) {
            ApplicationClass.messages = response;
            adapter = new MessageAdapter(MessageBoard.this, ApplicationClass.messages);
            lvList.setAdapter(adapter);
            showProgress(false);


        }

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

Hello @Leslie_Harding

First of all, you should figure out in what point your application crashes. To do that add logs to your code, for example:

private android.view.View mProgressView;
private View mLoginFormView;
private TextView tvLoad;

ListView lvList;
MessageAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    lvList = findViewById(R.id.lvList);

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

   DataQueryBuilder queryBuilder = DataQueryBuilder.create();
   queryBuilder.setGroupBy("date");
Log.i(TAG, "query builder created" );

    showProgress(true);
    tvLoad.setText("loading announcements please wait");


    Backendless.Persistence.of(Message.class).find(queryBuilder, new AsyncCallback<List<Message>>()
    {
        @Override
        public void handleResponse(List<Message> response) {
            Log.i(TAG, "received response: " + response );            

            ApplicationClass.messages = response;
            adapter = new MessageAdapter(MessageBoard.this, ApplicationClass.messages);
            lvList.setAdapter(adapter);
            showProgress(false);


        }

        @Override
        public void handleFault(BackendlessFault fault) {
           Log.e(TAG, "Received error: " + fault ); 
           Toast.makeText(MessageBoard.this, "error" + fault.getMessage(), Toast.LENGTH_SHORT).show();
            showProgress(false);
        }
 });

@sergey.kuk thank you for your reply I will let you know how it goes,
with thanks
Les

Hi Sergey

I added this line at the top of the activity : private static final String TAG = “MessageBoard”;
When i run the app im not sure it is recognizing the logs above because when i search for them they are not there the only red things in the logcat are;

2020-10-15 19:22:17.096 5364-5417/com.example.contactsapp E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2020-10-15 19:22:17.096 5364-5417/com.example.contactsapp E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2020-10-15 19:22:17.097 5364-5417/com.example.contactsapp E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2020-10-15 19:22:17.097 5364-5417/com.example.contactsapp E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824

Which from a google search the seems to be more related to the emulator than anything in the code.
It seems to suggest I need to be changing the GPU from hardware to software but in the AVD settings it is set to automatic and is not changeable

Any thoughts about the above?
with thanks
Les

Hello @Leslie_Harding

You should try to do what google advises. You need to ensure that you can see the logs. Only then will it be possible to understand what the problem is.

Regards,
Inna

hi thanks for your reply I will work on the logs

Hi there I think I have found the error in the logcat it is as below:
A quick google search seems to indicate that some aspect of my code hasn’t been initialized but I cant find which it is after trying a few variations, any thoughts based on the error message below?

2020-10-26 13:04:27.929 12701-12701/com.harding.contactsapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.harding.contactsapp, PID: 12701
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.harding.contactsapp/com.harding.BalmoralDoctorsTest.MessageBoard}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.view.View.setVisibility(int)’ on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.view.View.setVisibility(int)’ on a null object reference
at com.harding.BalmoralDoctorsTest.MessageBoard.showProgress(MessageBoard.java:88)
at com.harding.BalmoralDoctorsTest.MessageBoard.onCreate(MessageBoard.java:49)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Hi @Leslie_Harding

The main error is

java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.view.View.setVisibility(int)’ on a null object reference.

I would suggest you to inspect the code and find the place where you call setVisibility method.
My assumption - maybe you try to change the visibility of view that hasn’t been initialized.

Best Regards,
Maksym

Hi Maksym
Thank you for your answer, the setVisibility method was used as part of a progress bar used when moving between two activities (MainActivity and MessageBoard), it was non essential and thus removed and the error is gone.
But now there is a new error that wasnt there before:

java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.ListView.setAdapter(android.widget.ListAdapter)’ on a null object reference

which presumably has a similar logic (in this case that the setAdapter method is not initialized).
The setAdapter appears in the handleResponse aspect of the code below
Can you see in the below what needs to be done to initialize the setAdapter method?

public class MessageBoard extends AppCompatActivity {

ListView lvList;
MessageAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    lvList = findViewById(R.id.lvList);

   DataQueryBuilder queryBuilder = DataQueryBuilder.create();
   queryBuilder.setGroupBy("date");

    Backendless.Persistence.of(Message.class).find(queryBuilder, new AsyncCallback<List<Message>>()
    {
        @Override
        public void handleResponse(List<Message> response) {
            ApplicationClass.messages = response;
            adapter = new MessageAdapter(MessageBoard.this, ApplicationClass.messages);
            lvList.setAdapter(adapter);

        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Toast.makeText(MessageBoard.this, "error" + fault.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });


}

The error is self explanatory.

Attempt to invoke virtual method ‘setAdapter’ on a null object reference

You try to invoke setAdapter on a null object (which is lvList).
The onCreate method is not the best place to operate views. It’s fundamentals of Android development. I suggest you to take any Android Development course (there a lot of free information on the internet).and practice a little bit. :slight_smile:

Best Regards,
Maksym