2016-02-26 30 views
0

我只是一個在android開發中的begginer,最近我已經在developer.android.com上啓動了Android教程。一切似乎都很好,直到現在。我正在做這個課程http://developer.android.com/training/basics/fragments/communicating.html exacly,我卡住了。我改變了一下,因爲我想要更多的靈活性,所以我添加了我的片段運行時。我花了一個小時尋找答案,但無法找到解決我的問題的任何解決方案。 :(FindFragmentByTag返回null。

這裏是我的問題: 我想就像在教程,我的片段進行溝通,但我用findFragmentByTag,而不是findFragmentById我所做的一切,我能找到在互聯網上,但它說:

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

感謝任何

+0

在你的'TextFragment.onCreateView'你不應該回到你的'textView'? – aribeiro

+0

我應該嗎?我真的不知道。但爲什麼 ? – buRn

+0

不,你不應該。 – barq

回答

1

的問題是,你在你活動的onCreate()調用onArticleSelected(1)。這發生在您的Fragment的onCreateView()被調用之前。因此textView在這一點上仍然是空的。

嘗試在您的活動的onResume()中調用onArticleSelected(1)

看到你的動態的生命週期和碎片here

+0

有。當我在TextFragment中調用函數updateText()時,它會更改文本。 – buRn

+0

顯示您的fragment_text.xml並從onCreateView()中刪除@Nullable。看到我更新的答案。 – barq

+0

不能編輯問題,一些奇怪的錯誤。有我的TextFragment.xml:http://pastebin.com/a6XkpyCP。編輯@Nullable,仍然錯誤。 – buRn

0

基本上,你的TextView的對象爲null,這就是爲什麼被證明錯誤。原因可能是您在片段完成初始化之前調用updateText()方法。嘗試在400ms過後調用該方法。

你可以做到這一點

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

    } 
+0

必須去,但稍後會檢查它。 :) – buRn