2012-09-22 150 views
0

我試圖做一個簡單的片段示例,其中一個片段將顯示一個文章列表,另一個將顯示詳細的文章。 這是我的主要活動接收機類片段不顯示

public class ArticleFragment extends Activity{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     }  
}// end class 

這是主要的佈局文件 -

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <fragment android:name="com.example.ArticleList" 
      android:id="@+id/list" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 

    <fragment android:name="com.example.ArticleDetails" 
      android:id="@+id/viewer" 
      android:layout_weight="2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
</LinearLayout> 

這是我ArticleList兩個碎片類和ArticleDetails

public class ArticleList extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    return inflater.inflate(R.layout.articlelist, container, false); 
     } 

public class ArticleDetails extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.articledetails, container, false); 
    } 
    } 

我也有兩個ArticleList,ArticleDetails片段的XML佈局文件(包含textview)。但該應用程序已停止工作。我在這裏錯過了什麼?請幫助thanx。

+0

你是什麼意思〜「該應用程序已停止工作」?你有沒有在onCreateView方法中放置斷點並調試它們? –

回答

0

檢查你的包名稱裏面的代碼

<fragment android:name="com.abc.def............./> 

.Thanks你,我知道我們可以做到這一點。

+0

但是你是否認爲其他一切都好,意思是代碼..我的分SDK版本是api-11 – Tanvir

+0

你的軟件包名稱是什麼? – MGDroid

+0

UPPPSSS ...我的包名是com.example.articlefragment,所以我相應地改變了類名。 com.example.articlefragment.MYCLASSNAME,它的工作。順便說一句,在另一個例子中,它使用片段標記中的class =「com.example.MyclassName」來引用該類。有什麼區別? THANX A LOT – Tanvir