在Android Main Activity中添加一个包含Google Maps API V2的片段
我一直试图通过扩大片段中的地图布局,然后尝试通过片段管理器将其传递给活动,来在自己的主要活动中插入地图.我收到了致命异常,并且我不确定我要执行的操作是否可行.
I have been trying to insert a Map in my main activity by inflating the map layout in the fragment and then trying to pass it to the activity through fragment manager. I am getting a Fatal Exception and I am not quite sure if what I am trying to do is doable.
MainActivity:
MainActivity:
package com.example;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
private Fragment mMapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
mMapFragment = fm.findFragmentById(R.id.container);
if (savedInstanceState == null) {
fm.beginTransaction()
.add(R.id.container, mMapFragment)
.commit();
}
}
}
这是我的片段:
package com.example;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.SupportMapFragment;
public class RunMapFragment extends SupportMapFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
Activity_main(使用容器作为ID)只是一个简单的FrameLayout,而fragment_main是您在下面找到的内容:
Activity_main (with container as ID) is just a simple FrameLayout, while fragment_main is what you find below:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
可能只是因为我一直在玩这些代码,所以会胡说八道.谢谢.
There might be a few nonsense imports, simply because I have been playing with the code. Thanks.
LOGCAT:
06-26 14:01:42.674: E/AndroidRuntime(6942): FATAL EXCEPTION: main
06-26 14:01:42.674: E/AndroidRuntime(6942): Process: com.example, PID: 6942
06-26 14:01:42.674: E/AndroidRuntime(6942): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.NullPointerException
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.os.Handler.dispatchMessage(Handler.java:102)
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.os.Looper.loop(Looper.java:136)
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.app.ActivityThread.main(ActivityThread.java:5001)
06-26 14:01:42.674: E/AndroidRuntime(6942): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 14:01:42.674: E/AndroidRuntime(6942): at java.lang.reflect.Method.invoke(Method.java:515)
06-26 14:01:42.674: E/AndroidRuntime(6942): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-26 14:01:42.674: E/AndroidRuntime(6942): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-26 14:01:42.674: E/AndroidRuntime(6942): at dalvik.system.NativeStart.main(Native Method)
06-26 14:01:42.674: E/AndroidRuntime(6942): Caused by: java.lang.NullPointerException
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:384)
06-26 14:01:42.674: E/AndroidRuntime(6942): at com.example.MainActivity.onCreate(MainActivity.java:24)
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.app.Activity.performCreate(Activity.java:5231)
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-26 14:01:42.674: E/AndroidRuntime(6942): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
已解决: 我已经从xml中删除了片段,将其插入到活动xml中,并将onCreateView(来自片段类)传递给超类.
SOLVED: I have removed the fragment from the xml, inserted it in the activity xml and passed onCreateView (from the fragment class) to the superclass.
更简单的方法是按以下方式指定activity_main.xml布局:
An easier way to do this would be to specify your activity_main.xml layout as follows:
<FrameLayout ...>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.RunMapFragment"/>
</FrameLayout>
并删除该片段中的onCreateView()
.