Android Question CustomListView Exception on "Return sv" for "AsView"

Discussion in 'Android Questions' started by Guardian17, Apr 25, 2015.

  1. Guardian17

    Guardian17 Active Member Licensed User

    I am having a problem with CustomListView. (See attached Zipped Project).

    After initializing the CustomListView "clv1", the first guess can be entered by typing 4 letters and pressing "ENTER".

    Then in the code, when I try Inserting-At, I get a Null Pointer Exception when the "clv1.AsView" tries to "Return sv".

    Does something special need to be done to "sv" before attempting the "InsertAt" ?
     

    Attached Files:

  2. fixit30

    fixit30 Active Member Licensed User

    2 Problems I can see.

    You are trying to access the CustomListView before it has been Initialized.

    Move the following code into the Activity_Create Section

    Code:
    If clv1.IsInitialized = False Then
        clv1.Initialize(Me, 
    "clv1")
    End If
    Also, you are not adding clv1 to the activity. You need to Un-comment out the line in the below code.

    Code:
    Sub    GenerateGuessedWordsScrollWindow
        
    If clv1.IsInitialized = False Then
            clv1.Initialize(Me, 
    "clv1")
        
    End If
        
    If LandscapeMode = False Then 'Portrait
            clv1Top = 7.2%y
            clv1Height = 
    32%y
        
    Else
            clv1Top = 
    9.5%y
            clv1Height = 
    89%y
        
    End If
        
    'pnlGuesses.AddView(clv1.AsView, 0, clv1Top, pnlGuesses.Width, clv1Height) 'new
    End Sub
     
    Last edited: Apr 25, 2015
    mangojack likes this.
  3. fixit30

    fixit30 Active Member Licensed User

    BTW. I also had to uninstall the app after making the above changes for it to work correctly.
     
  4. Guardian17

    Guardian17 Active Member Licensed User

    Thank you, Fixit30. My original App from which this was taken has gotten quite large, (I had to whittle it down to be able to Export it as a Zip file), and I have tended to loose track of what needs to be done, and when, in the larger version.

    I did try what you said, and that did allow the words to be transferred to the scroll area below the guess-input area. Now I just have to figure out how to keep the words that have been transferred to the scroll area present when I change device orientations. I do have a mechanism in place to save the state of the game (SaveGameState) when it Pauses, and that state is read back on Resume (RestoreGame), however, after an orientation change has occurred, the guessed words are lost, and any new words guessed thereafter do not get placed into the scroll area.
     
  5. fixit30

    fixit30 Active Member Licensed User

    CustomListView is an activity variable which means that when the screen is rotated the variable is destroyed.

    You need to look at alternatives for storing the CustomListView items so that it can be restored after screen rotation. Maybe store the them in a list declared in Process_Globals and restore on rotation?
     
    mangojack likes this.
  6. Guardian17

    Guardian17 Active Member Licensed User

    In my larger App, which I can't Export As Zip due to its size, most of the functionality of the CustomListView works. However, when I change orientations prior to the first guess being entered, and then try to enter the first guess, I get this Null Pointer Exception when "Return sv" is executed for the CustomListView "AsView" Sub:

    "LastException java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference"

    If I already have one or more guesses present in the CustomListView, the Exception does not occur.

    I know that the CustomListView being referenced has been initialized, as I have some debug lines of code that check the clv1.IsInitialized condition right before the place where the NullPointerException occurs, but I can't tell from the variable pane whether it has been added to the Activity or Panel where it is normally placed. Is there a way programmatically to tell whether a CustomListView has been Added to an Activity/Panel, similar to the way "clv1.IsInitialized" is used?
     
  7. fixit30

    fixit30 Active Member Licensed User

  8. fixit30

    fixit30 Active Member Licensed User

    Have you also tried moving this line to Activity_Create?

    Code:
    pnlGuesses.AddView(clv1.AsView, 0, clv1Top, pnlGuesses.Width, clv1Height) 'new
     
  9. Guardian17

    Guardian17 Active Member Licensed User

    Yes. I have added that line to my Activity_Create (indirectly through ShowPlayScreen which is where the Layout gets loaded that contains the clv). That gets rid of the Exception error, however, now, none of the entered guesses appear in the clv scroll area. I'll try checking into that this weekend.
     
  10. Guardian17

    Guardian17 Active Member Licensed User

    As silly as it may sound, it took several days to figure out why the guesses did not appear in the scroll area. My error was that when I Initialized and Added the CLV in Activity_Create, I had copied the line of code for the AddView from another section of my code, but the Height and Width parameters specified in the AddView were variables based on the parent view which had a Width but not a Height (the Height was zero during Activity_Create). So the CLV essentially did not exist because it had no Height.:oops::confused:
    Thanks, Fixit30, for pointing me in a right direction.:D
     
    Last edited: May 7, 2015
    mangojack and fixit30 like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice