2013-07-21 84 views
1

嘗試設置自定義偵聽器,以便將來自Activity的字符串發送到該活動內部的片段。按下活動按鈕,文本字符串應發送到片段並顯示在該片段的文本視圖中,但我得到一個classCastException,不知道該怎麼做。你如何得到這個工作?設置偵聽器接口時出現ClassCastException

錯誤是在這條線

listener = (OnStringRequestedListener) fragment; 

我不知道如何設置這個自定義監聽器接口,而不該行代碼。我使用了類似的代碼行進入相反的方向,它工作。那是或從片段發送消息到活動。但是我試圖今天不工作

堆棧跟蹤

Caused by: java.lang.ClassCastException: 
com.example.asynctaskprogressbarexample.InterfaceActivityToFragmentFragment 
cannot be cast to com.example.asynctaskprogressbarexample 
.InterfaceActivityToFragment$OnStringRequestedListener 
at com.example.asynctaskprogressbarexample 
.InterfaceActivityToFragment.onCreate(InterfaceActivityToFragment.java:27) 

活動類

public class InterfaceActivityToFragment extends Activity { 

Button buttonOne; 
private OnStringRequestedListener listener; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.interface_activity_to_fragment); 

    Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment1); 

    listener = (OnStringRequestedListener) fragment; // <-- ERROR ON THIS LINE 

    if (fragment instanceof OnStringRequestedListener) { 
      listener = (OnStringRequestedListener) fragment; 
     } else { 
      throw new ClassCastException(fragment.toString() 
       + " must implemenet OnTimeRequestedListener"); 
     } 

    buttonOne = (Button) findViewById(R.id.button1); 

    buttonOne.setOnClickListener(new Button.OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
      // TODO Auto-generated method stub 
       listener.passString("string from Activity"); 

      } 

     }); 

} 

public interface OnStringRequestedListener { 
    public void passString(String sendNumberString); 

    } 

} 

片段類

public class InterfaceActivityToFragmentFragment extends Fragment implements FragmentHeadlessAsyncTaskFragment.OnStringRequestedListener { 

TextView textViewOne; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 

    View view = inflater.inflate(R.layout.interface_activity_to_fragment_fragment, container, false); 

    TextView textViewOne = (TextView) view.findViewById(R.id.textView1); 

    return view; 
} // end on create view 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    setRetainInstance(true); 
} 

@Override 
public void passString(String stringFromActivity) { 
    textViewOne.setText(stringFromActivity); 

} 
    } 
+0

而不是'InterfaceActivityToFragment extends Activity'你試過把類擴展到'FragmentActivity'嗎?即「InterfaceActivityToFragment擴展FragmentActivity」?你也試過使用'getSupportFragmentManager()'而不是'getFragmentManager()'?看看它有什麼區別? –

+0

還沒有嘗試過,但我認爲FragmentActivity是V4支持pre api level 11的舊版本以便與碎片一起使用。因爲我的目標API是11以上。我還沒有想過使用它。我現在會嘗試 – Kevik

+0

實際上,這將比我想象的要困難,我需要更改應用程序並添加V4支持才能使用FragmentActivity – Kevik

回答

3

要實現FragmentHeadlessAsyncTaskFragment.OnStringRequestedListener但鑄造InterfaceActivityToFragment.OnStringRequestedListener

定義爲嵌套在兩個不同類中的相同接口。

+0

我不敢相信我沒有看到,很好的抓住 – Kevik

相關問題