I am trying to call a new activity from a fragment. I have a button at the toolbar, and when it is pressed the following code is being called (this event is inside the fragment):
Intent secondactitvity = new Intent(this.Activity,typeof(Activity_SecondActivity));
StartActivity(secondactitvity );
Now the new activity is being called and here is my code inside OnCreate of the new activity:
RequestWindowFeature(WindowFeatures.ActionBar);
ActionBar.SetIcon(Resource.Drawable.icon_toolbar_back_new);
ActionBar.SetCustomView(Resource.Layout.ActionBar_Custom_Layout);
ActionBar.SetDisplayShowCustomEnabled(true);
base.OnCreate(bundle);
SetContentView(Resource.Layout.SecondActivity_Layout);
Here is the SecondActivity_Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView1" />
<fragment
class="frag.Fragment_Second"
android:id="#+id/fragment_secondActivity"
android:paddingTop="10dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Here is the layout for the fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="#string/secondFragment_name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:paddingLeft="10dp" />
</LinearLayout>
And here is the fragment code:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.Fragment_ProjectDetails, container); //view;// base.OnCreateView(inflater, container, savedInstanceState);
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Toast.MakeText(this.Activity, "activity for second fragment created",ToastLength.Short).Show();
base.OnActivityCreated(savedInstanceState);
}
Now the problem is. The second activity remains empty, it is not being filled with the fragment (but there is not an error message). I would understand it if the fragment cannot be found or something like that, but in this case all the functions from the fragment (onCreateView, onActivityCreated) are being called, and the Toast is also being displayed, but the activity is empty. Then I tried to insert a text view at the top of the fragment inside the second activity layout file, and its being shown but, the controls from the fragment remain un-visible. Then I tried to change the fragment associated to the second activity, I changed it to the fragment which is calling the activity, and this one is being properly loaded (I changed it inside the .axml file).
What am I missing here, for me it seems like the second one is not being created property and thus the controls cannot be displayed (but, I may also be wrong)?
Thank You
well after spending far too much hours on this i got the solution. First thing i have noticed is that i cannot acces Views from the second fragment layout (when trying to read them in code i could not compile). Then i created a new axml file, assigned it as the layout for the second fragment, and the controls are being displayed.
So for me it seems like a bug in Xamarin, somehow the first axml has not been registered accordingly (but funny thing is that i could access the axml per se, but not the controls).
I am somebody who is migrating from Java (Android development) to Xamarin, and i have put a lot of hope on it. But I must admit that after few weeks lots of the first enthusiasm is gone. There is a lot missing in Xamarin and as time is passing by I am more thinking about moving back to Java and start learning Objective C for iOS. This project which i am doing now in Xamarin, i would have done it already if I were using Java, but for now I am still in the early phase (of course it cannot be as fast as in Java because i have to google for everything, but ...). Thers is also the issue with the Resource file, sometimes when my project breaks I cannot stop it from executing and i have to kill Visual Studio via Taskbar. In most cases when I do this the Resource file gets "destroyed" and when I open the project again i cannot start it anymore (in most cases I didnt bother because I was practising and I created a new one, but what if it is a bigger project ?)
The next thing is the missing IntelliSense inside the .axml when the designer is open (when I open it with the xml viewer i get the IntelliSense). Do i have to switch always between these views ?
And now this thing with the fragment, how i am supposed to trust the system if such cruical things dont work right.
Since I am still in the trial period I will give this whole thing a chance, but as I sad, the "fun" working with it is decreasing ...
Sorry for the long post, but just had the need to say :)
I had the same problem and nothing I had done solved it, including removing the layout and adding it again even with different name.
I figured out that I had messed up the method and forgot the override keyword. After this fix, it worked just fine.
Related
I created an Activity, which I load from a fragment within my "main" activity.
Within this new activity I placed an ImageView with an image from my drawable directory (I have about 10 other drawables I present the exact same way in the main activity).
The problem is that although in the IDE I see the image, it is not displayed in run time (via the simulator).
-- to clarify, the image is static hence the difference compared to other questions (I'm not loading it by code).
Any ideas on how to solve it?
My hunch is that it relates to the fact it's a new activity, but I'm new to android development, so I can't base it on any knowledge...
I did not add any code to the java section of the activity (didn't touch any "on.." nor added functionality to this specific file)
P.S. I tried cleaning the project, tried presenting an image that is presented on the main activity, and restarting anything that I can... nothing helped :(
My Activity xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="reducted">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/manageSubscriptionScreenLogo"
android:layout_width="match_parent"
android:layout_height="110dp"
app:srcCompat="#drawable/managesubscription_header" />
</LinearLayout>
</RelativeLayout>
It doesn't matter I take the ImageView and place it outside of the LinearLayout (and remove it), or change the size to wrap_content or match_parent (I tried all combinations), the image is not presented.
How it looks in the IDE:
And how it looks in run time:
Try using android:src="#drawable/managesubscrip_header" instead of app:srcCompat="#drawable/managesubscription_header" in ImageView to avoid automatic scaling of the drawable.
Let me know if it helps
sorry for the long post, i tried to make it as readable as possible.
I have a decent knowledge of JAVA and trying to understand Android programming. Today i have been working on my knowledge of fragments.
I have read this article and got the basics of working with them.I managed to make fragments and replace them when needed.
I proceeded by taking this project, and thought it might be fun to try and add a fragment to it with a basic tictactoe game i had build for practising purposes.
Now i am having problems with replacing the fragment. Everything works fine until I add the TicTacToe view in the mix, which has a tablelayout. The fragment is not getting replaced, rather is it adding an extra fragment (or so it seems) and uses this to switch fragments in.
BoardField is simply a class I made which extends Button
The layout thats not working for me is this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0f0fdf">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.javacodegeeks.android.fragmentstest.BoardField android:id="#+id/my_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonDefault"
android:onClick="selected"
/>
<com.javacodegeeks.android.fragmentstest.BoardField android:id="#+id/my_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonDefault"/>
<com.javacodegeeks.android.fragmentstest.BoardField android:id="#+id/my_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonDefault"/>
</TableRow>
</TableLayout>
I omitted the other rows for readability, they are similar.
So my question is, why is replacing the fragments not working when I use TableLayout ??
EDIT:
Eric's solution worked, i hope to find out more about why?
Why parent view is TableLayout? Try to changing it to a RelativeLayout with a child, the TableLayout (just for test purpose)
With a RelativeLayout as parent view, are you able to "replace" instead of "add" the Fragment?
I think your mistake is not here. Perhaps your can look in code where you create new fragment or code where you replace fragment. There is no sense TableLayout make a problem to replace fragment. Eric is right RelativeLayout is better then TableLayout, but this is not the problem. Can you put more of your code?
I have a layout as below:
Let's name it my_layout.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ffffff"
android:background="#19396a"
/>
<TextView
android:id="#+id/textView2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ffffff"
android:background="#19396a"
/>
</LinearLayout>
<ListView
android:id="#+id/uilistView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
In my activity I have a method in which I initialize all the UI elements i.e I do mapping of activity UI variables to layout.
Assume, the method is as below:-
//used to initialize UI elements
//called in oncreate() method
public void initializeUIelements() {
......
activity_listView = (ListView) findViewById(R.id.uilistView);
}
Here, activity_listView is a class level variable of type ListView
In my project, I have res\layout as well as res\layout-land and my_layout.xml exists in both the folders.
But sometimes during activity restart/when it's created I get a NullpointerException while initializing activity_listView that comes from R.java.
I know:
We get NullpointerException similar to what I am asking when an
element does not exist in any of the layout folder(i.e it exists in layout-land but does not exist in layout folder).
But here,the element exists in both folders and even though I get this inconsistent error
i.e. I am not able to produce it always but sometimes it starts coming.
So,please help me in analyzing as what may be the reasons when we get this error apart from the one mentioned by me above.
Thanks in advance.
If you could provide a more specific example, it might be easier to debug the exact problem. A few reasons why your View might be null:
The layout you've set as the content view does not include a view with this id:
If you reference a View that exists in the R file (a file that holds references to all elements within the res folder), say uilistview, in an Activity where you are not setting it's layout container as the content view (in this case this would be my_layout.xml), you will not get an error in Eclipse or whatever IDE you are using. Android will look for a View with this id within the layout, and if it can't find it, it will return a null value.
You call findViewById too early
You must use the method setContentView(R.layout.some_layout) before you try to access any of the elements with findViewById(). Logically, it makes sense - you need to tell Android where it should look where it's find an element
Looking for an element inside an inflated View
This is similar to the first, but it's common. If you've inflated a view and then want to manipulate a View inside the inflated View, you must instruct Android to look in the right place. For example:
RLayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View smallerView = layoutInflater.inflate(R.layout.mylayout);
If you want to reference something within mylayout.xml, you would need to use this:
TextView text = (TextView) smallerView.findViewById(R.id.text_view);
rather than:
TextView text = (TextView) findViewById(R.id.text_view);
The first indicates to look into the inflated view by placing the findViewById method on the inflated view, while the second one applies it to the current View of the Activity, which will cause a null value to be return.
Clean your project
This is the most frustrating answer, because this is not your fault. Sometimes the R.java class messes up and causes wrong references to id's while it is being compiled/built. Try cleaning your project and correcting the wrong references by looking for Project -> Clean in the main bar of Eclipse (or the IDE you're using).
I'm developing an app with SupportMapFragment and custom view switching mechanism. Right now i styles app, to make its window to be transparent using styles.xml, but with that change i see a strange problem when using maps. When user switches it current view from one that uses map fragment to another, part of screen that was used by map on previous view becames transparent, for about 1 second.
In image below i present some description, on right i marked using yellow color situation than im talking about:
on left side is view that i present map using SupportMapFragment, on the right is one i have switched next, with 2 diffrent views(ImageView and TextView),but yellow part of TextView is transparent like my application window, su user can notice eg. apps menu. This is not what im trying to achive so Im looking way to get rid of this effect.
My code for switching views is like follows:
public void switchAppView(View view){
mMainView.removeViewAt(0);
mMainView.addView(view,0);
}
the mMainView is initialized in onCreate Method, by code:
#Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.activity_main);
mMainView = (RelativeLayout) findViewById(R.id.mymain_layout);
}
my activity_main.xml layout presents similar to:
<RelativeLayout
android:id="#+id/mymain_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="#+id/replacable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
></FrameLayout>
<FrameLayout
android:id="#+id/extra"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
></FrameLayout>
</RelativeLayout>
I tried to search clues and situations like that but I didnt find any, so if anyone can help i would be grateful, Thanks for help.
becames transparent, for about 1 second.
I'm guessing you are using black background and you really mean it becomes black.
In that case what you see is a known issue.
You may read more about it here:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=4659
and here:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=4639
I recently updated my phone to gingerbread and to my horror one of my apps stopped working!
The app consists of a SurfaceView and a layout (view) that shows up as an overlay over this SurfaceView.
THe xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.mycompany.MySurfaceView
android:id="#+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<include layout="#layout/settings"
android:layout_alignTop="#id/myview"
android:visibility="invisible"
android:id="#+id/settings"
/>
</RelativeLayout>
When the user press the menu button I use this code to show the view:
View settings = (View)findViewById(R.id.settings);
settings.setVisibility(View.VISIBLE);
This worked flawlessy on versions earlier than 2.3 but now the behaviour is very random.
Observations
Very rarely everything works.
Sometimes the view shows up after hiding / unhiding it.
Sometimes you can press the buttons on the view (even though it's invisible) this makes the view show up for the rest of the application lifetime.
The getVisibility method always report the correct value.
What I've tried
Rebuilding draw cache
Using BringToFront method
Post invalidate
Setting the initial value of the view to "visible", this makes everything work, but I really want it to be initially invisible!
If anyone wants to check the problem you can try it for yourself in my app (it's on market, search for "chainparticles").
Thanks!
Solved it by changing the views initial state to "gone". WTF?!