2011-09-10 22 views
1

Android APIのFragmentLayoutに基づいて簡単なアプリケーションを作成しようとしています。単純なTextViewを変更するのではなく、Fragmentクラスを拡張して作成したクラスのグループを切り替えることができます。Androidアプリでフラグメントを動的に読み込む際のエラー

layout.xmlファイルに私のサンプルを静的に作成することができます.Fragmentは、私が望むように実行します。私はこの方法で作成されたフラグメントを置き換えることはできないと読んだことがあります。しかし、サンプルアプリケーションは、詳細テキストを動的にロードするので、関連性があるようです。主な違いは、ファイルからテキストをロードする新しい汎用フラグメントを作成したくないということです。クラスファイル全体を読み込み(フラグメントも自分自身であり、そこに独自のSurfaveViewを含む)したいと思います。ここ

<?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.fragment_to_switch_dynamicallyにR.id.processingを変更する参照してください?