Following code generates exception as:
java.lang.ClassCastException: java.lang.Integer cannot be cast to vro.nagainfo.vromain.model.VROUser.
listUsers is a custom listview and VROUser is a class
listUsers.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
VROUser user = (VROUser) listUsers.getItemAtPosition(position);
Intent intent = new Intent(getActivity(), ProfileViewActivity.class);
intent.putExtra("userId", user.getUserID());
getActivity().startActivity(intent);
}
});
How to fix this?
ClassCastException means you are declaring a variable of some type and assigning it to another type you have defined in a layout xml file...
Example in your xml:
<Button
android:layout_height="wrap_content"
android:id="#+id/btn1"
android:layout_width="wrap_content">
</Button>
And in your java code
ImageView img1 = (ImageView)context.findViewById(R.id.btn1);
There is no compile error. But you are face ClassCastException
Check your ListVIew. How did you declare in both xml and java code
Correct:
VROUser user = listUsers.get(position);
Here, listUsers.getItemAtPosition(position); it is giving integer value,but you have casted to VROUser.
What are you doing with this code???
VROUser user = (VROUser) listUsers.getItemAtPosition(position);
Use like this
VROUser user = listUsers.get(position);
Use it
Related
My question is about Android/Java.
I wanna access to other views from my custom view.
My main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLinearLayout1">
<org.javaforum.input
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
required=""
android:hint="Enter your E-Mail"
oninvalid="this.setCustomValidity('SO - Please enter a correct E-Mail!!!')"/>
<org.javaforum.input
android:layout_width="wrap_content"
android:layout_height="wrap_content"
inputType="submit"/>
</LinearLayout>
So when the second "input" view is clicked, I wanna check if there exists an attribute named "required" at the first "input" view.
I tried this in my input.java:
public class input extends TextView{
public input(Context context, AttributeSet attr) {
super(context, attr, getIdentifier(context,attr));
}
#Override
public void onFinishInflate() {
setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLinearLayout1);
View v = null;
for(int i=0; i<layout.getChildCount(); i++) {//Error
//Code
}
}
});
}
}
But I get the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.LinearLayout.getChildCount()' on a null object reference
I don't understand why it's null. It should be already created, right?
So how can I get an LinearLayout reference of my main.xml at my custom view correctly?
To solve the null pointer exception, you will need to search for the other widget in the parent LinearLayout. You can use View.getParent() to identify the parent. Since the view you are looking for is a sibling of the current view, you will find it in the parent.
As for determining if a certain attribute is set, I recommend that the value be captured by its view and a method provided to present that value upon request. Say the value is your "required" value then capture that value locally and provide a method, getRequiredValue(), for instance, to expose it.
You can use
LinearLayout layout = (LinearLayout) getRootView().findViewById(R.id.mainLinearLayout1);
if you only call findViewById inside custom view it only lookup the child view.
This should be called in the Activity's onCreate instead of TextView's onFinishInflate.
First give the submit button an id eg btn_submit, then in onCreate
Button btn_submit = (Button)findViewById(R.id.btn_submit);
btn_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLinearLayout1);
View v = null;
for(int i=0; i<layout.getChildCount(); i++) {
//Code
}
}
});
Am trying to use transitions in my App but am facing this problem,
Cannot cast Int to android.view.View
Below is my code:
holder.newsRoot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newsIntent = new Intent(NewsAdapter.this.c, NewsDetails.class);
newsIntent.putExtra("newstitle",NewsTitle);
newsIntent.putExtra("detailednews",DetailedNews);
newsIntent.putExtra("newsdate",NewsDate);
newsIntent.putExtra("newsphoto",NewsPhoto);
ActivityOptionsCompat options= ActivityOptionsCompat.makeSceneTransitionAnimation(this,(View)news_image,"image_transit");
c.startActivity(newsIntent,options.toBundle());
}
});
Am trying to integrate it in the Adapter class, but this is the line where the problem is :
`ActivityOptionsCompat options= ActivityOptionsCompat.makeSceneTransitionAnimation(this,(View)news_image,"image_transit")`;
** problem is found on these words**
(View)news_image
My xml code of the ImageView.
<ImageView
android:id="#+id/news_image"
android:layout_width="80dp"
android:transitionName="image_transit"
android:layout_height="80dp"
android:src="#drawable/bbb"/>
PROBLEM: in android, you store the id's of your objects as int, but the int is the view id, not the view itself.
SOLUTION: in order to work with it you need to get the view use findViewById.
ImageView yourImageView = (ImageView) findViewById(R.id.news_image);
In your case you can also use it directly in the problematic method:
ActivityOptionsCompat options=
ActivityOptionsCompat.makeSceneTransitionAnimation(this,
findViewById(R.id.news_image),
"image_transit")`;
Because you placed it in the xml layout:
<ImageView android:id="#+id/news_image"
Can someone help me with this error that I keep getting?
The program that I'm trying to make is online streaming radio.
I need to return the value "value" so i can use it in another java class.
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,ch);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public int onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), mPlayer.class);
i.putExtra("name", ch.get(position));
int value = listView.getItemAtPosition(position);
startActivity(i);
return value;
}
}
The problem occurs in this line:
int value = listView.getItemAtPosition(position);
getItemAtPosition() returns an Object, so you must convert it to an int before assigning it to an int variable. You should do this only if you're sure that your listView contains integers, otherwise you'll get a ClassCastException. Do it like this:
int value = (int)listView.getItemAtPosition(position);
Also, if you startActivity() before using the value anywhere, then it does not make much sense in getting it. Use putExtra to pass it to your next activity if you wish to use it there.
I want to use this line of code I've seen in several examples:
lvRadios.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) lvRadios.getItemAtPosition(position);
int idRadio = cursor.getInt(cursor.getColumnIndex("ID"));
Log.i("ID", String.valueOf(idRadio));
ShowResults(position, view);
}
});
But I get the following error:
java.lang.ClassCastException: java.lang.String cannot be cast to android.database.Cursor
I'm using Android 6.0 with Android Studio 2.1.2
I hope you can help me.
Regards.
Your error is telling you that you're trying to turn a string into a cursor. For the operation you're performing, you wouldn't get a cursor. You would get a UI element or something tied to a UI element.
Comment out what you already have inside of the onItemClick and replace it with:
String radioString = (String) lvRadios.getItemAtPosition(position);
//Debug: Show a toast to see what information you're getting
Toast.makeText(getApplicationContext(), radioString, Toast.LENGTH_LONG);
Then, you can do what you want once you know the result.
I'm trying to do my first Spinner, and I have encountered some difficulties, such as that I don't know if I can get an option by spinner.getSelectItem == "some string".
Take a look at my code so far
Populating the spinner:
public void addItemsOnSpinner() {
Spinner buttonSpinner = (Spinner) findViewById(R.id.buttonSpinner);
List<String> list = new ArrayList<String>();
list.add("Ultimos 5 lancamentos");
list.add("Ultimos 7 lancamentos");
list.add("Ultimos 10 lancamentos");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
buttonSpinner.setAdapter(dataAdapter);
}
Trying to make an if statement:
if(buttonSpinner.getSelectedItem().toString() == "Ultimos 10 lancamentos"){
textView.setVisibility(View.VISIBLE);
}
TextView code as requested:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Deposito"
android:visibility="invisible"
android:id="#+id/textView"
android:layout_row="2"
android:layout_column="0"
android:layout_gravity="center|left" />
And its code on the class:
TextView textView = (TextView)findViewById(R.id.textView);
Yes you can do it and it will work fine, but please use
buttonSpinner.getSelectedItem().toString().equals("Ultimos 10 lancamentos");
As Stefano has pointed out, your comparison should be using equals (which compares the String contents, vs == which compares the object references).
Otherwise your if statement should work, however its not clear where you are calling it from (and that might be the cause of the problem). If you want to make the comparison immediately after a spinner item is selected then you need to set an OnItemSelectedListener and make the comparison there.
Here is an example of how you might declare this listener inline:
buttonSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getSelectedItem().toString();
if (selectedItem.equals("Ultimos 10 lancamentos"))
{
textView.setVisibility(View.VISIBLE);
}
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});