I have a TextView[] object and a String[] field in a Fragment. I have 5 Fragments in total, all handled by a ViewPager. I want to retrieve pretty much all of the Strings displayed in each Fragment (and save it to a text file later) from the click of the parent activity's menu.
Anyway, here is some code:
This is within the main Activity (only working on retrieving data from first fragment):
private Intent createShareIntent() {
DisplayFragment displayFragment = (DisplayFragment) mAdapter.getItem(2);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide what to do
// with it.
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Display Info");
shareIntent.putExtra(Intent.EXTRA_TEXT, displayFragment.retrieveContent());
return shareIntent;
}
The NullPointerException comes from this method within DisplayFragment:
public class DisplayFragment extends Fragment {
static String devDensity, devDensityCategory, devDpi, devXDpi, devYDpi,
devPxWidth, devPxHeight, devRefreshRate, devOrientation;
static TextView tvDispDensity, tvDensityCategory, tvDispDpi, tvDispXDpi,
tvDispYDpi, tvDispPxWidth, tvDispPxHeight, tvDispRefreshRate,
tvOrientation;
DisplayMetrics metrics;
// For sharing information via e-mail/SMS
/**
* Final Content of display info
*/
String displayInfoContent;
/**
* All textviews used in for-loop to getText()
*/
private TextView[] textViews;
private String[] strings;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.display, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay()
.getMetrics(metrics);
devDensity = String.valueOf(metrics.density);
devDpi = String.valueOf(metrics.densityDpi);
devXDpi = String.valueOf(metrics.xdpi);
devYDpi = String.valueOf(metrics.ydpi);
devPxWidth = String.valueOf(metrics.widthPixels);
devPxHeight = String.valueOf(metrics.heightPixels);
devRefreshRate = String.valueOf(getActivity().getWindowManager()
.getDefaultDisplay().getRefreshRate());
translate();
// Displays
tvDispDensity = (TextView) getView()
.findViewById(R.id.tvDisplayDensity);
tvDispDensity.setText(devDensity);
tvDensityCategory = (TextView) getView().findViewById(
R.id.tvDensityCategory);
tvDensityCategory.setText(devDensityCategory);
tvDispDpi = (TextView) getView().findViewById(R.id.tvDisplayDpi);
tvDispDpi.setText(devDpi);
tvDispXDpi = (TextView) getView().findViewById(R.id.tvDisplayXDpi);
tvDispXDpi.setText(devXDpi);
tvDispYDpi = (TextView) getView().findViewById(R.id.tvDisplayYDpi);
tvDispYDpi.setText(devYDpi);
tvDispPxWidth = (TextView) getView()
.findViewById(R.id.tvDisplayWidthPx);
tvDispPxWidth.setText(devPxWidth + " px");
tvDispPxHeight = (TextView) getView().findViewById(
R.id.tvDisplayHeightPx);
tvDispPxHeight.setText(devPxHeight + " px");
tvDispRefreshRate = (TextView) getView().findViewById(
R.id.tvDisplayRefreshRate);
tvDispRefreshRate.setText(devRefreshRate + " Hz");
tvOrientation = (TextView) getView().findViewById(
R.id.tvDisplayOrientation);
tvOrientation.setText(devOrientation);
//
// #################################
textViews = new TextView[]{tvDispDensity, tvDensityCategory, tvDispDpi, tvDispXDpi,
tvDispYDpi, tvDispPxWidth, tvDispPxHeight, tvDispRefreshRate,
tvOrientation};
strings = new String[]{devDensity, devDensityCategory, devDpi, devXDpi, devYDpi,
devPxWidth, devPxHeight, devRefreshRate, devOrientation};
}
private void translate() {
switch (metrics.densityDpi) {
case DisplayMetrics.DENSITY_LOW:
devDensityCategory = "Low (120 dpi)";
break;
case DisplayMetrics.DENSITY_MEDIUM:
devDensityCategory = "Medium (160 dpi)";
break;
case DisplayMetrics.DENSITY_HIGH:
devDensityCategory = "High (240 dpi)";
break;
case DisplayMetrics.DENSITY_XHIGH:
devDensityCategory = "XHigh (320 dpi)";
break;
case DisplayMetrics.DENSITY_XXHIGH:
devDensityCategory = "XXHigh (480 dpi)";
break;
case DisplayMetrics.DENSITY_XXXHIGH:
devDensityCategory = "XXXHigh (640 dpi)";
break;
case DisplayMetrics.DENSITY_TV:
devDensityCategory = "TV (213 dpi)";
break;
case DisplayMetrics.DENSITY_400:
devDensityCategory = "Intermediate (400 dpi)";
break;
default:
devDensityCategory = "Unknown";
break;
}
switch (getActivity().getWindowManager().getDefaultDisplay()
.getRotation()) {
case Surface.ROTATION_0:
devOrientation = "0°";
break;
case Surface.ROTATION_90:
devOrientation = "90°";
break;
case Surface.ROTATION_180:
devOrientation = "180°";
break;
case Surface.ROTATION_270:
devOrientation = "270°";
break;
default:
devOrientation = "Unknown";
break;
}
}
public String retrieveContent() {
String content = "";
for (int i = 0; i < textViews.length; i++) {
content += (textViews[i].getText() + " = " + strings[i] + "\n");
}
return content;
}
Are the Arrays initialized when the Fragment is off-screen? If not, how could I pull this off otherwise?
Logcat:
java.lang.NullPointerException
at my.package.name.fragments.DisplayFragment.retrieveContent(DisplayFragment.java:170)
which is this content += (textViews[i].getText() + " = " + strings[i] + "\n"); in my for loop.
Any insight will be greatly appreciated, thank you for reading.
Regards,
Matt
Are the Arrays initialized when the Fragment is off-screen? If not, how could I pull this off otherwise?
The problem is when the Fragment is off-screen, the fragment itself might not have been created at all. By default, the view pager only initialize the previous and next page of the current page. For example, if you are active on page 1, only page 2 will be initialized by default. if you are on third page, then only 2nd, 3rd and 4th pages are created.
To pull this of otherwise? you can just simply set the off screen page limit to 4, so that it will make sure that it will keep 4 pages next or previous of your current page (in your case, all 5 fragments will be created at once)
urViewPager.setOffscreenPageLimit(4);
put that code in the OnCreate on the activity after your initialization of urViewPager.
Hope this helps, cheer.
I found the culprit. Apparently the TextView[] was not initialized properly. Insted of having it in OnActivityCreated(), I initialized TextView[] and String[] directly in retrieveInfo() method of fragment. Also made textview fields static variables and works like a charm
public String retrieveContent() {
textViews = new TextView[]{tvDispDensity, tvDensityCategory, tvDispDpi, tvDispXDpi,
tvDispYDpi, tvDispPxWidth, tvDispPxHeight, tvDispRefreshRate,
tvOrientation};
strings = new String[]{devDensity, devDensityCategory, devDpi, devXDpi, devYDpi,
devPxWidth, devPxHeight, devRefreshRate, devOrientation};
String content = "";
for (int i = 0; i < textViews.length; i++) {
content += (textViews[i].getText() + " = " + strings[i] + "\n");
}
return content;
}
Works just fine now :)
Related
So I have multiple Views(a combination of multiple text boxes and images) and I have attached an onClickListener to the views and then attached an onClick method to them and after that I have used a switch statement within an onClick for the same.
ConstraintLayout[] ll_but = new ConstraintLayout[10];
ll_but[0] = findViewById(R.id.success_stories_tile);
ll_but[1] = findViewById(R.id.misc_but3_layout);
ll_but[2] = findViewById(R.id.misc_but2_layout);
ll_but[3] = findViewById(R.id.faqs_tile);
ll_but[4] = findViewById(R.id.mohfw_ll2);
ll_but[5] = findViewById(R.id.mohfw_ll3);
ll_but[6] = findViewById(R.id.mohfw_ll4);
ll_but[7] = findViewById(R.id.mohfw_ll5);
int[] btnToAdd = new int[]{0, 1, 2, 3, 4, 5, 6 , 7};
for (int i = 0; i < btnToAdd.length; i++) {
ll_but[btnToAdd[i]].setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent i = null;
switch (view.getId()){
case R.id.flipperLeft:
viewFlipper.stopFlipping();
viewFlipper.setInAnimation(anim1);
viewFlipper.setOutAnimation(anim4);
viewFlipper.showNext();
break;
case R.id.flipperRight:
viewFlipper.stopFlipping();
viewFlipper.setInAnimation(anim2);
viewFlipper.setOutAnimation(anim3);
viewFlipper.showPrevious();
break;
case R.id.misc_but2_layout:
i = new Intent(homeActivity.this, ChatActivity.class);
startActivity(i);
break;
case R.id.misc_but3_layout:
i = getMythIntent(this);
startActivity(i);
break;
case R.id.success_stories_tile:
i = getSuccessStoriesIntent(this);
startActivity(i);
break;
case R.id.faqs_tile:
i = getFAQsIntent(this);
startActivity(i);
break;
case R.id.mohfw_ll2:
case R.id.mohfw_ll3:
case R.id.mohfw_ll4:
case R.id.mohfw_ll5:
i = getTwitterIntent ( this );
startActivity(i);
break;
default:
i = null;
break;
}
}
This is working for me but I would really like to know if there is a better implementation for the same.
Android Studio now has something like Butterknife implemented in their IDE now, therefore, you can just use <YourLayoutName>Binding binding = <YourLayoutName>Binding.inflate(); in your onCreateView()
As example:
LoginFragmentBinding binding = LoginFragmentBinding.inflate(inflater, container, false);
With that, you can use for example binding.miscBut3Layout.setOnClickListener(this); instead of the getViewById that can return an exception.
Another approach for your Listeners is to put them in the XML with the view with the attribute android:onclick="" with this, you don't need to create an array for the views.
I am a beginner with Android Studio creating an app that will read from an API. I am trying to get the API to filter results using options chosen in two spinners; one option for a type of electric car charger and another for the max radius of results to be shown. The app suddenly crashes when it gets to the "if (defaultCharger.equals("0"))" statement. As there are no errors shown I have commented out different sections of code to see what works and this part seems to be the issue. This is here so the link will not include a filter for the charger type if one has not been chosen. Any help with this problem would be appreciated.
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Spinner;
public class results extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
String link, getLong, getLat;
TextView newAddress, see, testResult;
//Create spinner options
Spinner dropdownone, dropdowntwo;
String[] distance = {"Change the radius in miles", "1", "5", "10"};
String[] chargeType = {"Filter by type of charger", "Under 2kW", "Over 2kW", "40kW and higher"};
String defaultMiles, defaultCharger;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
//text link to go back to home and enter a new address
newAddress = findViewById(R.id.newAddress);
newAddress.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
newAddress.setTextColor(getResources().getColorStateList(R.color.colorAccent));
Intent intent = new Intent(results.this, home.class);
startActivity(intent);
}
});
//get longitude and latitude
home home = new home();
if (home.status.equals("gps")){
getLong = home.gpsLong;
getLat = home.gpsLat;
}
else if (home.status.equals("address")){
GeoLocation geoLocation = new GeoLocation();
getLong = geoLocation.adLongitude;
getLat = geoLocation.adLatitude;
}
else {
getLong = "-5.926437";
getLat = "54.607868";
}
//drop down one - choose radius of results
dropdownone = findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(results.this,
android.R.layout.simple_spinner_item,distance);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dropdownone.setAdapter(adapter);
dropdownone.setOnItemSelectedListener(this);
//drop down two - choose charger type
dropdowntwo = findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(results.this,
android.R.layout.simple_spinner_item,chargeType);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dropdowntwo.setAdapter(adapter2);
dropdowntwo.setOnItemSelectedListener(this);
//see if correct values are being returned
see = findViewById(R.id.textView6);
see.setText(defaultMiles + " " + defaultCharger);
//create link
if (defaultCharger.equals("0")){
link = "https://api.openchargemap.io/v3/poi/?output=json&Latitude=" + getLat + "&Longitude=" + getLong + "&distance=" + defaultMiles + "&distanceunit=miles&maxresults=10&compact=true&verbose=false";
}
else {
link = "https://api.openchargemap.io/v3/poi/?output=json&Latitude=" + getLat + "&Longitude=" + getLong + "&distance=" + defaultMiles + "&distanceunit=miles&LevelID=" + defaultCharger + "&maxresults=1&compact=true&verbose=false";
}
testResult = findViewById(R.id.testResult);
testResult.setText(link);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (parent.getId()){
case R.id.spinner1:
switch (position) {
case 0:
// Create default if nothing is changed
defaultMiles = "0";
break;
case 1:
// 1 mile radius
defaultMiles = "1";
break;
case 2:
// 5 mile radius
defaultMiles = "5";
break;
case 3:
// 10 mile radius
defaultMiles = "10";
break;
}
case R.id.spinner2:
switch (position) {
case 0:
// Default for no charger filter
defaultCharger = "0";
break;
case 1:
// Type 1 charger
defaultCharger = "1";
break;
case 2:
// Type 2 charger
defaultCharger = "2";
break;
case 3:
// Type 3 charger
defaultCharger = "3";
break;
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
defaultMiles = "10";
defaultCharger = "0";
}
}
Your defaultCharger String will be null in the onCreate method, causing a NullPointerException when trying to compare it : if (defaultCharger.equals("0")) .
You can prevent it by setting a value like "" for defaultCharger during variable declaration:
String defaultCharger = "";
or by switching around the equals statement:
if ("0".equals(defaultCharger))
I've spent for almost 2 days to solve this problem. I've also searched others similar problem but still can't find a way to solve this. Really grateful for the help.
#Override
public void addField() {
if (countTv < 7) {
allTextView[countTv] = new TextView(this);
allTextView[countTv].setId(countTv);
button = new Button(this);
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 50, 0, 0);
button = findViewById(R.id.bt_new_field);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.search_field, null);
allTextView[countTv].setOnClickListener(listener);
llParentField.addView(rowView, layoutParams);
}
countTv++;
}
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()){
case 0:
tag = v.getId();
toSearchActivity();
break;
case 1:
tag = v.getId();
toSearchActivity();
break;
case 2:
tag = v.getId();
toSearchActivity();
break;
case 3:
tag = v.getId();
toSearchActivity();
break;
case 4:
tag = v.getId();
toSearchActivity();
break;
case 5:
tag = v.getId();
toSearchActivity();
break;
case 6:
tag = v.getId();
toSearchActivity();
break;
}
}
};
Note: I've tried to do every possible ways on how to implement the View.OnClickListener but still the new added TextView can't be clicked. Forget about the button in my code, It's not important. Thank you very much for the help!
For every view component we can call setOnclickLister() like that for TextView.
You can write this:
allTextView[countTv].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
}
});
You can do it with
textGreen[i].setOnClickListener(ClickListener);
I am implementing a Drag and Drop interface with 6 Drag TextViews and 6 Drop TextViews.
Any 1 of 6 Drag views can be dropped onto any 1 of 6 Drop views.
An example would be if tvNum1=13, tvNum2=16, tvNum3=9, then I can drop any one of them onto tvSTR, tvDEX, or tvINT and set the respective Drop text to whatever was dragged. So if tvNum2 was dragged onto tvSTR, then tvSTR should show 16. Or let's say that instead you decided to drag tvNum3 onto tvSTR, then tvSTR should show 9. Each Drag view can only be used once.
Here's my current code. I can't find a way to make it so that the text of whatever was dragged is the text of the Drop view. I can only make (a 1-to-1 correlation) it so that if tvNum1 was dragged onto tvSTR, then tvSTR=13. Or if tvNum2 was dragged onto tvDEX, then tvDEX=16.
public class setCharacterRolls extends AppCompatActivity
{
// Variable declarations
private TextView tvNum1, tvNum2, tvNum3, tvNum4, tvNum5, tvNum6;
private TextView tvSTR, tvDEX, tvINT, tvWIS, tvCHA, tvCON;
String roll1, roll2, roll3, roll4, roll5, roll6;
// String roll1Num, roll2Num, roll3Num, roll4Num, roll5Num, roll6Num;
private Button bSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_character_rolls);
// Get the previous intent
Intent intent = getIntent();
// Save the rolls from the previous screen onto a String to use later.
roll1 = intent.getStringExtra("roll1");
roll2 = intent.getStringExtra("roll2");
roll3 = intent.getStringExtra("roll3");
roll4 = intent.getStringExtra("roll4");
roll5 = intent.getStringExtra("roll5");
roll6 = intent.getStringExtra("roll6");
// Get TextView id's
tvNum1 = (TextView) findViewById(R.id.tDrag1);
tvNum2 = (TextView) findViewById(R.id.tDrag2);
tvNum3 = (TextView) findViewById(R.id.tDrag3);
tvNum4 = (TextView) findViewById(R.id.tDrag4);
tvNum5 = (TextView) findViewById(R.id.tDrag5);
tvNum6 = (TextView) findViewById(R.id.tDrag6);
tvSTR = (TextView) findViewById(R.id.tvSTR);
tvDEX = (TextView) findViewById(R.id.tvDEX);
tvINT = (TextView) findViewById(R.id.tvINT);
tvWIS = (TextView) findViewById(R.id.tvWIS);
tvCHA = (TextView) findViewById(R.id.tvCHA);
tvCON = (TextView) findViewById(R.id.tvCON);
// Set TextView to the roll numbers
tvNum1.setText(roll1);
tvNum2.setText(roll2);
tvNum3.setText(roll3);
tvNum4.setText(roll4);
tvNum5.setText(roll5);
tvNum6.setText(roll6);
// Set TextView to be draggable
tvNum1.setOnTouchListener(onTouch);
tvNum2.setOnTouchListener(onTouch);
tvNum3.setOnTouchListener(onTouch);
tvNum4.setOnTouchListener(onTouch);
tvNum5.setOnTouchListener(onTouch);
tvNum6.setOnTouchListener(onTouch);
// Targets to be dropped on
tvSTR.setOnDragListener(dragListener);
tvDEX.setOnDragListener(dragListener);
tvINT.setOnDragListener(dragListener);
tvWIS.setOnDragListener(dragListener);
tvCHA.setOnDragListener(dragListener);
tvCON.setOnDragListener(dragListener);
}
View.OnTouchListener onTouch = new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent mEvent)
{
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuild = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuild, v, 0);
return true;
}
};
View.OnDragListener dragListener = new View.OnDragListener()
{
#Override
public boolean onDrag(View v, DragEvent event)
{
int dragEvent = event.getAction();
switch(dragEvent)
{
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
final View view = (View) event.getLocalState();
switch(view.getId())
{
// This is where I have the 1-to-1 correlation.
// I want to be able to drag any draggable texview and set it to whatever it was dropped on.
case R.id.tDrag1:
tvSTR.setText(roll1);
break;
case R.id.tDrag2:
tvDEX.setText(roll2);
break;
default:
break;
}
break;
case DragEvent.ACTION_DROP:
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
default:
break;
}
return true;
}
};
}
In my sample project, I implemented the code using activity, running fine.
After then I implemented that code in my main App which is using Fragment.
For that I extends Fragment, made a default constructor, made onCreateView & inside that inflated the layout, then changed getApplicationContext() to getActivity(). Thats how I managed to remove errors in my main app.
An in my MainActivity this is how we update the main content by replacing fragments by calling constructor & assigning it to fragments:
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new IntroductionFragment();
break;
case 1:
fragment = new PrefaceFragment();
break;
case 2:
fragment = new PreambleFragment();
break;
case 3:
fragment = new ContentsFragment();//ERROR here: because I modified this class
break;
case 4:
fragment = new SchedulesFragment();
break;
case 5:
fragment = new AppendixFragment();
break;
case 6:
fragment = new AmendmentFragment();
break;
case 7:
fragment = new PhotoFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
That line shows me Type mismatch: cannot convert from ContentsFragment to Fragment
I hope the problem is clear. Please tell me how can I avoid this error...
This is ContentsFragment.java
public class ContentsFragment extends Fragment// implements OnTouchListener
{
public ContentsFragment() {
// TODO Auto-generated constructor stub
}
public static ArrayList<GS> q = new ArrayList<GS>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBAdapter db = DBAdapter.getDBAdapter(getActivity());
if (!db.checkDatabase())
db.createDatabase(getActivity());
db.openDatabase();
q = db.getData();
// setContentView(R.layout.sample_activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v =inflater.inflate(R.layout.sample_activity, container, false);
final FloatingGroupExpandableListView list = (FloatingGroupExpandableListView) v.findViewById(R.id.sample_activity_list);
// final LayoutInflater inflater = getLayoutInflater();
// Even though the child divider has already been set on the layout file, we have to set it again here
// This prevents a bug where the background turns to the color of the child divider when the list is expanded
list.setChildDivider(new ColorDrawable(Color.BLACK));
final ArticlesAdapter adapter = new ArticlesAdapter(getActivity());
final WrapperExpandableListAdapter wrapperAdapter = new WrapperExpandableListAdapter(adapter);
list.setAdapter(wrapperAdapter);
for(int i = 0; i < wrapperAdapter.getGroupCount(); i++) {
list.collapseGroup(i);//expandGroup(i);
}
list.setOnScrollFloatingGroupListener(new FloatingGroupExpandableListView.OnScrollFloatingGroupListener() {
#Override
public void onScrollFloatingGroupListener(View floatingGroupView, int scrollY) {
float interpolation = - scrollY / (float) floatingGroupView.getHeight();
// Changing from RGB(162,201,85) to RGB(255,255,255)
final int greenToWhiteRed = (int) (0 + 3 * interpolation);
final int greenToWhiteGreen = (int) (0 + 6* interpolation);
final int greenToWhiteBlue = (int) ( 0+ 20 * interpolation);
final int greenToWhiteColor = Color.argb(200, greenToWhiteRed, greenToWhiteGreen, greenToWhiteBlue);
// Changing from RGB(255,255,255) to RGB(0,0,0)
final int whiteToBlackRed = (int) (255 - 1 * interpolation);
final int whiteToBlackGreen = (int) (255 - 1 * interpolation);
final int whiteToBlackBlue = (int) (255 - 1 * interpolation);
final int whiteToBlackColor = Color.argb(255, whiteToBlackRed, whiteToBlackGreen, whiteToBlackBlue);
final View background = floatingGroupView.findViewById(R.id.sample_activity_list_group_item_background);
background.setBackgroundColor(greenToWhiteColor);
final TextView text = (TextView) floatingGroupView.findViewById(R.id.sample_activity_list_group_item_text);
text.setTextColor(whiteToBlackColor);
final ImageView expanded = (ImageView) floatingGroupView.findViewById(R.id.sample_activity_list_group_expanded_image);
final Drawable expandedDrawable = expanded.getDrawable().mutate();
expandedDrawable.setColorFilter(whiteToBlackColor, PorterDuff.Mode.SRC_ATOP);
}
});
return v;
}
}
I think the problem is because of importing Fragment from different sources in two different classes ie. Your_Activity and ContentsFragment.java. One from android.support.v4.app.Fragment and one from android.app.Fragment.
Please make sure you are importing Fragment either from support library ie.android.support.v4.app.Fragment or from android.app.Fragment.