TabHost and/or ViewFlipper DialogProblem - android

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.

Related

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.

Adding Custom ImageButton to layout in Android causes error

I will start by saying this, while I have some Java training, is my first foray into development for Android.
What I have done is created a custom ImageButton called MapCell that is basically an ImageButton that holds a few extra pieces of information, and it compiles fine.
My problem comes when I want to procedurally create a MapCell in the relevant Activity and add it to my TableLayout which is defined in the xml and has the id 'mapTable'. The relevant bit looks like this:
Random randy = new Random();
MapCell n = new MapCell(randy.nextInt(4), this); //the random number is part of my extra info
findViewById(R.id.mapTable).addView((View)n, 20, 20); //add cell to display
I get one error out of that:
The method addView(View, int, int) is undefined for the type View
Which to me sounds like utter nonsense. I put that View cast in there as desperation after I got this same error with n sitting by itself and nothing changed (Obviously my MapCell is already a View since it extends ImageButton).
I hope a new pair of eyes can tell me what this is about, since I've checked for similar problems and I didn't find any quite like this. Let me now if you need to see more code.
The method findViewById returns a View and the View class doesn't have the method addView(this method is implemented in the ViewGroup and its subclasses). Instead you should write:
((TableLayout)findViewById(R.id.mapTable)).addView(n, 20, 20);
I've cast the return of the findViewById method in a class that actually has the addView method.
You got this problem because method findViewById(R.id.mapTable) returns View object.
In android you can't add one View to another.
You can use addView function with ViewGroup, and all LinearLayout (etc.) objects.

How to add Layout as a child

In my application I have 2 layouts. One's is root layout that changes dynamically and the second is just a form that opens many times. The form must be child of the root layout but I'm failing to perform so.
I assume that I should simply use:
main.AddView(formLayout)
but I can't figure out how get this formLayout object.
Will thank you for possible answers.
Sounds like you need the LayoutInflater object Android reference.
This allows you to create an object from the xml layout in your project.
With the advice of cjk I wrote piece of code that actually answers my question:
setContentView(R.layout.main);
main = ((ViewGroup)findViewById(android.R.id.content));
LayoutInflater inflater = this.getLayoutInflater();
ViewGroup form= (ViewGroup) inflater.inflate(R.layout.formLayout, null);
main.addView(form);
Thank you all
Not sure if I understand the question properly, but something like that might work:
View myView;
myView = (View) this.findViewById(R.id.formLayout);
main.addView(myView);
By get I figure you mean you want to retrieve a field in the new opened layout, you can do it by making it a new Intent and using startActivityForResult instead of startActivity.

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