Android Question Why can't I set table column width? [SOLVED]

Tim Chapman

Active Member
Licensed User
Longtime User
If I uncomment line 19 (Line 45 in Main in my actual code), I get a error:
java.lang.RuntimeException: Object should first be initialized (View).

I have spent hours trying to figure out what the problem is.
It works in the code in the Table.zip file: https://www.b4x.com/android/forum/t...supports-tables-of-any-size.19254/#post110901

Project is attached.
Please help!
And Thank You In Advance!

B4A Snippet:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Type Todo(ID As Short, Name As String, Status As Byte, Context As Byte, Category As Byte, Priority As Int, Note As String)
    Dim table1 As Table
    Dim Statuses(12,2) As String
    Dim Categories(40,2) As String
    Dim TodoList As List
    Dim HeaderRow(7) As String
    Dim Categories(40,2) As String
    Dim n As Notification = CreateNotification("Tap to Proceed.")
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If table1.IsInitialized Then
        Activity.RemoveAllViews 'remove the current table
    End If
    table1.Initialize(Me, "Table1", 4) 'The last value here sets the number of columns in the table that shows on the screen.
    table1.AddToActivity(Activity, 0, 0, 100%x, 100%y)
    'table1.SetColumnsWidths(Array As Int(100dip, 30dip, 130dip, 100%x - 260dip))
   
    myMap.Initialize
    'Activity.AddMenuItem("Load Table", "LoadTable")
    'Activity.AddMenuItem("Save Table", "SaveTable")
    Activity.AddMenuItem("Allow Context Notification", "AllowContextNotification")
    Activity.AddMenuItem("Clear Context Notification", "ClearContextNotification")
    Activity.AddMenuItem("Exit Program", "ExitProgram")
   
    LoadArrays(File.DirAssets, "Todo List.xls")
    Notifier
End Sub
 

Attachments

  • TimTodo Works.zip
    189.5 KB · Views: 37
Last edited:
Solution
In the Table class, you must fill the table before calling Table1.SetColumnsWidths.
This is the case in the code of Table.zip.
You should move this line at the end of the LoadTable routine after having filled the Table.
B4X:
        For j = 1 To TempContextLength

            If Context = TempContext(j) Then
                'Log("...")
                'Log("Context = " & Context)
                'Log("TempContext("&j&" = "& TempContext(j))
                TableRow = TableRow + 1
                table1.AddRow(Values)
            End If
        Next
    Next
    table1.SetColumnsWidths(Array As Int(100dip, 30dip, 130dip, 100%x - 260dip))

As I already told you in another thread, the Table class you use is a very old one.
It does scroll...

DonManfred

Expert
Licensed User
Longtime User

It is a mistake to add a customview by CODE.
Add the tableview with a LAYOUT!
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
Which part is a customview? I don't understand my code to have any customviews. The table example was the same unless I am mistaken.
I am not very good at this programming so I am sure I am missing something. If you can see why the example table works and mine does not, please tell me. I have tried everything I can think of to fix this. I have never used the designer and don't know how to do a layout. My background is quickbasic and BASCOM-AVR. Programming is only a hobby for me. The learning curve on this has been steep.
 
Last edited:
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
I checked out B4Xtable and it requires a database which makes way more learning curve for me. So I avoided it.
I want to make my code work. I found that there is a scrollview in the table module which is set up with no layout created in the designer. So, why won't my code work like it does in the table example I shared the link to? It is obvious to me that something I added in my code is making it not work but I can't narrow down which part of my code is the problem.
I have never used the designer and don't know how to do a layout. My background is quickbasic and BASCOM-AVR. Programming is only a hobby for me.
 
Last edited:
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
I have never used the designer and don't know how to do a layout.

I strongly believe you should take a pause and read some basic tutorials..

Using the Designer and creating a basic Layout containing your table and then loading that layout is very, very simple.

Also... If you wish to continue coding in this environment, it is an absolute must that you at least learn this basic step.

Some reading is definitely suggested.
 
Upvote 0

zed

Active Member
Licensed User
B4XTable works very well without a database.
You can load a CSV file and even other file "setdata" request a list. You can easily create a list with your data

example:
B4X:
Dim parser As CSVParser
parser.Initialize
Dim data As List = parser.Parse(File.ReadString(File.DirAssets, "us_counties.csv"), ",", True)
B4XTable1.SetData(data)

Programming is only a hobby for me.
Whether for pleasure or professionally, you have to use the right tools.
Read the documentation, try the examples. It's simpler than you think to implement.

