更改为片段活动会导致应用程序崩溃 - Android

时间:2013-06-27 10:54:59

标签: android eclipse google-maps-android-api-2 android-fragmentactivity

我正在制作一个包含四个标签的Android应用程序。其中一个标签是“查找我们”标签,该标签应显示谷歌地图有两个位置,即用户的当前位置和标记。

我已经有了我的主要活动,它使用tabhost生成四个标签,其中一个是找到我们:

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.support.v4.app.FragmentActivity;

public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost tabHost = getTabHost();

    // Tab for About
    TabSpec photospec = tabHost.newTabSpec("About");
    // setting Title and Icon for the Tab
    photospec.setIndicator("About", getResources().getDrawable(R.drawable.icon_about_tab));
    Intent photosIntent = new Intent(this, AboutActivity.class);
    photospec.setContent(photosIntent);

    // Tab for Facebook
    TabSpec songspec = tabHost.newTabSpec("Facebook");        
    songspec.setIndicator("Facebook", getResources().getDrawable(R.drawable.icon_facebook_tab));
    Intent songsIntent = new Intent(this, FacebookActivity.class);
    songspec.setContent(songsIntent);

    // Tab for Videos
    TabSpec videospec = tabHost.newTabSpec("Notices");
    videospec.setIndicator("Notices", getResources().getDrawable(R.drawable.icon_youtube_tab));
    Intent videosIntent = new Intent(this, YoutubeActivity.class);
    videospec.setContent(videosIntent);

 // Tab for Map
    TabSpec mapspec = tabHost.newTabSpec("Find Us");
    mapspec.setIndicator("Find Us", getResources().getDrawable(R.drawable.icon_map_tab));
    Intent mapIntent = new Intent(this, MapsActivity.class);
    mapspec.setContent(mapIntent);

    // Adding all TabSpec to TabHost
    tabHost.addTab(photospec); // Adding about tab
    tabHost.addTab(songspec); // Adding facebook tab
    tabHost.addTab(videospec); // Adding videos tab
    tabHost.addTab(mapspec); // Adding map tab
} 
  //making the tab view:
    private View makeTabIndicator(Drawable drawable){
    ImageView Tabimage = new ImageView(this);
    Tabimage.setImageDrawable(drawable);
    Tabimage.setBackgroundResource(R.drawable.icon_about_tab);
    return Tabimage;

}}

我已按照developers.google.com/maps/documentation/android/map上的指示进行操作,因此地图类如下所示:

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MapsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.map);}}

我的map.xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

此刻,它工作正常并显示地图,但是,要在其上显示制作者,您需要添加叠加层。为此,而不是public class MapsActivity extends Activity {它应该说public class MapsActivity extends FragmentActivity {因为它不是FragmentActivity,所以下面的代码将不起作用:

private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
        .position(new LatLng(0, 0))
        .title("Hello world"));

当我将其更改为FragmentActivity时,应用程序崩溃,logcat在下面:

06-27 12:05:11.550: D/dalvikvm(2567): GC_FOR_ALLOC freed 71K, 13% free 9516K/10887K, paused 21ms, total 22ms
06-27 12:05:11.560: I/dalvikvm-heap(2567): Grow heap (frag case) to 12.903MB for 2457616-byte allocation
06-27 12:05:11.590: D/dalvikvm(2567): GC_CONCURRENT freed 6K, 11% free 11910K/13319K, paused 12ms+2ms, total 38ms
06-27 12:05:11.991: D/libEGL(2567): loaded /system/lib/egl/libEGL_mali.so
06-27 12:05:12.041: D/libEGL(2567): loaded /system/lib/egl/libGLESv1_CM_mali.so
06-27 12:05:12.051: D/libEGL(2567): loaded /system/lib/egl/libGLESv2_mali.so
06-27 12:05:12.121: D/OpenGLRenderer(2567): Enabling debug mode 0
06-27 12:05:17.526: W/IInputConnectionWrapper(2567): showStatusIcon on inactive InputConnection
06-27 12:05:54.812: D/AndroidRuntime(2567): Shutting down VM
06-27 12:05:54.812: W/dalvikvm(2567): threadid=1: thread exiting with uncaught exception (group=0x412552a0)
06-27 12:05:54.952: E/AndroidRuntime(2567): FATAL EXCEPTION: main
06-27 12:05:54.952: E/AndroidRuntime(2567): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.asa.ignite/com.asa.ignite.MapsActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1922)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:704)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.widget.TabHost.setCurrentTab(TabHost.java:368)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.view.View.performClick(View.java:4162)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.view.View$PerformClick.run(View.java:17082)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.os.Handler.handleCallback(Handler.java:615)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.os.Looper.loop(Looper.java:137)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.app.ActivityThread.main(ActivityThread.java:4856)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at java.lang.reflect.Method.invokeNative(Native Method)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at java.lang.reflect.Method.invoke(Method.java:511)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at dalvik.system.NativeStart.main(Native Method)
06-27 12:05:54.952: E/AndroidRuntime(2567): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.app.Activity.setContentView(Activity.java:1901)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at com.asa.ignite.MapsActivity.onCreate(MapsActivity.java:11)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.app.Activity.performCreate(Activity.java:5047)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
06-27 12:05:54.952: E/AndroidRuntime(2567):     ... 18 more
06-27 12:05:54.952: E/AndroidRuntime(2567): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
06-27 12:05:54.952: E/AndroidRuntime(2567):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
06-27 12:05:54.952: E/AndroidRuntime(2567):     ... 28 more

