簡體   English   中英

當我運行Fragment項目時,eclipse顯示錯誤。 我不知道怎么解決

[英]When i run my Fragment project, eclipse shows errors. I don't know how to fix it

這是我嘗試仔細遵循的說明:

http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/

這是Logcat中出現的第一個錯誤:

E / MoreInfoHPW_ViewGroup(21404):父視圖不是TextView

E / AndroidRuntime(21404):致命異常:主要

E / AndroidRuntime(21404):進程:com.example.fragmentstest,PID:21404

E / AndroidRuntime(21404):java.lang.RuntimeException:無法啟動活動ComponentInfo {com.example.fragmentstest / com.example.fragmentstest.MainActivity}:android.view.InflateException:二進制XML文件行#20:錯誤誇大類分段

這是我的MainActivity和activity_main代碼:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void selectFrag(View view){
    Fragment fr;
    if (view == findViewById(R.id.button2)){
        fr = new FragmentTwo();
    } else {
        fr = new FragmentOne();
    }
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_place, fr);
    fragmentTransaction.commit();


}  




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

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fragment No.1"
    android:onClick="selectFrag" />

<Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fragment No.2" 
    android:onClick="selectFrag"/>

<fragment 
    android:name="com.example.fragmentstest.FragmentOne"
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>"

</LinearLayout>

我終於設法找到錯誤原因。 這是一個邏輯錯誤(很難找到)。 在FragmentOne.java和FragmentTwo.java中,我都返回activity_main.xml而不是fragment_one.xml和fragment_two.xml作為源布局。 很抱歉,如果問題不在於我在問題正文中提供的信息。 但至少現在我知道更多!

這是示例代碼:

   public class FragmentTwo extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater,
   ViewGroup container, Bundle savedInstanceState) {

   //I had to use fragment_one instead of activity_main.
   //Such simple mistake took about two days of my time.

  return inflater.inflate(
          R.layout.activity_main, container, false);
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM