簡體   English   中英

為什么我的片段不起作用

[英]Why my fragment does not work

我的一側有一個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)

有什么想法可以使其正常工作嗎?

我建議您瀏覽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"

會工作的

嘿,您忘了設置線性布局的方向。 默認情況下,它是水平的,因此只需添加direction屬性。

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

在您的線性布局標簽中。

對於錯誤膨脹類片段只是改變

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

使用onCreateView()設置片段的布局-系統在片段第一次繪制其用戶界面時調用此方法。 要為片段繪制UI,必須從此方法返回一個View,它是片段布局的根。 如果片段不提供UI,則可以返回null。

暫無
暫無

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

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