Android - SetText中的碎片不起作用

时间:2017-06-14 13:53:26

标签: android android-fragments

我无法使用片段中的函数更改文本字段或任何对象。我该怎么办?

片段:

public class Benimsayfam extends Fragment{
  TextView tv;

  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout.benimsayfam,container,false);
      tv = (TextView) v.findViewById(R.id.TtT);
      return v;

  }

  public void update(){
      tv.setText("Updated");

  }
}

MainActivity:

public class MainActivity extends AppCompatActivity implements HttpRequest.AsyncResponse {


private ViewPager mViewPager;
SectionsPageAdapter mSectionPageAdapter;

BanaYazilanAdapter banaYazilanAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainactivity);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });


    mSectionPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    setupViewPager(mViewPager);

    TabLayout tablayout = (TabLayout) findViewById(R.id.tabs);
    tablayout.setupWithViewPager(mViewPager);



}

public void setupViewPager(ViewPager viewPager){
    SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
    adapter.addFragment(new Benimsayfam(),"Benim Sayfam");
    adapter.addFragment(new Arges(),"Arges");
    adapter.addFragment(new Bersd(),"Bersd");
    viewPager.setAdapter(adapter);
}




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_s, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


public void processFinish(JSONObject object) throws JSONException {

            Benimsayfam b = new Benimsayfam();
            b.update();

}
}

我正在使用标签菜单。我需要在Tab转换期间在MainActivity中使用一个函数。我在片段中尽快使用AsyncResponse时遇到问题。

1 个答案:

答案 0 :(得分:0)

只有在片段事务发生后才会调用片段的

onCreateView()

您刚刚创建了片段的对象并调用了 update() 如果我没有错,这应该导致空指针异常。

您需要做的是在Activity中,在片段上创建片段实例和performa事务,以便显示片段。

以下是您的需求:

public class YourActivty extends Activity {

  public void onCreate(Bundle SavedInstanceState) {
   Benimsayfam fam = Benimsayfam.newInstance();
     getFragmentManager()
            .beginTransaction()
            .replace(R.id.fragment_container, 
                     fam,
                     Benimsayfam.TAG)
            .commit();

       fam.update();
  }


}





    public class Benimsayfam extends Fragment {
      public static final String TAG = "Benimsayfam";

      public static Benimsayfam newInstance() {
        return new Benimsayfam(); 
      }

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.benimsayfam,container,false);
          tv = (TextView) v.findViewById(R.id.TtT);
          return v;
      }

      public void update(){
          tv.setText("Updated");
      }

}

在您的activity.xml中,您需要一个标识为R.id.fragment_container的框架布局 这就是我们将片段化的地方。