ClassCastException in Android ScrollView - java

There is a class in my project (B) which extends (not directly) ScrollView (A) which can be used to set a value in the month field scroll which is vertical
Here is the inheritance heirarchy:
B extends D which extends an abstract class E which extends A
I am using a robotium method which gets all the scroll views in the present activity which returns an ArrayList of scrollviews(ArrayList) .
I am trying to assign the first element of that arraylist ArrayList.get(0) and trying to cast it to the B object doing it as follows:
ScrollView scrview = new ScrollView(a.getApplicationContext());
ChildScrollView scrview1 = new ChildScrollView(a.getApplicationContext());
scrview = solo.getScrollViews().get[0];
scrview1 = (ChildScrollView) scrview; // I get the classcastexception here
Is there any way to get around this?
I also tried
ChildScrollView scrview1 = new ChildScrollView(a.getApplicationContext());
ScrollView scrview = (ScrollView)new childScrollView(a.getApplicationContext());
//Also tried this --> ScrollView scrview = (ScrollView) scrview1;
scrview = solo.getScrollViews().get[0];
scrview1 = (ChildScrollView) scrview; // I get the classcastexception here
Any help would be appreciated. Let me know if I am missing something.

You are getting an exception because the cast cannot work like that. Think about it this way: All ChildScrollView are also ScrollView, so it makes sense to use them in a ScrollView context. But a ScrollView object may or may not be a ChildScrollView.
You could use reflection to try to determine the object's class at runtime before casting to ensure that the cast is safe. However, I'm not sure what you're trying to accomplish with your code above. You created a new object referred to by scrview1, but then immediately assign that variable to scrview? Can you give any more details about what you want to accomplish?

Related

Vaadin-8 ComboBox click has no effect

I have a ComboBox in my Vaadin 8 code.
allAtts here is a Set of Attendant-s, and the field theAtt in there is of type String:
private ComboBox<Attendant> theCB = new ComboBox<Attendant>(null,allAtts);
theCB.setEmptySelectionAllowed(false);
Binder<SearchArgs> binder = new Binder<SearchArgs>(SearchArgs.class);
binder.setBean(sas);
binder.forField(theCB).bind("theAtt");
..
VerticalLayout vl = new VerticalLayout(theCB, rb, deleteBtn);
What's more - this exact flow of theCB is a copy-paste from another class i wrote, on the very same type Attendant.
The problem here is the drop-down click on theCB is not working. That is, clicking that little chevron-down icon on the ComboBox is producing no effect. What could this be!?
I tried removing the other items, rb, deleteBtn in vl. Still didn't work.
Nothing else happening to theCB anywhere else.
TIA.
EDIT:
theCB is otherwise functional. typing in the field is allowed, brings the choices and returns the selection accurately.
EDIT-2
https://github.com/vaadin/vaadin-combo-box/issues/680 is not the issue. nothing changing when i pass a non-null label value to the constructor. Besides, it's working fine with null label in that other place in the code.
EDIT-3
Attendant contains several fields, one of which is String att.
SearchArgs composes Attendant in a its field theAtt. Attendant.toString() returns att only.
Once again - this exact same logic is working, as i'm typing these, in another part in the code.
Also note: i since also tried the following without success:
binding theCB on a separate binder, i.e. declare a second binder on sas and use that one for binding theCB
cloning the Attendant objects that go into theCB as data-provider.

ImageView hiding and showing

I'm struggling to get my android app to show and hide imageView objects programmatically. Actually I'm struggling to get expected behavior from imageView objects full stop.
Following the answers to this question,
Here's what I'm testing with:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageView warn = (ImageView)findViewById(R.id.gpsStatusWarning);
warn.setImageResource(R.drawable.gps_error);
warn.getLayoutParams().height = 64;
warn.getLayoutParams().width = 64;
}
}
The above code is called in the parent activity's OnCreate method, and it does exactly what I expect: It changes the image of the object to be the one designated, and it sets the height and width of said object. However, what I can't seem to do is to set the object as INVISIBLE or GONE. I just can't make it vanish at all, in fact. I've tried both:
warn.setVisibility(View.INVISIBLE);
warn.setVisibility(View.GONE);
But the image is still visible. I've even tried changing it in the XML to
android:visibility="gone"
But even that hasn't helped. The image is still visible.
What am I doing wrong? Am I missing a call to some update method? Does setting the image resource force the image to be drawn?
Try :
warn.setImageResource(0);
Or : warn.setImageResource(android.R.color.transparent);
This is how you set the visibility of ImageView objects on Android programatically:
yourImage.setVisibility(View.VISIBLE);
yourImage.setVisibility(View.INVISIBLE);
yourImage.setVisibility(View.GONE);
You can also set the initial states of ImageView objects in XML layout files like this:
visibility="visible"
visibility="gone"
visibility="invisible"
You can follow the official documentation about ImageView controls to try it on yourself on this link below. Learn how to set the visibility state of a view.
imageView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);

