2012-12-07 116 views
1

我有兩個片段。我使用的接口通信的一個片段與另一:片段之間的通信android

public class FragmentLanguages extends ListFragment { 
LanguageArrayAdapter adapter; 
OnLanguageChangedListener mCallback; 

public interface OnLanguageChangedListener { 
    public void onLanguageSelected(int position); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.languages, null); 

    return view; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    Language languages[] = new Language[] { 
      new Language(R.drawable.ic_flag_german, "German"), 
      new Language(R.drawable.ic_flag_spanish, "Spanish"), 
      new Language(R.drawable.ic_flag_russian, "Russian"), 
      new Language(R.drawable.ic_flag_ukrainian, "Ukrainian") }; 

    adapter = new LanguageArrayAdapter(getActivity(), R.layout.list_row, 
      languages); 
    getListView().setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      adapter.setSelectedItem(position); 
      mCallback.onLanguageSelected(position + 1); 
     } 
    }); 
    setListAdapter(adapter); 
} 

public static class Language { 
    public int image; 
    public String text; 

    public Language() { 
     super(); 
    } 

    public Language(int image, String text) { 
     super(); 
     this.image = image; 
     this.text = text; 
    } 
} 

public void setOnLanguageChangedListener(OnLanguageChangedListener mCallback) { 
    this.mCallback = mCallback; 
} 

}

和另一

public class FragmentCircles extends Fragment implements OnClickListener, 
    OnLanguageChangedListener { ... 
..... 
    @Override 
public void onLanguageSelected(int position) { 
    currentLang = position; 
    translationCircle.removeAllViews(); 
    String strSelect = "SELECT Translation FROM Translation WHERE Lang_Id=" 
      + currentLang + " ORDER BY Word_Id ASC"; 
    translationList = DataBaseHelper.getInstance(getActivity()) 
      .SelectTranslationList(strSelect); 
    Log.i("Lang ", "changed " + currentLang); 


    calculateCentreTranslation(lLabelRadius, width, height); 
} 

但屏幕旋轉後,當我在ListFragment選擇一些項目它給了我一個錯誤

12-07 19:06:15.163: E/AndroidRuntime(436): java.lang.NullPointerException 

at line

   mCallback.onLanguageSelected(position + 1); 

看起來mCallback爲空。但我如何解決它?

+0

你在哪裏調用setOnLanguageChangedListener? –

+0

我在HTC上看到,旋轉後,類方法OnCreate被調用,重置所有我的值。你可以嘗試使它成爲靜態的,但更好地測試娛樂是否發生並在構造函數中執行。 – dmaij

回答

0

在android中,當您從縱向切換到橫向或者反向時,活動被重新創建,所以如果您想保留一些數據,您必須使用Bundle saveInstanceState管理這些數據。

1

您的FragmentLanguages必須通知您的FragmentActivity依次通知其他片段。這確保沒有片段鏈接在一起,如果其中一個在另一個通知它時沒有實例化,則可以觸發NullPointerExceptions

所以,你的界面OnLanguageChangedListener停留在你的LanguagesFragment

FragmentActivity必須實現setOnLanguageChangedListener

FragmentLanguages

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    onLanguageChangedListener = (onLanguageChangedListener) activity; 
} 

由於onAttach()被稱爲甚至在onCreate()你可以在你的片段使用只要你想要:

onLanguageChangedListener.onLanguageChanged(...); 

在你FragmentActivity它實現OnLanguageChangedListener您需要重寫此:

@Override 
public void onLanguageChanged(...) { 
    // here you implement the logic to notify other fragments 
}