簡體   English   中英

FindFragmentByTag返回null。

[英]FindFragmentByTag returns null.

我只是android開發的入門者,最近我在developer.android.com上啟動了Android教程。 到目前為止,一切似乎還不錯。 我在這門課上做得很出色http://developer.android.com/training/basics/fragments/communication.html ,但我陷入了困境。 我對其進行了一些更改,因為我想獲得更大的靈活性,因此我在運行時添加了片段。 我花了一個小時來尋找答案,但是找不到解決我問題的方法。 :(

這是我的問題:我想像本教程一樣與片段通信,但是我使用findFragmentByTag而不是findFragmentById。 我已盡我所能在Internet上找到的所有內容,但它說:

java.lang.RuntimeException:無法啟動活動ComponentInfo {com.example.filip.t1 / com.example.filip.t1.MainActivity}:java.lang.NullPointerException:嘗試調用虛擬方法'void android.widget.TextView。在空對象引用上的setText(java.lang.CharSequence)'

這是我的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");
    }
}

和片段:

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

謝謝你

問題在於您正在Activity的onCreate()中調用onArticleSelected(1) 這是在調用Fragment的onCreateView()之前發生的。 因此,此時textView仍為null。

嘗試在Activity的onResume()中調用onArticleSelected(1)

此處查看活動和片段的生命周期

基本上,您的textview對象為null,這就是顯示該錯誤的原因。 原因可能是您在片段初始化完成之前調用了updateText()方法。 經過400毫秒后嘗試調用該方法。

你可以這樣做

    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