Cannot use addOnTabSelectedListener in fragment [duplicate] - java

Can anyone help me to find out what can be the issue with this program.
In the onCreate() method the findViewById() returns null for all ids and this causes a null pointer exception later. I can not figure out why the findViewById() can not find the view. Any suggestions?
This is the main code:
public class MainActivity extends Activity {
ViewPager pager;
MyPagerAdapter adapter;
LinearLayout layout1, layout2, layout3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout1 = (LinearLayout) findViewById(R.id.first_View);
layout2 = (LinearLayout) findViewById(R.id.second_View);
layout3 = (LinearLayout) findViewById(R.id.third_View);
adapter = new MyPagerAdapter();
pager = (ViewPager) findViewById(R.id.main_pager);
pager.setAdapter(adapter);
}
private class MyPagerAdapter extends PagerAdapter
{
#Override
public int getCount() {
return 3;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
LinearLayout l = null;
if (position == 0 )
{
l = layout1;
}
if (position == 1)
{
l = layout2;
}
if (position == 2)
{
l = layout3;
}
collection.addView(l, position);
return l;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==object);
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
}
}
And the related XML files:
activity_main 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"
android:background="#a4c639">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_pager"/>
</LinearLayout>
activity_first layout:
<?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:id="#+id/first_View">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
activity_second layout:
<?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:id="#+id/second_View">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
And the activity_third layout:
<?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:id="#+id/third_View">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>

findViewById() returns a View if it exists in the layout you provided in setContentView(), otherwise it returns null and that's what happening to you. Note that if you don't setContentView(), and don't have a valid view to findViewById() on, findViewById() will always return null until you call setContentView().
This also means variables in the top-level trigger an NPE, because they're called before onCreate(), and by extension, before setContentView(). See also the activity lifecycle
Example if you setContentView(R.layout.activity_first); and then call findViewById(R.id.first_View); it will return a View which is your layout.
But if you call findViewById(R.id.second_View); before setContentView(), it will return null since there is not a view in your activity_first.xml layout called #+id/second_View.

Emphasis added
For those cases within an Activity class.
Activity.findViewById(int id)
Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
Otherwise, such as an Fragment, Adapter, a View from a LayoutInflater, etc.
View.findViewById(int id)
Look for a child view with the given id. If this view has the given id, return this view.
Either case,
Returns
The view if found or null otherwise.
Now, re-check your XML files. Make sure you put the right value into setContentView or inflater.inflate.
In the case of an Activity, call findViewById after setContentView.
Then, make sure there is a View you are looking for with android:id="#+id/..." in that layout. Make sure the + is at #+id, which will add the resource to the R.id values to ensure you can find it from Java.

The views you're trying to get are not defined in your activity_main layout. You need to programmatically inflate the views you're trying to add to the pager.-
#Override
public Object instantiateItem(ViewGroup collection, int position) {
LinearLayout l = null;
if (position == 0) {
l = (LinearLayout) View.inflate(this, R.layout.activity_first, null);
}
if (position == 1) {
l = (LinearLayout) View.inflate(this, R.layout.activity_second, null);
}
if (position == 2) {
l = (LinearLayout) View.inflate(this, R.layout.activity_third, null);
}
collection.addView(l, position);
return l;
}

Sometimes you need clean your project in Eclipse (Project - Clean..).

add those views to the pager adapter before accessing them.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MyPagerAdapter();
pager = (ViewPager) findViewById(R.id.main_pager);
pager.setAdapter(adapter);
layout1 = (LinearLayout) findViewById(R.id.first_View);
layout2 = (LinearLayout) findViewById(R.id.second_View);
layout3 = (LinearLayout) findViewById(R.id.third_View);
}
in the pager adapter:
public Object instantiateItem(View collection, int position) {
if(position == 0){
View layout = inflater.inflate(R.layout.activity_first, null);
((ViewPager) collection).addView(layout);
return layout;
}
... and so forth.
}
from here you can access them via findViewById.

In my case, it was a stupid mistake on my part. I had written code in the OnCreate method but it was above the setContentView line of code. Once I moved my code below this line the application started working fine.
setContentView(R.layout.activity_main);

What #Warlock said above is right , you should initial LinearLayout layout1, layout2, layout3 by the right way:
LinearLayout layout1 = (LinearLayout) View.inflate(this, R.layout.first_View, null);
LinearLayout layout2 = (LinearLayout) View.inflate(this, R.layout.second_View, null);
LinearLayout layout3 = (LinearLayout) View.inflate(this, R.layout.third, null);
wish my advise help you

