问题
I'm constantly receiving data in MainActivity and I need it to only be written a textView when the fragment that contains the textView is inflated. Obviously the app crashes if I try to write to a textView that is in a fragment currently not in display. I have made a dummy activity to explain my problem and I'm using a countdown timer to represent the data that is continually changing in the MainActivity. The issue resides in MainActivity.
Would someone mind explaining how I can only write to the textView when it is inflated?
Thanks
FragmentA.java (Fragment B and C are almost identical)
package com.felhr.scrolltabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Luke on 20/04/2015.
*/
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
}
MainActivity.java
package com.felhr.scrolltabs;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
ViewPager viewPager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
final TextView timer = (TextView)findViewById(R.id.textViewTimer);
// Countdown timer code -> to be run constantly but only displayed in Fragment A is in focus
// This section breaks the code
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
timer.setText("done!");
}
}.start();
}
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
Log.d("SWIPE","get Item is called"+i);
if (i==0)
{
fragment=new FragmentA();
}
if (i==1)
{
fragment=new FragmentB();
}
if (i==2)
{
fragment=new FragmentC();
}
return fragment;
}
@Override
public int getCount() {
Log.d("SWIPE","get count is called");
return 3;
}
// talks to title to give the page
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "Tab 1";
}
if (position == 1) {
return "Tab 2";
}
if (position == 2) {
return "Tab 3";
}
return null;
}
}
fragment_a.xml (fragments b and c are almost identical)
<?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:background="#C3f3f3">
<TextView
android:id="@+id/textViewTimer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment A"
android:textColor="#000000"
android:gravity="center"/>
</RelativeLayout>
activity_main.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager">
<android.support.v4.view.PagerTitleStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:background="#33B5E5">
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
来源:https://stackoverflow.com/questions/29743862/updating-a-textview-s-in-a-fragment-from-an-activity-using-viewpager