在我的Eclipse中,我创建了一个新的应用程序。
出于测试目的,我在XML中放置了Fragment
并保持代码不变。但我的应用程序仍在崩溃。我尝试过其他问题的帮助,但每个人都在做一些事情,比如切换标签和改变导航。
到目前为止,我只想要一个空的Map Fragment。甚至不想显示地图。
public class MainActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
03-07 01:0
8:54.174: E/AndroidRuntime(6687): FATAL EXCEPTION: main
03-07 01:08:54.174: E/AndroidRuntime(6687): Process: com.example.testtt, PID: 6687
03-07 01:08:54.174: E/AndroidRuntime(6687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testtt/com.example.testtt.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.ActivityThread.access$800(ActivityThread.java:139)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.os.Handler.dispatchMessage(Handler.java:102)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.os.Looper.loop(Looper.java:136)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.ActivityThread.main(ActivityThread.java:5105)
03-07 01:08:54.174: E/AndroidRuntime(6687): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 01:08:54.174: E/AndroidRuntime(6687): at java.lang.reflect.Method.invoke(Method.java:515)
03-07 01:08:54.174: E/AndroidRuntime(6687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
03-07 01:08:54.174: E/AndroidRuntime(6687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
03-07 01:08:54.174: E/AndroidRuntime(6687): at dalvik.system.NativeStart.main(Native Method)
03-07 01:08:54.174: E/AndroidRuntime(6687): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-07 01:08:54.174: E/AndroidRuntime(6687): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:315)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.Activity.setContentView(Activity.java:1973)
03-07 01:08:54.174: E/AndroidRuntime(6687): at com.example.testtt.MainActivity.onCreate(MainActivity.java:14)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.Activity.performCreate(Activity.java:5275)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
03-07 01:08:54.174: E/AndroidRuntime(6687): ... 11 more
03-07 01:08:54.174: E/AndroidRuntime(6687): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.Fragment.instantiate(Fragment.java:597)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.Fragment.instantiate(Fragment.java:561)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.Activity.onCreateView(Activity.java:4822)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
03-07 01:08:54.174: E/AndroidRuntime(6687): ... 21 more
03-07 01:08:54.174: E/AndroidRuntime(6687): Caused by: java.lang.ClassNotFoundException: Didn''t find class "com.google.android.gms.maps.SupportMapFragment" on path: DexPathList[[zip file "/data/app/com.example.testtt-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.testtt-2, /vendor/lib, /system/lib]]
03-07 01:08:54.174: E/AndroidRuntime(6687): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-07 01:08:54.174: E/AndroidRuntime(6687): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-07 01:08:54.174: E/AndroidRuntime(6687): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-07 01:08:54.174: E/AndroidRuntime(6687): at android.app.Fragment.instantiate(Fragment.java:583)
03-07 01:08:54.174: E/AndroidRuntime(6687): ... 24 more
当我运行应用程序但它崩溃时,至少应该向我显示空SupportMapFragment
。
答案 0 :(得分:0)
尝试在代码中添加地图片段。无论如何,您可能需要稍后使用它。更改您的xml以使用FrameLayout
mapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map, mapFragment);
fragmentTransaction.commit();
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" />