Got exception in create run time editText object - android

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.

Related

empty EditText caused by wrong view?

I'm new to android studio and programming. I create second table to my database and have almost identical onClickListener class first one work perfect but second give me error. EditText .getText().toString() is empty so I can't convert it to dobule. I think this problem may be caused by View but I dont know java that well and I cant find solution.
EDIT: I manage to solve this problem by adding AlertDialog to my second OnClick listener. This is not exactly what I want but it works.
Could you try setting the input type on the edittext as numberDecimal
android:inputType="numberDecimal"
That way, the user will have to input decimal number always if thats what you are expecting the user to enter.
Also, you could do a null check
if(stringWzrost != null && !stringWzrost.trim().equals("")) {
bmiWzrost = Double.valueOf(stringWzrost);
}
Right now I see that its commented out.

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

getting logcat error as cannot be casted

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.

setText on EditText and TextView not working

We have a bigger Android 2.1 project, and for some reason, only at two places calling setText() on an EditText or a TextView object just doesn't work. It does not crashes, does not throw an exception, only the TextView/EditText is not updating.
Here's the code for setting the text:
EditText etzip_a = (EditText)activity.findViewById(R.id.editTextZip_a);
etzip.setText(m_addressA.zip);
etzip.addTextChangedListener(new onInputChanged(120));
this code runs on the GUI thread
it parses the R.id.editTextZip_a object successfully from the layout
a totally similar code for other tens of EditText are working fine
Does anybody encountered a problem like this?
try using this code:
You have create a object etzip_a of the EditText. Now after that you are using different object etzip.
EditText etzip_a = (EditText)activity.findViewById(R.id.editTextZip_a);
etzip_a.setText(m_addressA.zip);
etzip_a.addTextChangedListener(new onInputChanged(120));
EditText etzip = (EditText)activity.findViewById(R.id.editTextZip_a);
replace this line of code

TabHost and/or ViewFlipper DialogProblem

I have some problems with the TabHost and ViewFlipper.
Here are the ViewFlipper as I expect the answer to this will also do the job in the TabHost.
I would like to have a Custom Dialog shown when the user reach a certain stage, but I can not figure out which Context to hand it?
final Dialog congratsDialog = new Dialog(MyActivity.this);
congratsDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
congratsDialog.setContentView(R.layout.congrats_dialog);
TextView name = (TextView) congratsDialog.findViewById(R.id.congratsDialogName);
name.setText(player.getName());
This will result in a NullPointerException in the line were I try to setText.
I have also tried flipper.getContext(), getBaseContext(), getApplicationContext() and have also tried other crazy thing but every time I get a NullPointerException
setContentView() to dialog is trade off over android version if you are using android 2.0 or less it would not work use versions 2.0 or above for this function. Otherwise if you want to do for all version then use setContentView(View) where View is from xml layout of congrats dialog after inflating it.
Please try this and let me know if you got solution.

Categories

Resources