2014-02-22 39 views
1

我在一側有一個ListView,另一側有一個分段。我正在嘗試製作一個簡單的應用程序來了解碎片的工作原理。 這裏是我的代碼:爲什麼我的分段不工作

主要活動:

public class MainActivity extends Activity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // storing string resources into Array 
     // 
     setContentView(R.layout.activity_main); 
     String[] adobe_products = getResources().getStringArray(R.array.adobe_products); 
     //List view 
     final ListView list = (ListView)findViewById(R.id.questionsList); 
     ArrayAdapter<String> adapter; 
     adapter = new ArrayAdapter<String>(this, R.layout.list_item, adobe_products); 

     //final TextView text = (TextView)findViewById(R.id.textView1); 

     list.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 

       //String selectedFromList =(String) (list.getItemAtPosition(position)); 
       //text.setText(selectedFromList); 

      } 
     }); 

     list.setAdapter(adapter); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 


    @SuppressLint("ValidFragment") 
    public class MyFragment extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      // Inflate the layout for this fragment 
      return inflater.inflate(R.layout.activity_fragment, container, false); 
     } 
    } 

} 

主要活動XML佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <ListView 
     android:id="@+id/questionsList" 
     android:layout_width="144dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_weight="0.07" > 

    </ListView> 

    <fragment android:name="com.example.Layout.MyFragment" 
       android:id="@+id/article_fragment" 
       android:layout_weight="2" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" /> 



</LinearLayout> 

更新:感謝您的幫助,我注意到,該片段是子類,所以我將它包含在我的活動中。但仍然得到相同的結果。它停止,甚至不開放。無論如何,我更新了整個代碼logCat。

的logcat:

02-22 00:06:50.944: E/AndroidRuntime(2886): FATAL EXCEPTION: main 
02-22 00:06:50.944: E/AndroidRuntime(2886): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layout/com.example.layout.MainActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class fragment 
02-22 00:06:50.944: E/AndroidRuntime(2886):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
02-22 00:06:50.944: E/AndroidRuntime(2886):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
02-22 00:06:50.944: E/AndroidRuntime(2886):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 

任何想法,使其正常工作?

+0

+1適當的格式,即使你是新用戶:) – Kedarnath

+0

如果你的類'Fragment'旨在成爲片段,它應該擴展'android.app.Fragment',你應該重寫'onCreateView'。此外,請參閱此[android開發人員鏈接](http://developer.android.com/training/implementing-navigation/descendant.html#master-detail) – Vino

回答

0

我建議你去通過http://developer.android.com/guide/components/fragments.htmlhttp://developer.android.com/training/basics/fragments/creating.html

我在你的代碼中發現許多結構和設計問題,一旦你通過上面的鏈接,你會明白,還可以下載示例應用程序可在上面的鏈接頁面,如果你仍然無法做到,我一定會試着給出解釋示例代碼。祝你好運

進行以下更改代碼中的

1.add the fragment in activity layout like below and don't forget to replace YOUR PACKAGE NAME specified in class attribute 

<fragment 
     android:id="@+id/article_fragment" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     class="YOUR PACKAGE NAME.MyFragment" /> 

2. Create MyFragment as a separate Class (Should not be inside the Main Activity): 

3. Main Activity must extends FragmentAcivity instead of Actvity 

4. if you need Listview and fragment side by side change LinearLayout Orientation to android:orientation="horizontal" 

它將工作

+0

感謝您的幫助。 Android網站中的例子對我來說不是很清楚。無論如何,我盡我所能改變我的代碼,但仍然沒有運氣。請看看我的更新代碼。 – Dan

0

嘿ü忘了設置爲線性佈局的方向。默認情況下,它的水平方向只需添加方向屬性。

<LinearLayout 
    android:orientation="vertical" > 
</LinearLayout> 

在你的線性佈局標籤。

的錯誤充氣類片段只是改變

import android.app.Fragment; 
to: import android.support.v4.app.Fragment; 

使用onCreateView()設置佈局片段 - 系統調用這個當它的時間片段繪製它的用戶界面首次。要爲您的片段繪製UI,您必須從此方法返回一個視圖,該方法是片段佈局的根。如果片段不提供UI,則可以返回null。

+0

感謝您的幫助。我做了修改,仍然沒有工作。 :(請看看更新的代碼。 – Dan