简体   繁体   中英

FindFragmentByTag returns null.

I am just a begginer in android development, and recently I have started the Android Tutorial on developer.android.com. Everything seemed fine, until now. I am doing exacly this course http://developer.android.com/training/basics/fragments/communicating.html , and i am stuck. I change it a bit, coz i wanted more flexibility, so I add my fragment an runtime. I have spend like an hour looking for an answer, but couldnt find any solution for my problem. :(

Here is my problem: I want to communicate with my fragment just like in the tutorial, but I use findFragmentByTag, instead of findFragmentById. I have done everything I could find on Internet, but it says:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.filip.t1/com.example.filip.t1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

Here is my MainActivity:

public class MainActivity extends FragmentActivity
implements TextFragment.OnHeadlineSelectedListener{

EditText editText;
TextFragment fragment = new TextFragment();
Context context = this;
String fragment_tag = "FRAGMENTO";

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

    getSupportFragmentManager().beginTransaction().add(R.id.frameLayout, fragment, fragment_tag).commit();
    getSupportFragmentManager().executePendingTransactions();

    onArticleSelected(1);
}


@Override
public void onArticleSelected(int position) {

    TextFragment textFragment = (TextFragment) getSupportFragmentManager().findFragmentByTag(fragment_tag);

    if(textFragment != null){
        String strung = "It should be displayed, but it is not... :(";
        textFragment.updateText(strung);
    }else{
        Log.e("tag_prog","textFragment == null");
    }
}

And the Fragment:

public class TextFragment extends Fragment {

TextView textView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_text, container, false);
    textView = (TextView)rootView.findViewById(R.id.textView);
    return rootView;
}

OnHeadlineSelectedListener mCallBack;

public interface OnHeadlineSelectedListener {
    public void onArticleSelected(int position);
}

@Override
public void onResume(){
    super.onResume();
}

public void onAttach(Activity activity){
    super.onAttach(activity);

    try{
        mCallBack = (OnHeadlineSelectedListener) activity;
    } catch(ClassCastException e) {
        throw new ClassCastException(activity.toString() + "must implement OHSL");
    }
}

public void funkcja(){

}

public void updateText(String s) {
    textView.setText(s);
}

Thanks for any

The problem is that you are calling onArticleSelected(1) in onCreate() of your Activity. This happens before onCreateView() of your Fragment is called. Therefore textView is still null at this point.

Try calling onArticleSelected(1) in onResume() of your Activity.

See the lifecycle of your Activity and Fragment here

Basically, your textview object is null that's why that error is being shown. The reason could be that you are calling the updateText() method before the fragment has been fulling initialized. Try to call that method after 400ms have elapsed.

You can do this

    public void updateText(final String s) {
       if(textView!=null){
       textView.setText(s);
       }
else{
        new android.os.CountDownTimer(400, 400){

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                updateText(s);
            }

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub

            }}.start();
}

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM