2011-09-10 101 views
1

我正在嘗試基於Android API FragmentLayout製作一個簡單的應用程序。我不想改變一個簡單的TextView,而是希望能夠在我創建的一組類之間進行切換,這些類擴展了Fragment類。在Android應用程序中動態加載片段時出錯

我可以在我的layout.xml文件中靜態創建任何示例,並且Fragment按照我的想法運行。我已經讀過,您無法替換以此方式創建的碎片,因此示例應用似乎相關,因爲它會動態加載詳細信息文本。主要區別在於我不想創建一個新的通用片段,其中我從文件加載一些文本,我想加載整個類文件(同樣,它們本身是片段,幷包含自己的SurfaveViews)。

這裏是我的佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <fragment class="com.android.app.Examples$TitlesFragment" 
     android:id="@+id/titles" android:layout_weight="1" 
     android:layout_width="0px" android:layout_height="match_parent" /> 

    <FrameLayout android:id="@+id/fragment_to_switch_dynamically" android:layout_weight="1" 
     android:layout_width="0px" android:layout_height="match_parent" 
     android:background="?android:attr/detailsElementBackground" /> 

</LinearLayout> 

,這裏是從活動相關的方法,我試圖創建和更新

void showDetails(int index) { 
     mCurCheckPosition = index; 

     if (mDualPane) { 
      // We can display everything in-place with fragments, so update 
      // the list to highlight the selected item and show the data. 
      getListView().setItemChecked(index, true); 

      if (mShownCheckPosition != mCurCheckPosition) { 
       // If we are not currently showing a fragment for the new 
       // position, we need to create and install a new one. 
       PApplet pa = getProcessingSketchByIndex(index); 

       // Execute a transaction, replacing any existing fragment 
       // with this one inside the frame. 
       FragmentTransaction ft = getFragmentManager() 
         .beginTransaction(); 
       ft.replace(R.id.processing, pa); 
       ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
       ft.commit(); 
       mShownCheckPosition = index; 
      } 

     } else { 
      // Otherwise we need to launch a new activity to display 
      // the dialog fragment with selected text. 
      // Intent intent = new Intent(); 
      // intent.setClass(getActivity(), DetailsActivity.class); 
      // intent.putExtra("index", index); 
      // startActivity(intent); 
     } 
    } 

    private PApplet getProcessingSketchByIndex(int index) { 
     PApplet pa = null; 
     switch (index) { 
     case 0: 
      pa = new Lights1(); 
      break; 
     case 1: 
      pa = new Lights2(); 
      break; 
     case 2: 
      pa = new Reflection(); 
      break; 
     case 3: 
      pa = new Rotate(); 
      break; 
     case 4: 
      pa = new Spot(); 
      break; 
     case 5: 
      break; 
     default: 
      break; 
     } 
     return pa; 
    } 
} 

一切似乎是想罰款,然後我得到這個錯誤 :

E/AndroidRuntime(31393): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.processing.app/com.android.processing.app.ProcessingExamples}: java.lang.IllegalArgumentException: No view found for id 0x7f050001 for fragment Lights1{407bf678 #1 id=0x7f050001} 
    E/AndroidRuntime(31393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815) 
    E/AndroidRuntime(31393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831) 
    E/AndroidRuntime(31393): at android.app.ActivityThread.access$500(ActivityThread.java:122) 
    E/AndroidRuntime(31393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024) 
    E/AndroidRuntime(31393): at android.os.Handler.dispatchMessage(Handler.java:99) 
    E/AndroidRuntime(31393): at android.os.Looper.loop(Looper.java:132) 
    E/AndroidRuntime(31393): at android.app.ActivityThread.main(ActivityThread.java:4123) 
    E/AndroidRuntime(31393): at java.lang.reflect.Method.invokeNative(Native Method) 
    E/AndroidRuntime(31393): at java.lang.reflect.Method.invoke(Method.java:491) 
    E/AndroidRuntime(31393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
    E/AndroidRuntime(31393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
    E/AndroidRuntime(31393): at dalvik.system.NativeStart.main(Native Method) 
    E/AndroidRuntime(31393): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f050001 for fragment Lights1{407bf678 #1 id=0x7f050001} 
    E/AndroidRuntime(31393): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:770) 
    E/AndroidRuntime(31393): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:977) 
    E/AndroidRuntime(31393): at android.app.BackStackRecord.run(BackStackRecord.java:638) 
    E/AndroidRuntime(31393): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1309) 
    E/AndroidRuntime(31393): at android.app.Activity.performStart(Activity.java:4406) 
    E/AndroidRuntime(31393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1788) 
    E/AndroidRuntime(31393): ... 11 more 

在此先感謝您的幫助

+0

您的Lights1代碼在哪裏?好像你需要傳遞一個論點。 – doNotCheckMyBlog

回答

1

也許是因爲'R.id.processing'在視圖的XML中不存在。

在你的XML我看到

<FrameLayout android:id="@+id/fragment_to_switch_dynamically"> 

所以,也許改變R.id.processing到R.id.fragment_to_switch_dynamically?

相關問題