我实际上正在编写一个使用 Google Maps API 的 Android 应用程序。
布局activity_main.xml如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:mapType="normal"
map:uiCompass="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true" />
</LinearLayout>
我在 fragment 中异步加载 map 。当我尝试膨胀 Activity_main View 时:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup mainView = (ViewGroup) inflater.inflate(R.layout.activity_main, null);
应用程序崩溃并返回错误:
01-15 19:25:16.045: E/AndroidRuntime(21115): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
如何解决?我需要这个 ViewGroup 来为应用程序设置一个电源按钮 KeyListener(类似于我在另一个没有 fragment 的应用程序中所做的操作):
mainView.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_POWER)
{
//STUFF
}
}
});
谢谢你的帮助:D
最佳答案
命名空间必须在第一个 View 中声明
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:mapType="normal"
map:uiCompass="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true" />
</LinearLayout>
关于java - 获取带有 fragment 的布局的 inflate ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27970506/