Get inflate ViewGroup of a layout with fragment - java

I'm actually programming an Android app that uses Google Maps API.
The layout activity_main.xml looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mapContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:mapType="normal"
map:uiCompass="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true" />
</LinearLayout>
And I load the map asynchronically in the fragment. When I try to inflate the activity_main view:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup mainView = (ViewGroup) inflater.inflate(R.layout.activity_main, null);
The app crashes and returns an error:
01-15 19:25:16.045: E/AndroidRuntime(21115): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
How can i solve it? I need this ViewGroup to set a Power Button KeyListener to the app (similar to what I did in another app without fragment):
mainView.setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_POWER)
{
//STUFF
}
}
});
Thank you for your help :D

namespace must be declared in the first view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/mapContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:mapType="normal"
map:uiCompass="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true" />
</LinearLayout>

Did you add this lines in your manifest file:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR_KEY" />
Take a look on this site
https://docs.google.com/document/pub?id=19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw

I've almost solved the problem. I've created a custom SupportMapFragment class and I'm loading the fragment in a FrameLayout dynamically.
The activity_main.xml looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/mapContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/mapPlaceholder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I can now inflate the activity_main through code:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup mainView = (ViewGroup) inflater.inflate(R.layout.activity_main, null);
After this, I've set the listener (100% working):
mainView.setFocusableInTouchMode(true);
mainView.setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
//STUFF
}
});
And I've added the view like this:
final WindowManager myWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
myWindowManager.addView(mainView, viewParams);
I'm not using the setContentView anywhere in onCreate so here's the second problem. I need to load the GoogleMap in the FrameLayout through FragmentTransaction.
FragmentTransaction needs setContentView to work. This code:
FrameLayout mapPlaceholder = (FrameLayout) mainView.findViewById(R.id.mapPlaceholder);
if(mapPlaceholder != null) {
mapFragment = GoogleMapFragment.newInstance();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(mapPlaceholder.getId(), mapFragment, GoogleMapFragment.class.getName());
ft.commit();
}
Returns this error when launching the application:
01-16 18:22:20.263: E/AndroidRuntime(7755): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070018 (com.example.app:id/mapPlaceholder) for fragment GoogleMapFragment{b666a7d #0 id=0x7f070018 com.example.app.GoogleMapFragment}
Does anyone know how to use the FragmentTransaction (or add a Fragment) without setContentView?

Related

TextView.setView() does not update text

From a fragment I start an activty, that opens a new fragment. Also I pass a string value from the first fragment (AllDishes) over the activity to the last fragment (DishDetailView) via intent.
The fragment (DishDetailView) is set as context to a layout (fragment_dishdetailview), but when I try to set the text that was passed, the view is not getting updated.
When I debug my app, the string parameter ("Test") seems to be passed and also the text of the TextView name seems to be set correctly, as you can see in the screenshot below, but somehow the UI (fragment_dishdetailview) is not getting updated, it´s just empty.
I´m not sure whether the update of the UI or the way of passing the value from one fragment to another is the problem. My aim is to open the fragment (DishDetailView) from fragment (AllDishes) and pass a string from the first to the second fragment. So actually I don´t need the activity but without I was not able to pass the String.
AllDishes (fragment)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Dish dish = ca.getItem(position);
Intent intent = new Intent(rootView.getContext(), DishDetailsViewActivity.class);
intent.putExtra("nameToPass" , dish.name);
startActivity(intent);
}
DishDetailView (fragment)
public class DishDetailView extends Fragment {
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
View rootView = inflater.inflate(R.layout.fragment_dishdetailview, container, false);
String tempHolder = getActivity().getIntent().getStringExtra("nameToPass");
TextView name = rootView.findViewById(R.id.textview_view_dishName);
name.setText(tempHolder);
rootView.invalidate();
return inflater.inflate(R.layout.fragment_dishdetailview, container, false);
}
}
DishDetailsViewActivity (activity)
public class DishDetailsViewActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dish_details_view);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
fragment_dishdetailview.xml (fragment layout)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="#color/screen_background"
tools:context=".fragments.DishDetailView">
<TextView
android:id="#+id/textview_view_dishName"
android:layout_width="300dp"
android:layout_height="27dp"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="20dp"
android:textColor="#color/headercolor"
android:textSize="#dimen/header_fontsize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
** content_dish_details_view.xml (activity layout)**
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph3" />
</androidx.constraintlayout.widget.ConstraintLayout>
nav-graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="#+id/nav_graph3"
app:startDestination="#id/fragment_dishdetailview">
<fragment
android:id="#+id/fragment_dishdetailview"
android:name="com.example.nutritracker.fragments.DishDetailView"
android:label="DishDetailView"
tools:layout="#layout/fragment_dishdetailview">
</fragment>
</navigation>
I think you should return your rootView on the onCreateView method of DishDetailView.

