Reputation: 1637
I have an XML
file where I have a FrameLayout
and inside of it I have another FrameLayout
.
I my class, I want to show the screen and hide the FrameLayout
that is inside the other one, and when the user clicks in a button the FrameLayout
was hidden appears, but I am getting an error.
Is that possible to find this Framelayout
inside the other one through findViewById
?
My code :
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_gravity="center_horizontal"
android:textColor = "@color/white"
android:text="."
android:textSize="70sp" />
<ImageView
android:id="@+id/imageViewwaves_startScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="@drawable/waves" />
<FrameLayout
android:id="@+id/teamInfoLayout"
android:layout_width="275dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="160dp"
android:background="@color/white"
>
<TextView
android:id="@+id/textViewMyTeams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="80dp"
android:textSize="20sp"
android:text="My Teams:"
/>
<TextView
android:id="@+id/textViewClickOnTeamName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:textSize="13sp"
android:text="(Click on a team name to race for that team)"
/>
</FrameLayout>
</FrameLayout>
Class:
ImageView waves;
View layout;
LayoutParams layoutParams, layoutParamsTextView;
FrameLayout layoutteamInfo;
private static float dx =0, dy=0, x= 0, y=0;
public ShowInfo(Context context) {
super(context);
waves = (ImageView) findViewById(R.id.imageViewwaves_startScreen);
layoutteamInfo = new FrameLayout(getContext());
layoutteamInfo = (FrameLayout) findViewById(R.id.teamInfoLayout);
}
public void hideImages()
{
layoutteamInfo.setVisibility(View.INVISIBLE);
waves.setVisibility(View.INVISIBLE); //Waves are gone
}
Update because of comments:
So, my logcats shows:
06-06 16:42:33.800: E/AndroidRuntime(2073): Caused by: java.lang.NullPointerException 06-06 16:42:33.800: E/AndroidRuntime(2073): at com.qwantech.workshape.ShowInfo.hideImages(ShowInfo.java:33) 06-06 16:42:33.800: E/AndroidRuntime(2073): at com.qwantech.workshape.Start.onCreate(Start.java:47)
and I created another class "ShowInfo extends View" to deal with the images and Layout.
These codes are there.
In my class "Starts", I created an object:
ShowInfo showObj = new ShowInfo(getContext());
and in my onCreate
of Start
I call:
showObj.hideImages();
I am getting the error in the moment I calling this function.
Upvotes: 2
Views: 5438
Reputation: 962
Since you are working from another class, you need to pass the root element of your view to be able to use findViewById()
. Otherwise, your class is out of context and cannot access the view element.
mRootView.findViewById(R.id.teamInfoLayout)
should work.
Upvotes: 1
Reputation: 4897
You are doing this:
waves = (ImageView) findViewById(R.id.imageViewwaves_startScreen);
But there is no ImageView in your XML that has this id: imageViewwaves_startScreen. You have to create an ImageView inside Xml with that id
Try giving an id to your first FrameLayout.
If that doesnt work than try to copy this class that you have done for dealing with layout, to your Activity class, if this will work it means that raybaybay answer is the solution
Upvotes: 1
Reputation: 647
What you can do, is to inflate your XML layout file and find your frame using the layout.findViewById(...)
Example:
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.yourfilename, null);
FrameLayout layoutteamInfo = (FrameLayout) layout.findViewById(R.id.teamInfoLayout);
This will get the second framelayout in your xml file
Upvotes: 1