getting logcat error as cannot be casted - android

I want to generate a listview from sqlite database but the
LOGCAT shows error.
What does this below error mean?
09-23 12:01:20.354: E/AndroidRuntime(3682): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abhishekp.listproduct/com.abhishekp.listproduct.List}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ListView

Its Happen only because of..two possibilities.
1) If you are using TextView in xml file...
and try to initialize it with.
TextView tv=(ListView)findViewbyId(R.id.textView);
and if it is right then change it with
TextView tv=(TextView)findViewbyId(R.id.textView);
2) May be you are trying to put id of listView instead of text view.
like
TextView tv=(TextView)findViewbyId(R.id.listView);
if it is then should change
TextView tv=(TextView )findViewbyId(R.id.textView );
And if it is all good then try to clean your project and rebuild it...
Thanks!!

From your logcat its clearly showing that you are trying to cast your TextView in ListView. Check you may have declared your TextView i.e.
TextView tv=(ListView)findViewbyId(R.id.textView);
or may be you are referring the id of your ListView in your java file.
TextView tv=(ListView)findViewbyId(R.id.listView); //id defined is of listview

Eclipse tends to mess up your resources every now and then. This leads to some odd behavior such as strings and images being swapped all over your app, and more commonly classCastException(s), which happen when Eclipse switches your Views' ids around.
Just clean your project.It will work fine.

Related

Android espresso accessing EditText in adapter in first row

I am using Espresso for testing my app. I have a listview with some data in it. I want to check the value of an edittext in the first line.
I tried:
onData(withId(R.id.editTextKommissioniert)).inAdapterView(withId(R.id.jflArticleList_ListView)).atPosition(0).check(matches(withText("60.0")));
But I always get this exception:
android.support.test.espresso.AmbiguousViewMatcherException: 'with id:
at.stockserv:id/editTextKommissioniert' matches multiple views in the
hierarchy.
What can I do, to access the first line in my adapter?
Try below approach , Working for me
onData(anything()).inAdapterView(withId(R.id.jflArticleList_ListView))
.atPosition(0).onChildView(withId(R.id.edt_id)).check(matches(withText("60.0")));

TextView setText Cannot resolve type

Im using Android Studio, trying set text to Text View but every time i do this
TextView test = (TextView) findViewById(R.id.testView);
test.setText("fds");
on Android Studio i see Cannot resolve symbol 'setText'.
Can someone tell me what im doing wrong?
setText() will accept String which you have passed, so there is no issue in that.
One possible reason for this might be you have not written your code inside onCreate() or onCreateView().
If it is Activity you need to use these lines after setContentView() in onCreate().
If it is Fragment you need to use these lines after inflating your view in onCreateView().
Second reason, you might be having one more test variable of different type like String or something else

Got exception in create run time editText object

I attempt to create an EditText object at run time this way:
EditText et=new EditText(MyActivity.this);
And i have got below exception in Samsung galaxy tab in other tablet it works fine.
android.os.Handler.<init>(Handler.java:121)
android.sec.clipboard.ClipboardExManager$1.<init>(ClipboardExManager.java:90)
android.sec.clipboard.ClipboardExManager.<init>(ClipboardExManager.java:89)
android.app.ContextImpl$8.createService(ContextImpl.java:292)
android.app.ContextImpl$ServiceFetcher.getService(ContextImpl.java:199)
android.app.ContextImpl.getSystemService(ContextImpl.java:1158)
android.view.ContextThemeWrapper.getSystemService(ContextThemeWrapper.java:79)
android.app.Activity.getSystemService(Activity.java:3932)
android.widget.EditText.<init>(EditText.java:68)
android.widget.EditText.<init>(EditText.java:62)
android.widget.EditText.<init>(EditText.java:58)
com.example.myProject.MyActivity.insertTextBox(MyActivity.java:8249)
com.example.myProject.MyActivity$19.run(com.example.myProject.MyActivity.java:8057)
I have solve this issue just by adding one view from xml layout which visually gone and than add all view runtime in linearlayout and its works fine. I don't know what's the issue but it's solve.
You should really post more code for the problem to become apparent.
Based on the little information you provided my guess is you tried to create that EditText before the context is initialized.
Try to move that code in the onCreate() method if it's not already there.

facing problem while displaying the value of the variable

I have just tried to display the value of the variable but is showing some kind of exception
that is FORCE CLOSE.
the code i have tried is
TextView myTextView = (TextView) findViewById(R.id.result9);
myTextView.setText("your score is " +count);
considered count=0 intially.
Can any one suggest me for this problem
thanks in advance
if myTexyView is in dialog you have to do this:
myTextView = (TextView) dialog.findViewById();
anyway do clean project...this happens sometimes:
in eclipse: project->clean->your project
Seems findViewById() returns null. It means that either there's no widget with id result9 in current layout or your forgot to setContentView().
can please check your xml which one you have set, setContainView() it mast have TextView with "result9" this id.

java Runtime exception

TextView txtOtherMatches = (TextView) dialog.findViewById(R.id.txtOtherMatches);
txtOtherMatches.setText("Other Matches");
i m getting this error while running application and i m just assigning simple text to Textview at run time .....
java.lang.NullPointerException
Make sure your TextView in the xml has exactly the same id: android:id="#+id/txtOtherMatches"
Check that you don't have more than one res/layout folder (e.g., layout-normal, layout-large or layout-land) and, if you do, make sure that your TextView is present in the layourt corresponding to your device/emulator
Check that it was added to the current View (most probably, through some ViewGroup)
If everything else fails, clean the Eclipse project and rebuild
txtOtherMatches is probably null because it doesn't exist in the current view.When you use findViewById the view must be in the "contentview" you've set with setContentView, or added later tot the view. You can't find any "random" view that has an ID somewhere, it must actually be "present" in your current view.
If the view is NOT in the xml you're using already, but somewhere else in one of your xmls, you must use an inflater to get the View, and add it with View.add()
It would be helpfull if you provide a few more lines before that one.
But my guess is Nanne is right.
Try to add a safety check:
(this wouldn't change the code flow)
if(txtOtherMatches == null) {
throw new NullPointerException("darn, the R.id.txtOtherMatches is not in the dialog")
}
(on the other hand Nanne mistakenly mentions 'current view' while I'm sure he meant the 'dialog' view.
Put some more meat if you need further assistance ;)

Categories

Resources