Android Fragment not replacing UI from Activity

I am currently building an android application that uses a navigation drawer. When the user selects an item from the drawer, a fragment is created that corresponds to the item selected. For example, if the user selects the contacts item from the navigation drawer, the UI should change from the dashboard, to the contacts layout.
I am successfully creating fragments, however the Fragment retains the calenderView from my dashboard.
Here is the dashboard layout:
Here is the Dashboard layout
Here is the layout once the user selects timer from the navigation drawer
enter image description here
The problem is that the calenderView remains when the fragment is inflated. Ideally it would just be the "Timer Frag" Button that is visible in this fragment.
Here is my mainActivity code:
//Fragments will be inflated through this method.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_patient) {
fragment = new Patient_Fragment();
// Handle the camera action
} else if (id == R.id.nav_timer) {
fragment = new Timer_Fragment();
} else if (id == R.id.nav_reminders) {
} else if (id == R.id.nav_contacts) {
} else if (id == R.id.nav_account) {
} else if (id == R.id.nav_about) {
}
if(fragment !=null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.dashboardArea, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Here is my timer Fragment:
public class Timer_Fragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_timer, null);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
Here is the fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/timerFrag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Timer Frag"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.181" />
This is the dashboard XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:id="#+id/dashboardArea"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.a517086.carefree.MainActivity"
tools:showIn="#layout/app_bar_main">
<CalendarView
android:id="#+id/calendarView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
The problem is you are replacing the R.id.dashboardArea (which is in your CalendarView fragment layout), but you need to replace the View in your MainActivity which will hold the fragment. I have called it content_container for this example.
if(fragment !=null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.content_container, fragment);
ft.commit();
}
Example:
I use a DrawerLayout, but you can use just any layout. This is my MainActivity Layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:id="#+id/content_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
When I want to swap out my fragments I just get the fragment object I want to be visible and replace the container--In this case "content_container".
You should put your CalendarView in another xml fragment related to the calendar fragment like you did with the timer fragment like this :
fragment_calendar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<CalendarView
android:id="#+id/calendarView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
CalendarFragment.java
public class CalendarFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_calendar, null);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
Your dashboard xml file should be empty like this :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:id="#+id/dashboardArea"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.a517086.carefree.MainActivity"
tools:showIn="#layout/app_bar_main">
</FrameLayout>
Then, in your main activity initialize the calendar fragment just like that :
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(R.id.dashboardArea, CalendarFragment);
ft.commit();

Create Switch and add it to Layout

I have a Fragment
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="#+id/switchLayout">
</LinearLayout>
</FrameLayout>
and want to add switches to the LinearLayout as soon as the Fragment is loaded
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment, container, false);
LinearLayout switchLayout = (LinearLayout) rootview.findViewById(R.id.switchLayout);
Switch new_switch = new Switch(getActivity());
new_switch.setText("test");
new_switch.setChecked(true);
switchLayout.addView(new_switch);
return rootview;
}
but when the Fragment gets loaded nothing happens, the LinearLayout is displayed but the switch doesn't appear and I don't get any error messages.
I already tried adding
new_switch.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
);
but it makes no difference
note: this is my first question here, hopefully it's not too stupid ^^
I've tried to perform the same what you are doing and it works.
Main activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
<FrameLayout
android:id="#+id/containerFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Then in MainActivity.java:
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.containerFragment,
new MyFragment()).commit();
MyFragment and fragment layout is the same as you described.
Result:

