简体   繁体   English

FindFragmentByTag返回null。

[英]FindFragmentByTag returns null.

I am just a begginer in android development, and recently I have started the Android Tutorial on developer.android.com. 我只是android开发的入门者,最近我在developer.android.com上启动了Android教程。 Everything seemed fine, until now. 到目前为止,一切似乎还不错。 I am doing exacly this course http://developer.android.com/training/basics/fragments/communicating.html , and i am stuck. 我在这门课上做得很出色http://developer.android.com/training/basics/fragments/communication.html ,但我陷入了困境。 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. 这是我的问题:我想像本教程一样与片段通信,但是我使用findFragmentByTag而不是findFragmentById。 I have done everything I could find on Internet, but it says: 我已尽我所能在Internet上找到的所有内容,但它说:

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 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.filip.t1 / com.example.filip.t1.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView。在空对象引用上的setText(java.lang.CharSequence)'

Here is my MainActivity: 这是我的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. 问题在于您正在Activity的onCreate()中调用onArticleSelected(1) This happens before onCreateView() of your Fragment is called. 这是在调用Fragment的onCreateView()之前发生的。 Therefore textView is still null at this point. 因此,此时textView仍为null。

Try calling onArticleSelected(1) in onResume() of your Activity. 尝试在Activity的onResume()中调用onArticleSelected(1)

See the lifecycle of your Activity and Fragment here 此处查看活动和片段的生命周期

Basically, your textview object is null that's why that error is being shown. 基本上,您的textview对象为null,这就是显示该错误的原因。 The reason could be that you are calling the updateText() method before the fragment has been fulling initialized. 原因可能是您在片段初始化完成之前调用了updateText()方法。 Try to call that method after 400ms have elapsed. 经过400毫秒后尝试调用该方法。

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();
}

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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