2016-04-22 134 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帕特尔 – Ghost

+0

@ghost您可以发布错误日志? –

+0

这里是链接:https://onedrive.live.com/redir?resid=CDB5011499C666C7!422&authkey=!AIVQ_oh-JIvTbGM&ithint=file%2c for 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"/>