新的安卓版本中建立工程时候layout 中会生成两个xml文件一个activity_main.xml还有一个fragment_main.xml。
问题一:这两个XML之间的关系
可以看一下activity_main.xml就明白
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wapstore.Wap"
tools:ignore="MergeRootFrame" />
可以看出一个activity中是可以包含多个fragment的
详细了解可以看这里 http://blog.youkuaiyun.com/guolin_blog/article/details/8881711
问题二:但是这里会存在一个问题,比如在fragment_main.xml中定义了一个button,在activity中使用按键监听就会出现空指针问题,导致程序闪退
解决办法
方法一:activity.java中使用findviewbyid时候做一点改变
play = (Button) rootView.findViewById(R.id.play);
方法二:
1.删除fragment_main.xml整个文件
2.对activity_main.xml,删除里面的内容。然后切换到Graphy Layout,放入一个LinearLayout就可以
3.对MainActivity.java,可以删除部分的内容,再把MainActivity extends ActionBarActivity 改为MainActivity extends Activity :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
这样就可以随意使用控件了