[英]Get inflate ViewGroup of a layout with fragment
我实际上正在编写一个使用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>
然后,我在片段中异步加载地图。 当我尝试给activity_main视图充气时:
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来为应用程序设置一个Power Button KeyListener(类似于我在另一个没有片段的应用程序中所做的操作):
mainView.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_POWER)
{
//STUFF
}
}
});
谢谢您的帮助:D
名称空间必须在第一个视图中声明
<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>
您是否在清单文件中添加了以下行:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR_KEY" />
看看这个网站https://docs.google.com/document/pub?id=19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw
我几乎解决了这个问题。 我创建了一个自定义的SupportMapFragment类,并将片段动态加载到FrameLayout中。
activity_main.xml如下所示:
<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" >
<FrameLayout
android:id="@+id/mapPlaceholder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我现在可以通过代码为activity_main充气:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup mainView = (ViewGroup) inflater.inflate(R.layout.activity_main, null);
在此之后,我设置了侦听器(100%工作):
mainView.setFocusableInTouchMode(true);
mainView.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
//STUFF
}
});
我添加了这样的视图:
final WindowManager myWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
myWindowManager.addView(mainView, viewParams);
我不在onCreate的任何地方使用setContentView,所以这是第二个问题。 我需要通过FragmentTransaction将GoogleMap加载到FrameLayout中。
FragmentTransaction需要setContentView才能工作。 这段代码:
FrameLayout mapPlaceholder = (FrameLayout) mainView.findViewById(R.id.mapPlaceholder);
if(mapPlaceholder != null) {
mapFragment = GoogleMapFragment.newInstance();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(mapPlaceholder.getId(), mapFragment, GoogleMapFragment.class.getName());
ft.commit();
}
启动应用程序时返回此错误:
01-16 18:22:20.263: E/AndroidRuntime(7755): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070018 (com.example.app:id/mapPlaceholder) for fragment GoogleMapFragment{b666a7d #0 id=0x7f070018 com.example.app.GoogleMapFragment}
有谁知道如何在没有setContentView的情况下使用FragmentTransaction(或添加Fragment)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.