I have gotten this error today and it was so simple to clear, that I "facepalmed".
Just try to add the UI element to your layout xml File in your res/layout-port directory!!!

In Android, findViewById(R.id.some_id) works when you are finding view in the layout set.
That is, if you have set a layout say:
setContentView(R.layout.my_layout);
Views can be found only in this layout (my_layout).
In your code layout1, layout2, layout3 all are three different layouts and they are not set to the activity.

The findViewById method must find what is in the layout (which you called in the setContentView)

Use the adaptor, to inflate the layout, and based on the position you can search for the view.
override fun instantiateItem(collection: ViewGroup, position: Int) : ViewGroup {
val inflater = LayoutInflater.from(mContext)
val layout = inflater.inflate(mData[position], collection, false) as ViewGroup
if(position == your_position) {
val nameOfField: TextView = layout.findViewById(R.id.item_id)
}

This problem is also generated when you are having same name of many components in XML file. Even if they are different layout files you should give every element a unique name.
This error occur because you are having a XML element with the same name in another layout file and android studio is trying to access that and showing this error

Related

App crashes without any error on a click of button [duplicate]

Can anyone help me to find out what can be the issue with this program.
In the onCreate() method the findViewById() returns null for all ids and this causes a null pointer exception later. I can not figure out why the findViewById() can not find the view. Any suggestions?
This is the main code:
public class MainActivity extends Activity {
ViewPager pager;
MyPagerAdapter adapter;
LinearLayout layout1, layout2, layout3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout1 = (LinearLayout) findViewById(R.id.first_View);
layout2 = (LinearLayout) findViewById(R.id.second_View);
layout3 = (LinearLayout) findViewById(R.id.third_View);
adapter = new MyPagerAdapter();
pager = (ViewPager) findViewById(R.id.main_pager);
pager.setAdapter(adapter);
}
private class MyPagerAdapter extends PagerAdapter
{
#Override
public int getCount() {
return 3;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
LinearLayout l = null;
if (position == 0 )
{
l = layout1;
}
if (position == 1)
{
l = layout2;
}
if (position == 2)
{
l = layout3;
}
collection.addView(l, position);
return l;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==object);
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
}
}
And the related XML files:
activity_main 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"
android:background="#a4c639">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_pager"/>
</LinearLayout>
activity_first layout:
<?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:id="#+id/first_View">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
activity_second layout:
<?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:id="#+id/second_View">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
And the activity_third layout:
<?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:id="#+id/third_View">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
findViewById() returns a View if it exists in the layout you provided in setContentView(), otherwise it returns null and that's what happening to you. Note that if you don't setContentView(), and don't have a valid view to findViewById() on, findViewById() will always return null until you call setContentView().
This also means variables in the top-level trigger an NPE, because they're called before onCreate(), and by extension, before setContentView(). See also the activity lifecycle
Example if you setContentView(R.layout.activity_first); and then call findViewById(R.id.first_View); it will return a View which is your layout.
But if you call findViewById(R.id.second_View); before setContentView(), it will return null since there is not a view in your activity_first.xml layout called #+id/second_View.
Emphasis added
For those cases within an Activity class.
Activity.findViewById(int id)
Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
Otherwise, such as an Fragment, Adapter, a View from a LayoutInflater, etc.
View.findViewById(int id)
Look for a child view with the given id. If this view has the given id, return this view.
Either case,
Returns
The view if found or null otherwise.
Now, re-check your XML files. Make sure you put the right value into setContentView or inflater.inflate.
In the case of an Activity, call findViewById after setContentView.
Then, make sure there is a View you are looking for with android:id="#+id/..." in that layout. Make sure the + is at #+id, which will add the resource to the R.id values to ensure you can find it from Java.
The views you're trying to get are not defined in your activity_main layout. You need to programmatically inflate the views you're trying to add to the pager.-
#Override
public Object instantiateItem(ViewGroup collection, int position) {
LinearLayout l = null;
if (position == 0) {
l = (LinearLayout) View.inflate(this, R.layout.activity_first, null);
}
if (position == 1) {
l = (LinearLayout) View.inflate(this, R.layout.activity_second, null);
}
if (position == 2) {
l = (LinearLayout) View.inflate(this, R.layout.activity_third, null);
}
collection.addView(l, position);
return l;
}
Sometimes you need clean your project in Eclipse (Project - Clean..).
add those views to the pager adapter before accessing them.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MyPagerAdapter();
pager = (ViewPager) findViewById(R.id.main_pager);
pager.setAdapter(adapter);
layout1 = (LinearLayout) findViewById(R.id.first_View);
layout2 = (LinearLayout) findViewById(R.id.second_View);
layout3 = (LinearLayout) findViewById(R.id.third_View);
}
in the pager adapter:
public Object instantiateItem(View collection, int position) {
if(position == 0){
View layout = inflater.inflate(R.layout.activity_first, null);
((ViewPager) collection).addView(layout);
return layout;
}
... and so forth.
}
from here you can access them via findViewById.
In my case, it was a stupid mistake on my part. I had written code in the OnCreate method but it was above the setContentView line of code. Once I moved my code below this line the application started working fine.
setContentView(R.layout.activity_main);
What #Warlock said above is right , you should initial LinearLayout layout1, layout2, layout3 by the right way:
LinearLayout layout1 = (LinearLayout) View.inflate(this, R.layout.first_View, null);
LinearLayout layout2 = (LinearLayout) View.inflate(this, R.layout.second_View, null);
LinearLayout layout3 = (LinearLayout) View.inflate(this, R.layout.third, null);
wish my advise help you
I have gotten this error today and it was so simple to clear, that I "facepalmed".
Just try to add the UI element to your layout xml File in your res/layout-port directory!!!
In Android, findViewById(R.id.some_id) works when you are finding view in the layout set.
That is, if you have set a layout say:
setContentView(R.layout.my_layout);
Views can be found only in this layout (my_layout).
In your code layout1, layout2, layout3 all are three different layouts and they are not set to the activity.
The findViewById method must find what is in the layout (which you called in the setContentView)
Use the adaptor, to inflate the layout, and based on the position you can search for the view.
override fun instantiateItem(collection: ViewGroup, position: Int) : ViewGroup {
val inflater = LayoutInflater.from(mContext)
val layout = inflater.inflate(mData[position], collection, false) as ViewGroup
if(position == your_position) {
val nameOfField: TextView = layout.findViewById(R.id.item_id)
}
This problem is also generated when you are having same name of many components in XML file. Even if they are different layout files you should give every element a unique name.
This error occur because you are having a XML element with the same name in another layout file and android studio is trying to access that and showing this error

Getting imageview inside ListView Adapter [duplicate]

Can anyone help me to find out what can be the issue with this program.
In the onCreate() method the findViewById() returns null for all ids and this causes a null pointer exception later. I can not figure out why the findViewById() can not find the view. Any suggestions?
This is the main code:
public class MainActivity extends Activity {
ViewPager pager;
MyPagerAdapter adapter;
LinearLayout layout1, layout2, layout3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout1 = (LinearLayout) findViewById(R.id.first_View);
layout2 = (LinearLayout) findViewById(R.id.second_View);
layout3 = (LinearLayout) findViewById(R.id.third_View);
adapter = new MyPagerAdapter();
pager = (ViewPager) findViewById(R.id.main_pager);
pager.setAdapter(adapter);
}
private class MyPagerAdapter extends PagerAdapter
{
#Override
public int getCount() {
return 3;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
LinearLayout l = null;
if (position == 0 )
{
l = layout1;
}
if (position == 1)
{
l = layout2;
}
if (position == 2)
{
l = layout3;
}
collection.addView(l, position);
return l;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==object);
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
}
}
And the related XML files:
activity_main 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"
android:background="#a4c639">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_pager"/>
</LinearLayout>
activity_first layout:
<?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:id="#+id/first_View">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
activity_second layout:
<?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:id="#+id/second_View">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
And the activity_third layout:
<?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:id="#+id/third_View">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
findViewById() returns a View if it exists in the layout you provided in setContentView(), otherwise it returns null and that's what happening to you. Note that if you don't setContentView(), and don't have a valid view to findViewById() on, findViewById() will always return null until you call setContentView().
This also means variables in the top-level trigger an NPE, because they're called before onCreate(), and by extension, before setContentView(). See also the activity lifecycle
Example if you setContentView(R.layout.activity_first); and then call findViewById(R.id.first_View); it will return a View which is your layout.
But if you call findViewById(R.id.second_View); before setContentView(), it will return null since there is not a view in your activity_first.xml layout called #+id/second_View.
Emphasis added
For those cases within an Activity class.
Activity.findViewById(int id)
Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
Otherwise, such as an Fragment, Adapter, a View from a LayoutInflater, etc.
View.findViewById(int id)
Look for a child view with the given id. If this view has the given id, return this view.
Either case,
Returns
The view if found or null otherwise.
Now, re-check your XML files. Make sure you put the right value into setContentView or inflater.inflate.
In the case of an Activity, call findViewById after setContentView.
Then, make sure there is a View you are looking for with android:id="#+id/..." in that layout. Make sure the + is at #+id, which will add the resource to the R.id values to ensure you can find it from Java.
The views you're trying to get are not defined in your activity_main layout. You need to programmatically inflate the views you're trying to add to the pager.-
#Override
public Object instantiateItem(ViewGroup collection, int position) {
LinearLayout l = null;
if (position == 0) {
l = (LinearLayout) View.inflate(this, R.layout.activity_first, null);
}
if (position == 1) {
l = (LinearLayout) View.inflate(this, R.layout.activity_second, null);
}
if (position == 2) {
l = (LinearLayout) View.inflate(this, R.layout.activity_third, null);
}
collection.addView(l, position);
return l;
}
Sometimes you need clean your project in Eclipse (Project - Clean..).
add those views to the pager adapter before accessing them.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MyPagerAdapter();
pager = (ViewPager) findViewById(R.id.main_pager);
pager.setAdapter(adapter);
layout1 = (LinearLayout) findViewById(R.id.first_View);
layout2 = (LinearLayout) findViewById(R.id.second_View);
layout3 = (LinearLayout) findViewById(R.id.third_View);
}
in the pager adapter:
public Object instantiateItem(View collection, int position) {
if(position == 0){
View layout = inflater.inflate(R.layout.activity_first, null);
((ViewPager) collection).addView(layout);
return layout;
}
... and so forth.
}
from here you can access them via findViewById.
In my case, it was a stupid mistake on my part. I had written code in the OnCreate method but it was above the setContentView line of code. Once I moved my code below this line the application started working fine.
setContentView(R.layout.activity_main);
What #Warlock said above is right , you should initial LinearLayout layout1, layout2, layout3 by the right way:
LinearLayout layout1 = (LinearLayout) View.inflate(this, R.layout.first_View, null);
LinearLayout layout2 = (LinearLayout) View.inflate(this, R.layout.second_View, null);
LinearLayout layout3 = (LinearLayout) View.inflate(this, R.layout.third, null);
wish my advise help you
I have gotten this error today and it was so simple to clear, that I "facepalmed".
Just try to add the UI element to your layout xml File in your res/layout-port directory!!!
In Android, findViewById(R.id.some_id) works when you are finding view in the layout set.
That is, if you have set a layout say:
setContentView(R.layout.my_layout);
Views can be found only in this layout (my_layout).
In your code layout1, layout2, layout3 all are three different layouts and they are not set to the activity.
The findViewById method must find what is in the layout (which you called in the setContentView)
Use the adaptor, to inflate the layout, and based on the position you can search for the view.
override fun instantiateItem(collection: ViewGroup, position: Int) : ViewGroup {
val inflater = LayoutInflater.from(mContext)
val layout = inflater.inflate(mData[position], collection, false) as ViewGroup
if(position == your_position) {
val nameOfField: TextView = layout.findViewById(R.id.item_id)
}
This problem is also generated when you are having same name of many components in XML file. Even if they are different layout files you should give every element a unique name.
This error occur because you are having a XML element with the same name in another layout file and android studio is trying to access that and showing this error

Change view on button click from CustomAdapter

I have created a layout that contains a listView, the view is populated with multiple similar elements in the onCreateView function from a Fragment, the view is populated in the CustomAdapter extends BaseAdapter. In each element of the listView I have a button for which I want to implement a onClickListener. When the button is clicked I want to switch to another view, something similar to viewing comments on the GooglePlus mobile app.
I have tried doing so this way in the CustomAdapter:
public void setOnClickComment(View view) {
final ImageButton btn = (ImageButton)view.findViewById(R.id.addComment);
if (btn != null) {
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONCLICK", "I just clicked this shit" + btn.getTag());
manager.beginTransaction()
.replace(R.id.feedLayout, new CommentFragment()).commit();
}
});
} else {
Log.d("CLICK", "ESTE NULL");
}
}
The manager is a FragmentManager and it's passed from the fragment to the CustomAdapter constructor. I can see the Log in LogCat but that's it nothing else happens, the tag of the button is the position of the element.
This is the layout:
<?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="fill_parent"
android:orientation="vertical"
android:id="#+id/feedLayout"
>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
And in the fragment I inflate the above layout:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout linear = (LinearLayout) inflater.inflate(R.layout.swipe_screen_left, container, false);
view = (ListView) linear.getChildAt(0);
adapter = new CustomAdapter(getActivity(), data, getFragmentManager());
view.setAdapter(adapter);
return linear;
}

