fragment使用注意

来源:互联网 发布:@@error mysql 编辑:程序博客网 时间:2024/06/04 21:53

<span style="font-size:48px;color:#ff0000;"><strong>一</strong></span>
用的是android.app中的fragment

一种是在xml中直接写明name

如:

<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:orientation="vertical"    tools:context="com.example.myonpencvtestone.load.ContentActivity" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:background="@drawable/border_content_bg"        android:orientation="horizontal" >        <Button            android:id="@+id/add"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:layout_marginLeft="24dp"            android:layout_marginRight="24dp"            android:layout_marginTop="12dp"            android:layout_marginBottom="12dp"            android:text="dsfdf" />        <Button            android:id="@+id/judg"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:layout_marginLeft="24dp"            android:layout_marginRight="24dp"            android:layout_marginTop="12dp"            android:layout_marginBottom="12dp"            android:text="dsfdf" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="3"         android:layout_marginTop="10dp"        android:layout_marginBottom="10dp"        android:background="@drawable/border_content_bg"        >        <fragment             android:name="com.example.myonpencvtestone.load.fragment.CameraFragment"            android:id="@+id/camera_fragment"            android:layout_weight="2"            android:layout_width="match_parent"            android:layout_height="match_parent"            />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="2"         android:background="@drawable/border_content_bg"        >        <Button            android:id="@+id/adsf"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:layout_marginLeft="24dp"            android:layout_marginRight="24dp"            android:layout_marginTop="12dp"            android:layout_marginBottom="12dp"            android:text="dsfdf" />    </LinearLayout></LinearLayout>


只需要这样就可以显示了,

注意可能会提示这个错误

 Caused by: java.lang.IllegalStateException: Fragment com.example.myonpencvtestone.load.fragment.CameraFragment did not create a view.
 Caused by: android.view.InflateException: Binary XML file line #46: Error inflating class fragment
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myonpencvtestone/com.example.myonpencvtestone.load.ContentActivity}: android.view.InflateException: Binary XML file line #46: Error inflating class fragment


这个是因为没有在fragment中的oncreateview中指明fragment显示的是哪个layout

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stub//return super.onCreateView(inflater, container, savedInstanceState);return inflater.inflate(R.layout.camera_fragment, container,false);}

这样就行了



在代码中添加

package com.example.myonpencvtestone.load;import com.example.myonpencvtestone.R;import com.example.myonpencvtestone.R.id;import com.example.myonpencvtestone.R.layout;import com.example.myonpencvtestone.R.menu;import com.example.myonpencvtestone.load.fragment.CameraFragment;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class ContentActivity extends Activity {<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>protected void onCreate(Bundle savedInstanceState) {<span style="white-space:pre"></span>super.onCreate(savedInstanceState);<span style="white-space:pre"></span>setContentView(R.layout.activity_content);<span style="white-space:pre"></span>//<span style="white-space:pre"></span>//在程序中加入Fragment        FragmentManager fragmentManager = getFragmentManager();        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();                CameraFragment fragment = new CameraFragment();        fragmentTransaction.add(R.id.camera_fragment, fragment);        fragmentTransaction.commit();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public boolean onCreateOptionsMenu(Menu menu) {<span style="white-space:pre"></span>// Inflate the menu; this adds items to the action bar if it is present.<span style="white-space:pre"></span>getMenuInflater().inflate(R.menu.content, menu);<span style="white-space:pre"></span>return true;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public boolean onOptionsItemSelected(MenuItem item) {<span style="white-space:pre"></span>// Handle action bar item clicks here. The action bar will<span style="white-space:pre"></span>// automatically handle clicks on the Home/Up button, so long<span style="white-space:pre"></span>// as you specify a parent activity in AndroidManifest.xml.<span style="white-space:pre"></span>int id = item.getItemId();<span style="white-space:pre"></span>if (id == R.id.action_settings) {<span style="white-space:pre"></span>return true;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return super.onOptionsItemSelected(item);<span style="white-space:pre"></span>}}


        <FrameLayout             android:id="@+id/camera_fragment"            android:layout_weight="2"            android:layout_width="match_parent"            android:layout_height="match_parent"            />














0 0