我还在Manifest.xml(权限,API密钥等)中添加了所有必要的信息

非常感谢任何帮助。:)

编辑:android:name="com.google.android.gms.maps.MapFragment"更改为android:name="com.google.android.gms.maps.SupportMapFragment"会产生以下错误:

06-27 12:11:03.783: I/Process(2567): Sending signal. PID: 2567 SIG: 9
06-27 12:18:03.192: D/dalvikvm(4617): GC_FOR_ALLOC freed 75K, 14% free 9516K/10951K, paused 30ms, total 30ms
06-27 12:18:03.202: I/dalvikvm-heap(4617): Grow heap (frag case) to 12.903MB for 2457616-byte allocation
06-27 12:18:03.232: D/dalvikvm(4617): GC_CONCURRENT freed 6K, 12% free 11910K/13383K, paused 13ms+6ms, total 35ms
06-27 12:18:04.103: D/libEGL(4617): loaded /system/lib/egl/libEGL_mali.so
06-27 12:18:04.153: D/libEGL(4617): loaded /system/lib/egl/libGLESv1_CM_mali.so
06-27 12:18:04.163: D/libEGL(4617): loaded /system/lib/egl/libGLESv2_mali.so
06-27 12:18:04.423: D/OpenGLRenderer(4617): Enabling debug mode 0
06-27 12:18:04.613: I/Choreographer(4617): Skipped 59 frames!  The application may be doing too much work on its main thread.
06-27 12:18:06.795: D/AndroidRuntime(4617): Shutting down VM
06-27 12:18:06.795: W/dalvikvm(4617): threadid=1: thread exiting with uncaught exception (group=0x412552a0)
06-27 12:18:06.846: E/AndroidRuntime(4617): FATAL EXCEPTION: main
06-27 12:18:06.846: E/AndroidRuntime(4617): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.asa.ignite/com.asa.ignite.MapsActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1922)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:704)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.widget.TabHost.setCurrentTab(TabHost.java:368)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.view.View.performClick(View.java:4162)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.view.View$PerformClick.run(View.java:17082)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.os.Handler.handleCallback(Handler.java:615)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.os.Looper.loop(Looper.java:137)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.app.ActivityThread.main(ActivityThread.java:4856)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at java.lang.reflect.Method.invokeNative(Native Method)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at java.lang.reflect.Method.invoke(Method.java:511)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at dalvik.system.NativeStart.main(Native Method)
06-27 12:18:06.846: E/AndroidRuntime(4617): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.app.Activity.setContentView(Activity.java:1901)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at com.asa.ignite.MapsActivity.onCreate(MapsActivity.java:11)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.app.Activity.performCreate(Activity.java:5047)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
06-27 12:18:06.846: E/AndroidRuntime(4617):     ... 18 more
06-27 12:18:06.846: E/AndroidRuntime(4617): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SuppourtMapFragment: make sure class name exists, is public, and has an empty constructor that is public
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
06-27 12:18:06.846: E/AndroidRuntime(4617):     ... 28 more
06-27 12:18:06.846: E/AndroidRuntime(4617): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SuppourtMapFragment
06-27 12:18:06.846: E/AndroidRuntime(4617):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-27 12:18:06.846: E/AndroidRuntime(4617):     at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
06-27 12:18:06.846: E/AndroidRuntime(4617):     ... 31 more

2 个答案:

答案 0 :(得分:2)

使用SupportMapFragment

机器人:名称= “com.google.android.gms.maps.SupportMapFragment”

答案 1 :(得分:2)

我在我的代码中使用了这个             GoogleMap mMap;            if(mMap == null){             //尝试从SupportMapFragment获取地图。

        mMap = fragment.getMap();
        // Check if we were successful in obtaining the map.
        if (mMap == null) {
            return;
        }
    } else {
        return;
    }