If you still have problems afterward, we can help you better.
Android is constantly evolving. It is better to use the most recent views
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
I searched and found these two examples I built my code from. One was an Excel Table example (https://www.b4x.com/android/forum/threads/read-write-excel-files-on-android.25632/) and the other was a background GPS example (https://www.b4x.com/android/forum/threads/background-location-tracking.99873/#content). The examples I find on the forum should work. After spending a week on my project I come to find that I can't set a simple column width without an error and am advised to start the project over using another setup (B4XTable) having not know that I was going down the wrong path. I don't have time to waste starting again. So, if I can't find out how to get my code to work, I will move on to other work that I have to get done and this time will be wasted. It is completely baffling to me that a command (SetColumnsWidths) which I am using correctly in the code does not work and sends me on a wild goose chase. This is beyond frustrating to me.

I have been working on this literally 16 hours per day since Sunday. Every time I resume programming with this programming language it has changed and I have more learning curve. Last time I tried using this programming language was March 2023. I have been reading and watching tutorials, but I don't have a laid out path to follow to learn what I need. I am approaching it topically and and not in a linear fashion because I have not found a linear fashion to do it in. It is very frustrating and consumes a ton of time.

I have a project for my business that I need to program next and was trying to use this project as a refresher on B4A but it is maddening!
 
Last edited:
Upvote 0

zed

Active Member
Licensed User
I understand your frustration.
But you use examples that no longer work with Google's new recommendations.
I tried to open your app so i could help you. Unfortunately, I can't do it.
Here is the log.
Log:
--------- beginning of main
Copying updated assets files (1)
java.lang.RuntimeException: Unable to create service b4a.example.starter: java.lang.RuntimeException: java.net.SocketException: Connection reset
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:4643)
    at android.app.ActivityThread.access$1800(ActivityThread.java:273)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8302)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
Caused by: java.lang.RuntimeException: java.net.SocketException: Connection reset
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:188)
    at b4a.example.starter.onCreate(starter.java:48)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:4630)
    ... 9 more
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:191)
    at java.net.SocketInputStream.read(SocketInputStream.java:143)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:239)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:258)
    at java.io.DataInputStream.readByte(DataInputStream.java:273)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:344)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    ... 11 more
You are still using a Sarter service.
This service is replaced by Receivers and Services.
You need to update if you want your apps to work properly.
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
Yes, of course I read your reply. You did not tell me that my code would not work. Only that there was a new approach available that requires SQL.

This is the first if have heard of receivers and services. In the B4A IDE when you start a new default project, the starter module is automatically added.
This again is absolutely incomprehensible to me if it won't work properly. My code runs perfectly with the starter service. So I never thought to look beyond that. I watched the video Android Process and activities life cycle (https://www.b4x.com/android/forum/threads/android-process-and-activities-life-cycle.6487/) which showed the starter service. Now I am told that infomation in the video is not up to date but I would have no way of know that when I watched it. I hope you see that this situation is running me around in circles.

I have no idea why you are getting an error. It runs perfectly on my computer as long as I don't try to set the column width. I have the latest install of all the pieces specified in the installation instructions page for B4A.

My approach to coding is to find the simplest examples I can that cover the topics I need and build on them. I dug around. I saw ones that require database stuff to do tables and avoided them like the plague. Adding the complexity of a database in order to have the simple functionality of a table makes not sense to me and I know nothing about databases or SQL. I had no idea my code would not work. I was making great progress until I tried to set the column width on the table and now find that I can't do so using commands that I found in 2 examples on the forum. My time has been wasted and I have to spend a ton more time learning more and then redoing my code. I hope you can see that this is untenable. How is a noob supposed to know which examples on the forum are useless and won't work? Unfortunately that information was not available to me when I started. I am so angry with this situation! At the rate I have been going, I would be done with this project in a couple days and felt great about my progress considering my very limited programming background. Now the length of time required just went way up.

I would be happy to use the designer to make my table work as Klaus suggested. Making a layout is a piece of cake but implementing it is another story. I don't what the pieces are that are involved, how to use them or the order in which to use them. I don't have a frame of reference about adding views to code and then removing them or reusing them. I don't even know what the components are called. More learning curve. I have avoided the learning curve thus far and gotten work done, but now I am back to the drawing board.

