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.
Related
I have looked for several times, but I think no one is facing the same issue that I'm facing right now. I have the following fragment:
FRAGMENT XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="8dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</RelativeLayout>
RecyclerView XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
/>
</LinearLayout>
FRAGMENT JAVA CODE SNIPPET
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.fragment_xml, container, false);
recyclerView = (RecyclerView) viewRoot.findViewById(R.id.recyclerview_id);
recyclerView.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
#Override
public void onSwipeRight() {
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
ft.replace(R.id.frameLayout, new Fragment2()).commit();
}
});
So, what I'm doing on the code above is adding a touch listener, so when the user swipe from right to left, the fragment goes to the other fragment (Fragment2)
This works perfectly fine! However... I need to add onClickListener inside my recyclerAdapter, so I can catch the click listener of each item. And here comes the problem... When adding a click listener, the swipe listener does not work. It seems that the listener of the click "overrides" my listener of the swipe. The question is. How to add both on the recyclerview, and make them work together?
I try to make a button active fragment, leading to a new layout with the url of this fragment. But it is not working. Something wrong. Please help
no receive error, but the button does not work.
code fragment
public class ContentFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.content_fragment, container, false);
final View button = view.findViewById(R.id.bSendUrl);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText simpleEditText = (EditText) getView().findViewById(R.id.bTextUrl);
String strValue = simpleEditText.getText().toString();
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("url", strValue);
startActivity(intent);
}
}
);
return view;
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_TEXT_Url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_below="#+id/include"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_marginTop="6dp">
<EditText
android:id="#+id/bTextUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri"
android:hint="#string/url_edit_text" />
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="#dimen/latimea_button_send_tv"
android:layout_height="#dimen/inaltimea_button_send_tv"
android:text="#string/button_send_android_tv"
android:id="#+id/bSendUrl"
style="?android:attr/borderlessButtonStyle"
android:layout_below="#+id/input_TEXT_Url"
android:paddingLeft="16dp"
android:layout_marginLeft="16dp" />
</RelativeLayout>
Edit
I am removed openjdk and installed clean javasdk and is work fine
You are trying to find a reference to a Button before the fragment's view hierarchy is inflated. The fragment transaction you're using does not immediately create the view hierarchy after commit() is called. This means the view with id bTextUrl is not yet created and findViewById() returns null.
Also, it's kind of strange for an activity to be messing with the views of child fragments. I recommend that you modify the views of the fragment within the fragment itself (not within the containing activity like you're doing now).
What I want to do is that when I click a button ( BtnA or BtnB) , the fragment just below buttons is replaced by corresponding fragment (FragmentA or FragmentB).
this is my code in MainActivity.java
#Override
public void onClick(View v) {
Fragment replacableFragment = new Fragment();
switch (v.getId()){
case R.id.button1:
{
replacableFragment = new Fragment_One();
Log.d("OnClick","button1");
break;
}
case R.id.button2:
{
replacableFragment = new Fragment_Two();
Log.d("OnClick","button2");
break;
}
}
FragmentManager manager= getFragmentManager();
Log.d("OnClick","1");
FragmentTransaction transaction = manager.beginTransaction();
Log.d("OnClick","2");
transaction.add(R.id.fragmentPlace, replacableFragment);
//transaction.addToBackStack(null);
Log.d("OnClick","3");
transaction.commit();
}
and this is my code of Fragment_One.java
public class Fragment_One extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one,container,false);
}
}
Fragment_Two.java is the same with 'inflater.inflater(R.layout.fragment_two,container,false);'
When I click any button, logcat shows everything exactly. But Fragment is not replaced at all.
I think my code has no problem to function it.
Can anyone know about this?
xml files for my project are below:
activity_main.xml
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FRAGMENT ONE"
android:id="#+id/button1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FRAGMENT TWO"
android:id="#+id/button2"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragmentPlace"
android:name="lkm.com.practical5_3.Fragment_One"/>
fragment_one.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:background="#android:color/holo_orange_dark"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="THIS IS FRAGMENT ONE"
android:id="#+id/text1"/>
</LinearLayout>
fragment_two.xml
The same one as 'fragment_one.xml'
use this
transaction.replace(R.id.fragmentPlace, replacableFragment).commit();
I want to anser my question!
The problem was the container!
At activiy_main.xml, the container of the fragment ais defined as 'fragment ~/'.
I fix it as 'FrameLayout ~/' and it was succesfful!
Thank you!
I have a Android fragment containing a ListView, and I have attached an ArrayAdapter to the ListView, but when I insert items into the ArrayAdapter, the ListView only shows the first item.
onCreateView iterates over my Technologies, if they're unlocked, it adds them to the ArrayList shownTechs, but only the first item ever appears
public class TabTechnologyList extends Fragment {
private static final String TAB_NAME = "tab_name";
private ListView techList;
private ArrayList<Technology> shownTechs = new ArrayList<>();
ArrayAdapter<Technology> techListAdapter;
public TabTechnologyList() { }
public static TabTechnologyList newInstance() {
TabTechnologyList fragment = new TabTechnologyList();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabData = Globals.getTabData();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_technology_list, container, false);
techList = (ListView) view.findViewById(R.id.tabTechListList);
techListAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.layout_techlist_element, shownTechs);
techList.setAdapter(techListAdapter);
for(Technology tech: tabData.getTechnologies()) {
shownTechs.add(tech);
}
//Only the first one is shown on the screen
return view;
}
}
}
When I google for similar issues, people seem to be recreating the ArrayAdapter instead of editing the existing one, but I'm not doing that, and cant' figure out what problem remains. Any suggestions?
fragment_technology_list.xml:
<RelativeLayout 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"
tools:context="com.tbohne.kittens.front.TabTechnologyList"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabTechListList" />
</RelativeLayout>
layout_techlist_element.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
Addition
I have just realized that this fragment is not filling the parent height, and is only tall enough to show the one entry, as if the fragment height were wrap_content. (I can't scroll it, so it appears as if there's only the one). This makes me think the nesting of fragments may be related, so here's the code that creates the fragment:
public class TabTechnology extends Fragment {
private TabTechnologyList table;
private OnFragmentInteractionListener mListener;
public TabTechnology() {}
public static TabTechnology newInstance(String tabName) {[SNIP]}
#Override public static onAttach(Activity activity) {[SNIP]}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_technology, container, false);
table = TabTechnologyList.newInstance(tabName);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.view_table, table);
transaction.commit();
return view;
}
}
fragment_technology.xml
<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"
tools:context="com.tbohne.kittens.front.TabTechnology"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/view_table"
android:layout_above="#+id/view_details" />
<View android:layout_width="match_parent"
android:layout_height="1px"
android:background="?android:attr/listDivider" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/view_details"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true" />
</LinearLayout>
Putting the Fragment inside the ScrollView was the source of the problem. Simply replaced those with FrameLayout, and suddenly everything magically works. Very strange.
I suspect the problem has something to do with nesting scrollable objects.
I want to change fragments depending on button click. (I have made two fragment classes called fragmentLeft and fragmentMiddle)
This is my MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
Fragment fr;
public void selectFrag(View view) {
if(view == findViewById(R.id.bigbutton_left)) {
fr = new fragmentLeft();
}else {
fr = new fragmentMiddle();
}
FragmentManager fm = getFragmentManager();
if (fm != null) {
// Perform the FragmentTransaction to load in the list tab content.
// Using FragmentTransaction#replace will destroy any Fragments
// currently inside R.id.fragment_content and add the new Fragment
// in its place.
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, fr);
ft.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
This is fragmentLeft.java
public class fragmentLeft extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragmentleft, container, false);
}
}
the same is for fragmenttwo with the exception of the layout id passed to infater.inflate()
I have made two layout files two, with the correct names as well .
This is the definition of my fragment from my activity_main.xml
But I am unable to get the buttons to do anything. No transactions take place, and I am stuck with the first fragment.
After a bit of googling , I came to know that its not recommended that you use fragment in the layout, but some other type of layout, but I saw this example code http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/
that seems to work just fine.
what is the problem ?
::EDIT::
There is something really weird going on.
If I use this layout xml file for main Activity:
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fragment No.1"
android:onClick="selectFrag" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<fragment
android:name="com.mainpackage.FragmentOne"
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
The fragments change and behave as they are expected to.
But if I use this layout:
<RelativeLayout 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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBaseline="#+id/bigbutton_middle"
android:layout_alignBottom="#+id/bigbutton_middle"
android:layout_marginLeft="7dp"
android:layout_toRightOf="#+id/bigbutton_middle"
android:background="#drawable/mainbutton_right" />
<Button
android:id="#+id/bigbutton_left"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBaseline="#+id/bigbutton_middle"
android:layout_alignBottom="#+id/bigbutton_middle"
android:layout_marginRight="7dp"
android:layout_toLeftOf="#+id/bigbutton_middle"
android:background="#drawable/mainbutton_left" />
<Button
android:id="#+id/bigbutton_middle"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="187dp"
android:background="#drawable/mainbutton_middle" />
<fragment
android:id="#+id/fragment_place"
android:name="com.mainpackage.FragmentOne"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="#+id/bigbutton_right" />
</RelativeLayout>
The fragments dont seem to change,i.e. the buttons seem to be dead.
What could be the problem ?
I am using Eclipse Juno
You are not getting a reference of the Buttons you are clicking in order for the FragmentTransaction to take place. You need to set OnClickListeners on the Buttons so they can execute relevant code.
One reason why the second layout doesn't display is because you are referencing an inexistant id:
<fragment
android:id="#+id/fragment_place"
android:name="com.mainpackage.FragmentOne"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="#+id/bigbutton_right" />
I do not see a View with an id of bigButton_right on that xml.
Another reason why the first xml works and the second doesn't is because you are not setting an onClick: property to any Button on the second xml.