My application (minimum level 13) is an action that uses tabs in the action bar to manage fragments of a pair, very similar to this .
Now the action starts the service, which performs continuous calculations and returns the values ββthat I would like to display in the Fragments. Communication Activity-Service is implemented through broadcast receivers, and Activity drags the data into the corresponding fragment.
Everything seems to be properly configured and the data moves to the fragment update method, but when I try to display new values ββin text views, new values ββare never displayed.
Code for changing text views:
TextView tv = (TextView) getView().findViewById(R.id.fieldNavGpsTime); Double doub = input.getDoubleExtra("com.some.thing.GPS_TIME", -1.0); tv.setText(doub.toString());
Code for calling methods for updating fragments from the broadcast receiver in the Office:
NavigationGuiFragment navfrag = (NavigationGuiFragment) getFragmentManager().findFragmentByTag("navigation"); if (navfrag != null && navfrag.isResumed()) navfrag.UpdateNavUI(intent);
I noticed that isVisible () seems to never return true, but I'm not sure what that means or how to change it.
Also, I cannot add the image to the fragment programmatically. Here's the code (which is in onActivityCreated ()):
this.compass = new BasicCompass(getActivity()); LinearLayout ll = (LinearLayout) getView().findViewById(R.id.nav_hrztl_lnly); ll.addView(this.compass);
The BasicCompass constructor takes context, admittedly, I'm not quite sure what I'm passing correctly.
The code for this was more or less taken from the working action and fell into the fragment to include tabs. I am open to suggestions regarding changing the structure of the code.
Thanks for any help.
EDIT
Layout xml fragment:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/nav_hrztl_lnly" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:focusable="true" android:focusableInTouchMode="true" android:baselineAligned="false" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="600dp" android:layout_height="fill_parent" android:orientation="vertical" android:focusable="true" android:focusableInTouchMode="true" > <EditText android:id="@+id/labelNavGpsTime" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/gps_time" /> <EditText android:id="@+id/fieldNavGpsTime" style="@style/field_padding" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/zero_3_digits" android:inputType="numberDecimal" /> <EditText android:id="@+id/labelNavLatitude" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/latitude" /> <EditText android:id="@+id/fieldNavLatitude" style="@style/field_padding" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/zero_6_digits" android:inputType="numberDecimal" /> <EditText android:id="@+id/labelNavLongitude" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/longitude" /> <EditText android:id="@+id/fieldNavLongitude" style="@style/field_padding" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/zero_6_digits" android:inputType="numberDecimal" /> <EditText android:id="@+id/labelNavAltitude" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/altitude" /> <EditText android:id="@+id/fieldNavAltitude" style="@style/field_padding" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/zero_3_digits" android:inputType="numberDecimal" /> <EditText android:id="@+id/labelNavRoll" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/roll" /> <EditText android:id="@+id/fieldNavRoll" style="@style/field_padding" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/zero_6_digits" android:inputType="numberDecimal" /> <EditText android:id="@+id/labelNavPitch" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/pitch" /> <EditText android:id="@+id/fieldNavPitch" style="@style/field_padding" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/zero_6_digits" android:inputType="numberDecimal" /> <EditText android:id="@+id/labelNavAzimuth" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/azimuth_heading" /> <EditText android:id="@+id/fieldNavAzimuth" style="@style/field_padding" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/zero_6_digits" android:inputType="numberDecimal" /> <LinearLayout android:id="@+id/nav_rdbtn_lnly" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:id="@+id/rdbtnNavGpsAvailability" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/gps_avail" /> <RadioButton android:id="@+id/rdbtnNavZuptStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/zupt_stat" /> </LinearLayout> </LinearLayout>
And the fragment that uses it:
public class NavigationGuiFragment extends Fragment { private RadioButton gpsRdBtn; private RadioButton zuptRdBtn; private BasicCompass compass; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View fragview = inflater.inflate(R.layout.navigation_fragment, container, false); // sets up the rose image that serves as a compass in the GUI this.compass = new BasicCompass(getActivity()); LinearLayout ll = (LinearLayout) fragview.findViewById(R.id.nav_hrztl_lnly); ll.addView(this.compass); return fragview; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getActivity().setContentView(R.layout.navigation_fragment); //Initialize the radio buttons gpsRdBtn = (RadioButton) getView().findViewById(R.id.rdbtnNavGpsAvailability); gpsRdBtn.setChecked(false); zuptRdBtn = (RadioButton) getView().findViewById(R.id.rdbtnNavZuptStatus); zuptRdBtn.setChecked(false); } @Override public void onResume() { super.onResume(); if (!IsMyServiceRunning()) { gpsRdBtn.setChecked(false); zuptRdBtn.setChecked(false); } } public void UpdateNavUI(Intent input) { TextView tv = (TextView) getView().findViewById(R.id.fieldNavGpsTime); Double doub = input.getDoubleExtra("com.some.thing.GPS_TIME", -1.0); tv.setText(doub.toString()); tv = (TextView) getView().findViewById(R.id.fieldNavLatitude); doub = input.getDoubleExtra("com.some.thing.LATITUDE", 100000.0); tv.setText(doub.toString()); tv = (TextView) getView().findViewById(R.id.fieldNavLongitude); doub = input.getDoubleExtra("com.some.thing.LONGITUDE", 100000.0); tv.setText(doub.toString()); tv = (TextView) getView().findViewById(R.id.fieldNavAltitude); doub = input.getDoubleExtra("com.some.thing.ALTITUDE", -1.0); tv.setText(doub.toString()); tv = (TextView) getView().findViewById(R.id.fieldNavRoll); doub = input.getDoubleExtra("com.some.androidndk.ROLL", 361.0); tv.setText(doub.toString()); tv = (TextView) getView().findViewById(R.id.fieldNavPitch); doub = input.getDoubleExtra("com.some.thing.PITCH", 361.0); tv.setText(doub.toString()); tv = (TextView) getView().findViewById(R.id.fieldNavAzimuth); doub = input.getDoubleExtra("com.some.thing.AZIMUTH", 361.0); tv.setText(doub.toString()); this.compass.SetDirection(doub.floatValue()); boolean bool = input.getBooleanExtra("com.some.thing.ZUPT_STATUS", false); zuptRdBtn.setChecked(bool); UpdateGpsIndicator(input); } public void UpdateGpsIndicator(Intent input) { boolean bool = input.getBooleanExtra("com.some.thing.GPS_ON", false); gpsRdBtn.setChecked(bool); } private boolean IsMyServiceRunning() { ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if ("com.some.thing.Service".equals(service.service.getClassName())) return true; } return false; }
}