2016-04-22 3 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); 
    } 
} 

FragmentTwo: -

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 
    android:id="@+id/fragment_place" 
    android:name="com.test.FragmentTwo" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

С

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

Как статический Fragment не может быть заменить во время выполнения. Но вы можете добавить или заменить фрагмент в FrameLayout.

+0

но теперь она дает java.lang.RuntimeException: Не удается запустить активность @Dhaval Patel – 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"/> 
Смежные вопросы