So, to me, the shortest distance between to two points is a straight line. The requirement seems to be an older version of android in order for my code to work properly. What is the latest version of android that will work properly with my code? I can more easily get a tablet with that version than to redo my code. This is absolutely ridiculous when all I need is for the Set Column Width command to work!
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
I just realized one thing. To me the cardinal rule of programming language development is to make sure that you maintain backwards compatibility as the language progresses. B4A has not done this. I never thought to look for this problem because the programming languages I use have done this. QB64 replaced QuickBASIC and has excellent backwards compatibility with the original language. I would much rather see effort put in to making sure the older parts of B4A still work than adding new more complicated features to do the same job. If I were not being forced to use the newer more complicated parts of B4A I would be perfectly happy using the parts I have selected because they get the job done nicely. The new B4Xtable won't even scroll. To me that is unacceptable on a touch screen. The latest and greatest is not necessarily the best especially when it adds more complexity.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I just realized one thing. To me the cardinal rule of programming language development is to make sure that you maintain backwards compatibility as the language progresses. B4A has not done this.
The B4X suite of IDEs tries to keep up with the targeted OSes latest.
No one is forced to use the latest IDE, and anyone can have concurrent IDEs installed, like B4A v6 and B4A v10 running on the same machine.
Since a few years, google AND Apple have established minimum Targeted OS versions to their developers base.
What you are asking for is, in a "out of this world" analogy, to keep the space station docking gates compatible with appolo11 capsules, while also being able to dock SpaceX ships...


I would much rather see effort put in to making sure the older parts of B4A still work than adding new more complicated features to do the same job.
Again... this is mostly due to the evolution of the targeted OSes and the way they work.

If I were not being forced to use the newer more complicated parts of B4A I would be perfectly happy using the parts I have selected because they get the job done nicely.
Again, you can re--install a (very) older version of the IDE and use it...
now... go ask a NASA engineer about a code line in the Apolo 13 on-board computer... things (must) evolve

The new B4Xtable won't even scroll. To me that is unacceptable on a touch screen.
That is an issue I never read about...
Have you tried to ask in the forum why it doesn't?

The latest and greatest is not necessarily the best especially when it adds more complexity.
Finally, something I can agree with you!
But then again... where are all the Model T?
It was a simple, good and reliable car...why did they stop making it?
 
Last edited:
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
@Tim Chapman
Updating to latest B4A version will always (at least up to now) maintain compatibility with older project's versions.
As already told B4A need to evolve to follow Google Madness Changes and also to provide new functions.
One thing that you should consider is:
you want to publish your app on the Google Play Market or just keep or use your app privately?
The first case it's the most painful: Lot of things to care about (mandatory!) and that can change year by year (usually worst).
If instead you want to do something just for you/family/friends then you can avoid many of these problems because you can declare a lower Target SDK in the Manifest.
I am sure that with the help of the members and you taking your time to learn always more will solve this thing and many others.
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
There is no point in asking why B4XTables won't scroll. The fact is that it won't.
I have no problem with keeping up with the latest OS releases as long as the previous code will still work in the latest edition of the software.
I realize that some things like DoEvents has to go and be replaced by Sleep(0) probably due to new requirements of the latest Android OS.
But I don't see any reason why the code for for an older implementation of a table should not still work properly. There is obviously a glitch somewhere because the Set Column Width command works in the example code on the latest IDE but won't work on my code.
That is all I care about since I am simply trying to get this job done.

I will never make code to release on google play. My brother is a professional developer and told me never to do so unless I want to maintain the code. It is all for my own use.
I would like to know what target SDK version to put in manifest for my code to work.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I would like to know what target SDK version to put in manifest for my code to work.
For personal use I would use
B4X:
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="26"/>
Note that every New Project will start with targetSdkVersion="33" by default, because B4A is set to follow Google Requests.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
To me the cardinal rule of programming language development is to make sure that you maintain backwards compatibility as the language progresses. B4A has not done this.
Sorry, I disagree when you refer to B4A not being backward compatible.
Erel pays always great attention to let the language backward compatible.

One thing that probably you miss is that the language itself is named B4X while B4A, B4i and B4J are the tools that use that language to produce software for each specific platform.
This is to say that it's not the language to blame.

As @Cableguy already explained, each platform has its quirks and "improves" following its own direction. What we do, upgrading our tools and listening to Erel's advice (and tutorials) is to learn how to cope with what's new with those OS news.
I understand it can be frustrating at times. Expecially when you have something that worked "perfectly" until the day before. But the OSes owners set the rules and there's nothing we can do but follow them (as long as we intend to stay on their stores or use the latest technologies and trends).

My post is intended just to put things in the right prospective: language vs. OSes
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
I did try changing the target SDK to 26 and it made no difference.

I would like to shift the focus back to the problem at hand. I really need my code to work without have to redo it.
Since the method I am using for a table has been around a long time without using the designer, I don't understand why the designer is now needed.
So, preferably, I would like to know how to make it work without the designer.
Failing that, I will be happy to use a layout from the designer if it is needed.
Has anyone been able to open my code and see the problem? Is the fix I want available?

To be specific, I am getting an error saying a view is not initialized when it is. This is in the Table module of my code which I did not write so I don't fully understand it. Can we tackle that problem and see what the answer is?
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I am trying to test your code, but I miss many Classes and Libs that you used and I am struggling a bit to try to catch them all.
Then I can't guarantee that I will be able to help you. :)
 
Upvote 0
Top