2016-04-09 1 views
-1

Androidでプログラミングする方法を学習していて、フラグメントの変更に関するいくつかの問題があります。ここで見つけたソリューションのほとんどすべてを試しましたが、動作していません。ここでフラグメントを動的に変更しています - java.lang.IllegalArgumentException:idのビューが見つかりません

私は画面に表示しようとしている私のレイアウトです:

fragment_result.xml

<?xml version="1.0" encoding="utf-8"?> 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" tools:context="com.example.ic.projeto_final.ResultFragment"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/resultView"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight=".90" 
       android:gravity="center" 
       android:orientation="horizontal" > 

       <Button 
        android:layout_width="0dp" 
        android:layout_weight=".10" 
        android:layout_height="wrap_content" 
        android:text="setaesq - pegar cod ascii" 
        android:id="@+id/btnPrevious" 
       /> 

       <LinearLayout 
        android:layout_width="0dp" 
        android:layout_weight=".80" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="15dp" 
         android:textAlignment="center" 
         android:textSize="25dp" 
         android:id="@+id/fileName" > 
        </TextView> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="15dp" 
         android:textAlignment="center" 
         android:textSize="25dp" 
         android:id="@+id/fileDistance" > 
        </TextView> 

        <ImageView 
         android:layout_marginTop="15dp" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:id="@+id/fileImage" 
        /> 

      </LinearLayout> 

      <Button 
       android:layout_width="0dp" 
       android:layout_weight="0.1" 
       android:layout_height="wrap_content" 
       android:text="seta dir pegar cod ascii" 
       android:id="@+id/btnNext" 
       /> 

     </LinearLayout> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Fazer nova consulta" 
      android:layout_gravity="center_horizontal" 
      android:layout_alignParentBottom="true" 
      android:id="@+id/btnNovaConsulta"/> 
    </LinearLayout> 

</FrameLayout> 

およびクラス関連:

ResultFragment.java

public class ResultFragment extends Fragment { 

public ResultFragment() { 

} 

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

    View rootView = inflater.inflate(R.layout.fragment_result, container, false);  

    TextView fileName = (TextView) rootView.findViewById(R.id.fileName); 
    TextView fileDistance = (TextView) rootView.findViewById(R.id.fileDistance); 

    ImageView imageName = (ImageView) rootView.findViewById(R.id.fileImage); 

    Button btnPrevious = (Button) rootView.findViewById(R.id.btnPrevious); 
    Button btnNext = (Button) rootView.findViewById(R.id.btnNext); 

    return rootView; 
} 

}

そして、これが表示されたフラグメントを変更しようとする私のmainActivityで一部です:

MainActivityFragment.java

btnConsulta.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 


     /* Others pieces of code that not affect this part */ 

       ResultFragment result = new ResultFragment(); 
       FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
       transaction.replace(R.id.resultView, result); 
       transaction.addToBackStack(null); 
       transaction.commit(); 
      } 
     } 

現在、私のlogcatに表示されるエラーは次のとおりです。

java.lang.IllegalArgumentException: No view found for id 0x7f0a000f (com.example.ic.projeto_final:id/resultView) for fragment ResultFragment{143a9831 #0 id=0x7f0a000f} 
     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886) 
     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
     at android.app.BackStackRecord.run(BackStackRecord.java:834) 
     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) 
     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5257) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

また、getChildFram()の代わりにgetFragmentManager entManager()は、idをfragment_result.xml内のFrameLayoutに関連付けますが、それも機能しません。私はこの問題を解決するために表示する必要があることが欠けているものがある場合は

、ちょうど私がそれに入れて、あなたの主な活動のXMLファイルに

+0

は 'resultViewのように思えますあなたの作品の中にあります。*あなたの活動ではありません。 –

+0

あなたがこれを指摘した後、これをどう扱うか理解し始めています、ありがとう。私はいくつかのテストをしており、それはほとんど動作しています。私の最後の問題は、古いXMLがまだバックグラウンドで表示されていることです。 – Alexandre

答えて

0

をお知らせこの:

< FrameLayout 
android:id = "@+id/resultView" 
android:layout_width = "wrap_content" 
android_layout_hight = "match_parent" 
/> 
+0

私は今あなたの考えを試しましたが、問題は依然として続きます – Alexandre

+0

resultViewのタイプは何ですか? FrameLayoutですか? resultViewを作成しましたか?主な活動のxmlに作成する必要があります。私のコードを更新しました –

+0

主な活動のXML外でした。私はDavidの解説を読み、私はこれを理解し始めました。私はあなたと同じことをやってほとんど仕事をしましたが、古いXMLはバックグラウンドでまだ見えています。 – Alexandre

関連する問題