2015-11-08 80 views
0

因此,您可以在onCreate方法中看到我在“片段”列表中的第0个片段上调用getView。它返回null。我试着在代码中放置一个计时器,让它每秒运行一次,并检查视图是否为空。而在计时器的第二个勾号上,视图不为空。基本上在片段上调用getView返回null

MainActivity.java

package com.axinite.standapp; 

import android.graphics.Color; 
import android.os.CountDownTimer; 
import android.os.Handler; 
import android.support.annotation.Nullable; 
import android.support.design.widget.TabLayout; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 

import android.view.ViewParent; 
import android.widget.EditText; 
import android.widget.NumberPicker; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Timer; 
import java.util.TimerTask; 
import java.util.Vector; 

public class MainActivity extends AppCompatActivity { 

    int i = 0; 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link FragmentPagerAdapter} derivative, which will keep every 
    * loaded fragment in memory. If this becomes too memory intensive, it 
    * may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    private SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    private ViewPager mViewPager; 

    int standUpSeconds = 4000; 

    List<android.support.v4.app.Fragment> fragments = new Vector<android.support.v4.app.Fragment>(); 

    NumberPicker SUPickerH; 
    NumberPicker SUPickerM; 
    NumberPicker SUPickerS; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

     setSupportActionBar(toolbar); 
     fragments.add(Fragment.instantiate(this, BlankFragment.class.getName())); 

     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), fragments); 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
     final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 
     tabLayout.setSelectedTabIndicatorColor(Color.WHITE); 
     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(); 
      } 
     }); 
     final int[] ICONS = new int[] { 
       R.drawable.walking, 
       R.drawable.iris 
     }; 
     for (int i=0; i < tabLayout.getTabCount(); i++) 
     { 
      tabLayout.getTabAt(i).setIcon(getDrawable(ICONS[i])); 
     }//endfor) 
     View view = ((BlankFragment) fragments.get(0)).getView(); 
     if(view != null) { 
      TextView tv = (TextView) view.findViewById(R.id.glupost); 
      tv.setText("dasdas"); 
      //Toast.makeText(getApplicationContext(), ((Fragment) fragments.get(0)).toString(), Toast.LENGTH_SHORT).show(); 
     } 
    } 

    void Setup() { 



    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 
     List<Fragment> fragments; 
     public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) { 
      super(fm); 
      this.fragments = fragments; 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return fragments.get(position); 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return fragments.size(); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 

      return ""; 
     } 
    } 
} 

BlankFragment.java

package com.axinite.standapp; 

import android.app.Activity; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


/** 
* A simple {@link Fragment} subclass. 
* Activities that contain this fragment must implement the 
* {@link BlankFragment.OnFragmentInteractionListener} interface 
* to handle interaction events. 
* Use the {@link BlankFragment#newInstance} factory method to 
* create an instance of this fragment. 
*/ 
public class BlankFragment extends Fragment { 

    protected View mView; 
    // TODO: Rename parameter arguments, choose names that match 
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    // TODO: Rename and change types of parameters 
    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 

    /** 
    * Use this factory method to create a new instance of 
    * this fragment using the provided parameters. 
    * 
    * @param param1 Parameter 1. 
    * @param param2 Parameter 2. 
    * @return A new instance of fragment BlankFragment. 
    */ 
    // TODO: Rename and change types and number of parameters 
    public static BlankFragment newInstance(String param1, String param2) { 
     BlankFragment fragment = new BlankFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public BlankFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_blank, container, false); 
     mView = view; 
     return view; 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mListener = (OnFragmentInteractionListener) activity; 
     } catch (ClassCastException e) { 

     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    /** 
    * This interface must be implemented by activities that contain this 
    * fragment to allow an interaction in this fragment to be communicated 
    * to the activity and potentially other fragments contained in that 
    * activity. 
    * <p/> 
    * See the Android Training lesson <a href= 
    * "http://developer.android.com/training/basics/fragments/communicating.html" 
    * >Communicating with Other Fragments</a> for more information. 
    */ 
    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     public void onFragmentInteraction(Uri uri); 
    } 

} 
+0

无处你编写一个调用'getView' – njzk2

+0

我们走了。固定。 – Axinite

+0

什么?任何人? – Axinite

回答

0

用于Activity的典型方法与其Fragment小号通信如下:

MyFragment frag = (MyFragment) getFragmentManager().findFragmentById(R.id.container_of_fragment); 
frag.getMyTextView().setText("New text"); 

或(通常首选IMO)

MyFragment frag = (MyFragment) getFragmentManager().findFragmentByTag("fragment_tag_string_value"); 
frag.getMyTextView().setText("New text"); 

这变得更加困难与然而一个ViewPager,作为容器的“id”是ViewPager本身,它包含多个片段。另外,没有内置的适配器机制(我知道)将标记设置为碎片。

有两种方法我可以想到绕过这个。首先,您可以使用Android API中的EventBus,例如GreenRobot's EventBusLocalBroadcastManager。 GreenRobot的EventBus也是回答this类似问题的推荐技巧。

一种替代将利用这样一个事实:Fragment总是到其主机Activity,这意味着Fragment可以经由interface请求来自Activity文本,将其返回给所述Fragment的引用:

public class MainActivity implements FragmentOne.Callbacks { 

    //... 
    @Override 
    public String requestTextViewString() { 
     return "Text to send to Fragment" 
    } 

而且你的假设FragmentOne内:

public class FragmentOne extends Fragment { 

    private Callbacks mCallback; 
    private TextView mTextView; 

    public interface Callbacks { 
     String requestTextViewString(); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     if (activity instanceof Callbacks) { 
      mCallback = (Callbacks) activity; 
     } 
    } 

/* 
*Later in your Fragment, when you want to get text from your MainActivity 
* i.e. in onCreateView() 
*/ 
    mTextView.setText(mCallback.requestTextViewString()); 

编辑:另请注意,在Activity中尝试访问onCreate()中的Fragment可能会导致产生NullPointerExceptionThis图表显示了两个生命周期并排。这不应该是一个问题,但是,由于在Fragment的“建立”生命周期事件所需的任何东西正好可以作为FragmentnewInstance()方法的参数传递:

public class FragmentOne extends Fragment { 

    private static final String KEY = "text_for_text_view_key"; 

    public static FragmentOne newInstance(String textForTextView) { 
     Bundle args = new Bundle(); 
     FragmentOne frag = new FragmentOne(); 
     args.putString(KEY, textForTextView); 

     frag.setArguments(args); 
     return frag; 
    } 

//And when required... 
    mTextView.setText(getArguments().getString(KEY));