2016-04-22 15 views
3

GPSが有効かどうかによって異なるフラグメントを表示しようとしています。しかし、私はこのエラーを取得しています: -フラグメントを変更する

Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0e00a1 

これを行う方法を私に教えてください:ここでは

は私のMainActivityクラスです:

public class MainActivity extends Activity { 

Fragment fr; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     fr = new FragmentTwo(); 
     getFragment(); 
    } else { 
     fr = new FragmentOne(); 
     getFragment(); 
    } 
} 

public void getFragment() { 
    FragmentManager fm = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
    fragmentTransaction.replace(R.id.fragment_place, fr); 
    fragmentTransaction.commit(); 
    } 
} 

FragmentOne: -

public class FragmentOne extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_one, container, false); 
    } 
} 

フラグメント2: -

public class FragmentTwo extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_two, container, false); 
    } 
} 

マイactivity_main: -

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 
<fragment 
    android:id="@+id/fragment_place" 
    android:name="com.test.FragmentTwo" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 
</LinearLayout> 

そしてfragment_one.xmlとfragment_two.xmlが同じであるだけで別のテキスト: - 事前に

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="GPS IS ENABLE"/> 
</RelativeLayout> 

感謝。

答えて

1

静的Fragmentとして

<FrameLayout 
    android:id="@+id/fragment_place" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<fragment 
    android:id="@+id/fragment_place" 
    android:name="com.test.FragmentTwo" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

を交換して、実行時に置き換えることはできません。しかし、FrameLayoutにフラグメントを追加または置き換えることができます。

+0

この行を削除することができますが、今ではjava.lang.RuntimeExceptionを与える:アクティビティ@Dhavalパテル – Ghost

+0

@ghostを開始することができません、あなたは、エラーログを投稿することができますか? –

+0

ここにリンクがあります: - https://onedrive.live.com/redir?resid=CDB5011499C666C7!422&authkey=!AIVQ_oh-JIvTbGM&ithint=file%2c logcatファイルの場合@Dhaval Patel – Ghost

0

あなたは

android:name="com.test.FragmentTwo" 

<fragment 
    android:id="@+id/fragment_place" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 
関連する問題