Displaying ArrayAdapter in fragment displays "false"

Recently I asked a question on how to have one style for a TextView and display it multiple times for other texts as well. The solution was, to create an XML-Layout where I design the textview and then use an ArrayAdapter to fill it with contents. I'm using the ArrayAdapter in a fragment because I have multiple fragments that replace the main fragment dependent on the menu click.
But I'm stuck. I don't really know how to achieve that. I'm writing all my values into an array and then I'm assigning them to my ArrayAdapter. I have seen plenty solutions, but none of them fixed my problem and they all used an other way.
This is the xml of the fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.03" >
<TextView
android:id="#+id/sources"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px" >
</TextView>
</ScrollView>
</RelativeLayout>
The TextView is the textview I want to populate. There is no style yet, I'm doing it just for test purposes. Now I did this in the Fragment.
This is my onCreateView method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
StrictMode.setThreadPolicy(policy);
View rootView = inflater.inflate(R.layout.main_fragment, null);
RelativeLayout ll = new RelativeLayout(getActivity());
ScrollView sv = new ScrollView(getActivity());
ArrayAdapter<String> sourceAdapter = new ArrayAdapter<String>(getActivity(), R.id.mainScrollView, R.id.sources, sourceArr);
return rootView;
}
sourceArr is the array with the contents. How exactly do I assign all values to the TextView so it gets displayed multiple times? Thanks
You are having textview in scrollview. Either loop through the array and append the data to textview.
Or
Just Have a listview in main_fragment.xml initialize listview and adapter and set the adapter to listview.
View rootView = inflater.inflate(R.layout.main_fragment, null);
ListView lv = (ListView) rootView.findViewById(R.id.lv);
ArrayAdapter<String> sourceAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item1, sourceArr);
lv.setAdapter(sourceAdapter);
return rootView;
Also remove ScrollView sv = new ScrollView(getActivity()) and RelativeLayout ll = new RelativeLayout(getActivity()).

