如何获取java代码中的LinearLayout作为容器?
How to get LinearLayout in java code as container?
我正在使用 google 地图 API 进行小型项目。现在我想在用户单击该标记时在标记上方的容器中添加按钮。我发现我可以在地图上方添加新图层并手动创建上方标记。我创建了 main_map.xml 作为我的布局文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mapLayout">
<fragment 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:id="@+id/map" tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentBottom="false"
android:layout_alignParentRight="false"
class="com.example.project.MapFragment" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/container_popup"
android:layout_x="0dp"
android:layout_y="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</AbsoluteLayout>
所以我想在 MainActivity.java 中获取我的 LinearLayout 我尝试了以下代码
public class MainActivity extends FragmentActivity {
private static final String TAG = "myLogs";
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private static final int ANIMATION_DURATION = 500;
private LatLng trackedPosition;
//offset window that allows to correct position according to marker
private int popupXOffset;
private int popupYOffset;
private int markerHeight;
//listener that will update the offset when the window is resized
private ViewTreeObserver.OnGlobalLayoutListener infoWindowLayoutListener;
private View infoWindowContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map);
infoWindowContainer = findViewById(R.id.container_popup);
infoWindowLayoutListener = new InfoWindowLayoutListener();
infoWindowContainer.getViewTreeObserver().addOnGlobalLayoutListener(infoWindowLayoutListener );
setUpMapIfNeeded();
}
但问题是 infoWindowContainer 始终为空。我不明白如何正确处理。
LogCat:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geoalarm/com.example.geoalarm.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.geoalarm.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
使用 LinearLayout 而不是使用 View 然后像这样转换它
LinearLayout infoWindowPopup = (LinearLayout) findViewById(R.id.container_popup);
我正在使用 google 地图 API 进行小型项目。现在我想在用户单击该标记时在标记上方的容器中添加按钮。我发现我可以在地图上方添加新图层并手动创建上方标记。我创建了 main_map.xml 作为我的布局文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mapLayout">
<fragment 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:id="@+id/map" tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentBottom="false"
android:layout_alignParentRight="false"
class="com.example.project.MapFragment" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/container_popup"
android:layout_x="0dp"
android:layout_y="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</AbsoluteLayout>
所以我想在 MainActivity.java 中获取我的 LinearLayout 我尝试了以下代码
public class MainActivity extends FragmentActivity {
private static final String TAG = "myLogs";
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private static final int ANIMATION_DURATION = 500;
private LatLng trackedPosition;
//offset window that allows to correct position according to marker
private int popupXOffset;
private int popupYOffset;
private int markerHeight;
//listener that will update the offset when the window is resized
private ViewTreeObserver.OnGlobalLayoutListener infoWindowLayoutListener;
private View infoWindowContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map);
infoWindowContainer = findViewById(R.id.container_popup);
infoWindowLayoutListener = new InfoWindowLayoutListener();
infoWindowContainer.getViewTreeObserver().addOnGlobalLayoutListener(infoWindowLayoutListener );
setUpMapIfNeeded();
}
但问题是 infoWindowContainer 始终为空。我不明白如何正确处理。
LogCat:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geoalarm/com.example.geoalarm.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.geoalarm.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
使用 LinearLayout 而不是使用 View 然后像这样转换它
LinearLayout infoWindowPopup = (LinearLayout) findViewById(R.id.container_popup);