TextView Outline in Android

I am trying to implement TextView outline in one of my android application which is described here
I have made custom textView class as mentioned and there no any error. I am trying to use it in my activity but as I am learning yet, I am confused to use it in Activity as setStroke method.
I am trying as below
text_quotes.setStroke(0,R.color.toolbar_color,0,0);
can anybody please suggest me this four filed which value I should enter?
in CustomeTextView class is defined as below
strokeWidth = width;
strokeColor = color;
strokeJoin = join;
strokeMiter = miter;
I have issue in strokeJoin field, which value I should enter for it ?
Thanks
StrokeJoin seems to be a value of the Paint.Join enumeration.
So you should try one of the Paint.Join values like :
Paint.Join.MITER;
Paint.Join.BEVEL;
Paint.Join.ROUND;
Look the different values here : https://developer.android.com/reference/android/graphics/Paint.Join.html

JavaFX How to get data of selected row from Tableview

I would like to get data from one selected row
I have this little code
Stlpce aktualne = (Stlpce) tableview_objednavka.getSelectionModel().getSelectedItems();
double aktcena = aktualne.getCena();
But When I run the app I get this error
Caused by: java.lang.ClassCastException: javafx.scene.control.TableView$TableViewArrayListSelectionModel$5 cannot be cast to sample.Stlpce
I use scene builder for create TableView.
Can you help me?
This is solved - the problem was, that I have getSelectedItems(); instead of getSelectedItem();
If you only care about which row is selected, assuming you have a TableView, you can simply use:
List selected = selectionModel.getSelectedItems();
or if your table only allows single row selection:
SomeObject selected = selectionModel.getSelectedItem();
System.out.println(selected.getName());
Try it 100% working...
or try this for better understand Get row data from TableView
The exception is clear in its meaning:
http://docs.oracle.com/javase/7/docs/api/java/lang/ClassCastException.html
The method call you make (https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableView.TableViewSelectionModel.html#getSelectedItems--) returns a reference to an
ObvservableList<T>
Object, which Stlpe does not implement/extend and therefore a Stlpe reference cannot point to such an Object.
Is the Stlpe class the Type class of your ObservableList? If so, maybe you need to find the Stlpe Object in your list:
if (returnedList.size() > 0) {
Stlpe item = returnedList.get(0);
}
In your declaration the TableView should be casted to your object
eg: TableView <Stlpce> tableview_objednavka;

ShareActionProvider Rewrite

I am trying to modify the ShareActionProvider function, so that I can intercept the onClick event and prepare the content that I want to share and then call setShareIntent(). So far, I've just copied the source code for ShareActionProvider() to MyShareActionProvider() and trying to compile/execute with my copy of the code.
The ShareActionProvider() uses ActivityChooserModel and ActivityChooserView classes which are defined inside
android.support.v7.internal.widget.ActivityChooserModel and
android.support.v7.internal.widget.ActivityChooserView
Everything seems ok, except for the activityChooserView.setProvider(this) call in the onCreateActionView() function.
#Override
public View onCreateActionView() {
// Create the view and set its data model.
ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName);
ActivityChooserView activityChooserView = new ActivityChooserView(mContext);
activityChooserView.setActivityChooserModel(dataModel);
// Lookup and set the expand action icon.
TypedValue outTypedValue = new TypedValue();
mContext.getTheme().resolveAttribute(R.attr.actionModeShareDrawable, outTypedValue, true);
Drawable drawable = mContext.getResources().getDrawable(outTypedValue.resourceId);
activityChooserView.setExpandActivityOverflowButtonDrawable(drawable);
activityChooserView.setProvider(this);
// Set content description
activityChooserView.setDefaultActionButtonContentDescription(
R.string.shareactionprovider_share_with_application);
activityChooserView.setExpandActivityOverflowButtonContentDescription(
R.string.shareactionprovider_share_with);
return activityChooserView;
}
It seems that the ActionProvider that is used inside activityChooser.setProvider() comes from android.support.v4.view.ActionProvider and not android.view.ActionProvider.
If I change the import for the ActionProvider to android.support.v4.view.ActionProvider, then the compile goes through Ok and I can start my app, but when the menu is inflated, I get an error saying MyShareActionProvider (which is used in menu.xml with the full path and is an extended class of android.support.v4.viewActionProvider) cannot be cast to android.viewActionProvider, which is what I guess inflater.inflate() returns.
Not sure how to proceed from here.
Thank You,
Gary

Categories