Add layouts to a fragment programmaticcaly

Introduction
I want to have different fragments depending on the user's choice from the navigation drawer. For every item in the navigation drawer is a different site that should called. I want to achieve this by fragments.
The main fragment will get displayed first by doing the following in the onCreate-method
if ( savedInstanceState == null ) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment fragment = MainFragment.passList(hashMap);
ft.replace(R.id.content_frame, new MainFragment());
ft.commit();
}
As you can see I'm passing data (a hashmap) to a method of the fragment which works perfectly. The data then gets put into string arrays. This works and the data can also be used in the onCreate-method of the fragment.
This is the xml of the main activity
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/darker_gray"
android:dividerHeight="0.1dp" />
</android.support.v4.widget.DrawerLayout>
And this is the XML of the main fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:gravity="center_vertical"
android:paddingRight="10dp"
>
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.03" >
<LinearLayout
android:id="#+id/mainListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
What I want to achieve now is to display data in the scroll view. But the question I have, is how do I populate that and how do I initliaze it?
Normally I would go and just create them like that - because findViewById doesn't work in a Fragment (Have to write the method by my own)
ScrollView sv = new ScrollView(getActivity());
LinearLayout ll = new LinearLayout ( getActivity() );
sv.addView(ll);
ll.addView(SAMPLETEXTVIEW);
View mainView = inflater
.inflate(R.layout.main_fragment, container, false);
return mainView;
but that just leaves me with a blank screen and no actual text added. How can I achieve this? The data is there - I know that, because it gets printed out with System.out.println - I just need a way to display it. Thx in advance!
EDIT
When I do it like that
View rootView = inflater.inflate(R.layout.main_fragment, container, false);
ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);
sv.addView(ll);
I get just an IllegalStateException that ScrollView can only host one direct child... But I don't get it. The linearlayout is the only thing that is the direct child. Why is there a problem?
Here is the complete onCreateView-method
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_fragment, container, false);
ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);
sv.addView(ll);
source = new TextView[hashMapSize];
for (int i = 0; i < hashMapSize; i++) {
source[i] = new TextView(getActivity());
source [i].setText( "Source: " + sourceArr [i] );
ll.addView(source[i]
}
return rootView;
}
The reason you get the IllegalStateException is because you inflate your R.layout.main_fragment which already contains your ScrollView that already has one direct child (declared in you layout xml).
You shouldn't add in code with sv.addView(ll) it's already there. All you need to do is get the references as you already do with findViewById.
Remove the sv.addView(ll); and you should be fine.

Categories

Resources