No view found for id 0x7f090058 for fragment

I'm doing an app that uses Google Maps APIs (with two separate maps), and I want to use a navigation drawer to switch between the two maps (fragments).
I created the main activity with the pre-compiled Navigation Drawer Activity in android studio, and here is the pieces of code:
The main activity uses this layout:
activity_main_activity2.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="it.radonmap.radonmap.MainActivity2">
<FrameLayout android:id="#+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start" android:name="it.radonmap.radonmap.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Here is the code which calls the .replace() method:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
//do something
break;
case 1:
fragment = new FragPunti();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager;
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.containerPunti,fragment).commit();
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
FragPunti.java
public class FragPunti extends android.support.v4.app.Fragment {
public FragPunti(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_punti, null);
return rootView;
}
}
frag_punti.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/containerPunti">
<FrameLayout
android:id="#+id/framePunti"
android:layout_width="0px"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Punti"/>
</FrameLayout>
</FrameLayout>
And here is the logcat trace:
04-30 19:17:45.823 20909-20909/it.radonmap.radonmap E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: it.radonmap.radonmap, PID: 20909
java.lang.IllegalArgumentException: No view found for id 0x7f090058 (it.radonmap.radonmap:id/containerPunti) for fragment FragPunti{36b6aaa5 #2 id=0x7f090058}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:945)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5274)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
I had already look for a solution but with no results.
Hope someone can help me.
You should pass id of container for fragment, and that's a view in which you want to show fragment. It is in activity, not in fragment you want to show. So change replace(R.id.containerPunti, fragment) to replace(R.id.container, fragment).

No view found for id 0x7f090005

I'm starting to build a simple Android app so when I press my video button it calls a fragment which contains the video player, but I'm always getting: No view found for id 0x7f090005...
I wonder if you give me a hand by figuring out what is wrong in the code that I'm writing.
Here is my MainActivity.java file:
public class MainActivity extends Activity {
private Button mVideoButton;
Fragment fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mVideoButton = (Button)findViewById(R.id.videoButton);
// Video Button
mVideoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentVideo sf = new FragmentVideo();
ft.add(R.id.fragmentVideo, sf);
ft.commit();
}
});
}
}
Then I have my main_activity.xml:
<?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:id="#+id/activityStart" >
<ImageView
android:src="#drawable/armstrong_on_moon"
android:contentDescription="#string/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:layout_weight="1" />
<TableRow
android:gravity="center|bottom"
android:layout_weight="0">
<Button
android:id="#+id/videoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/video" />
</TableRow>
Next, my FragmentVideo.java class:
public class FragmentVideo extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState ){
View v = inflater.inflate(R.layout.fragment_video, parent, false);
return v;
}
}
And at last, the fragment_video.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/fragmentVideo" >
<VideoView
android:id="#+id/video"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
<ProgressBar
android:id="#+id/prog"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center" />
</LinearLayout>
Please let me know if I'm doing something wrong or if I'm missing something as I can see where the issue lays.
Thanks very much for any help in advance.
EGMWEB
On ft.add() the int parameter refers to the container to which you want to add the Fragment; you are trying to add the Fragment to the Fragment's own layout (Android doesn't find the View because it is not inflated yet, if it did it will throw and Exception in this case; you cannot add a Fragment to its own layout). You want to replace R.id.fragmentVideo with R.id.activityStart to add the Fragment to the current TableLayout that is part of the already inflated main_activity layout
Could you check whats the equivalent view for the id '0x7f090005' in the R.java file. It should be under gen folder of your project. The framework is not able to find this under the resource file that you have in this java class.

Categories