2014-02-21 129 views
0

我有一個片段事務不工作。我的活動(SearchActivity)顯示一個警告對話框片段,當用戶點擊yes或no(正面/負面)時,onClickListener在SearchActivity中實現。在重寫的方法中,我創建了一個片段事務來加載列表片段。但是,當我單擊對話框按鈕時,我的應用程序崩潰。錯誤日誌說有一個ID ...這不是一個ListView類。不過,我不知道這是從哪裏來的。我關於調用新片段的方式是否正確?任何建議將不勝感激。加載ListView片段失敗

搜尋活動覆蓋:

@Override 
public void onDialogClick(DialogFragment dialog) { 
    ResultListFragment rls = new ResultListFragment(); 
    FragmentTransaction ft = getFragmentManager().beginTransaction(); 
    ft.replace(R.id.searchLayout_container,rls); 
    ft.addToBackStack("search"); 
    ft.commit(); 
} 

對話片段:

public class MoreIngredientsDialogFragment extends DialogFragment { 
SearchActivity sActivity; 

public interface MoreIngredientsDialogListener{ 
    public void onDialogClick(DialogFragment dialog); 
} 

MoreIngredientsDialogListener mListener; 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity);ce 
    try { 
     mListener = (MoreIngredientsDialogListener) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() 
       + " must implement MoreIngredientsDialogListener"); 
    } 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setMessage("Do you have additional ingredient a?"); 
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      mListener.onDialogClick(MoreIngredientsDialogFragment.this); 
     } 
    }); 
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      mListener.onDialogClick(MoreIngredientsDialogFragment.this); 
     } 
    }); 
    return builder.create(); 
} 
} 

列表片段(ResultListFragment):

public class ResultListFragment extends ListFragment { 
String[] menuItems = {"beef", "chicken", "pie"}; 
@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    setListAdapter(new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_list_item_1, menuItems)); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.activity_result, container, false); 
    return view; 
} 
} 

錯誤日誌:

02-20 12:29:57.381 2114-2114/com.ygutstein.neiumasterproject E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class 
     at android.app.ListFragment.ensureList(ListFragment.java:402) 
     at android.app.ListFragment.onViewCreated(ListFragment.java:203) 
     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:842) 
     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) 
     at android.app.BackStackRecord.run(BackStackRecord.java:622) 
     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) 
     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) 
     at android.os.Handler.handleCallback(Handler.java:605) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4424) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
      at dalvik.system.NativeStart.main(Native Method) 

02-20 12:29:57.421  873-1092/system_process W/ActivityManager﹕ Force finishing activity com.ygutstein.neiumasterproject/.SearchActivity 
    02-20 12:29:57.431  873-1092/system_process W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21020 

回答

0

原來我需要在我的xml文件中更改我的ListView。在這裏看到了答案:android.R.id.list not ListView

原名:

android:id="@+:id/listView"` 

新:

android:id="@android:id/list"