0

I cannot set the position of my Horizontal ScrollView in my main.xml because I am unable to target it using the code below. This method has always worked for me when targeting TextViews or Buttons, so i'm not sure why a ScrollView should be treated differently.

ScrollView scrollview1 = (ScrollView)findViewById(R.id.scrollview1);

So far I've been unable to find a working example of someone doing something similar that I could implement into my code successfully to scroll my bar to where it needs to be.

Here is what my error log displays during the app crash

dalvikvm threadid=1: thread exiting with uncaught exception (group=0x409c01f8)

AndroidRuntime FATAL EXCEPTION: main

AndroidRuntime java.land.ClassCastException: android.widget.HorizontalScrollView cannot be cast to android.widget.ScrollView

AndroidRuntime at com.testButtons.TestButtonsActivity.onKeyDown(TestButtonsActivity.java)

Here is the code block that holds the troubled code.

I'm using the android return key to exit a menu and go back to main.xml with horizontal scrollview1 set 40dp from the left.

    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if(pageNum < 4){

                setContentView(R.layout.main);

                ScrollView scrollview1 = (ScrollView)findViewById(R.id.scrollview1);
                //scrollview1.scrollTo(40,0);

            }else if(pageNum >= 4 && pageNum < 20){

                setContentView(R.layout.main2);
                pageNum=0;
            }

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

I'm pretty new to android app development and this form as well; as This is my first post pertaining to my first app store worthy app.

I appreciate the time you spent reading my question, and I know others android noobies will probably run into this same/similar problem and find this useful.

CharCar
  • 3
  • 3

1 Answers1

1

You're casting it to a ScrollView when in fact it is a HorizontalScrollView. ScollView is not a parent class of HorizontalScrollView, thus your error is thrown. Change

ScrollView scrollview1 = (ScrollView)findViewById(R.id.scrollview1);

to

HorizontalScrollView scrollview1 = (HorizontalScrollView)findViewById(R.id.scrollview1);
skUDA
  • 316
  • 1
  • 8
  • Thanks, that was super fast! That fixed what has managed to baffle me for hours. And browsing around I found the answer to my next question of why 'scrollview1.scrollTo(40,0); ' was not working either. http://stackoverflow.com/questions/3263259/scrollview-scrollto-not-working-saving-scrollview-position-on-rotation – CharCar Jul 17 '12 at 22:42