无法在android中扩充片段代码

时间:2014-05-18 22:08:52

标签: java android android-fragments android-maps-v2

我是android的新手,我收到一个错误,因为我说我无法在MapaFragment类中膨胀视图fragment_map,然后应用程序停止。我正在使用KitKat api 19。

我带了一天,我不明白为什么android中的应用程序停止在locat中发出错误,不能让视图膨胀。

我正在使用Google Maps v2 API。

MainActivity.java是:

包info.android;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;

public class MainActivity extends FragmentActivity  {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

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

        Fragment fragment = (Fragment) new MapaFragment();

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        ft.replace(R.id.frame_container, fragment);

        ft.commit();

    }
}

MapaFragment.java是:

package info.android;

import java.util.List;

import info.android.R.id;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;

public class MapaFragment extends Fragment {    
public MapaFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_map, container, false);

    ...

    return v;
}

}

fragment_map.xml是:

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

<fragment
    android:name="com.google.android.gms.maps.SupportMapFragment"        
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

我的AndroidManifest是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <permission
        android:name="info.android.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="info.android.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="19" />

        <uses-library android:name="com.google.android.maps" />



        <activity
            android:name="info.android.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDVAF4WaVSVRDKJx87It8OSFP5txQcPabc" />
    </application>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

</manifest>

布局fragment_map.xml中的片段,当我拿出时,这段代码没有问题。但是当我添加片段来绘制谷歌地图的时候就是它给我带来了问题。

locat中的错误列表是:

05-18 23:41:10.906: W/dalvikvm(25636): threadid=1: thread exiting with uncaught exception (group=0x4159ad88)
05-18 23:41:10.916: E/AndroidRuntime(25636): FATAL EXCEPTION: main
05-18 23:41:10.916: E/AndroidRuntime(25636): Process: info.android, PID: 25636
05-18 23:41:10.916: E/AndroidRuntime(25636): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.android/info.android.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2248)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2298)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.os.Looper.loop(Looper.java:212)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.ActivityThread.main(ActivityThread.java:5151)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at java.lang.reflect.Method.invokeNative(Native Method)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at java.lang.reflect.Method.invoke(Method.java:515)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at dalvik.system.NativeStart.main(Native Method)
05-18 23:41:10.916: E/AndroidRuntime(25636): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at info.android.MapaFragment.onCreateView(MapaFragment.java:20)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.Activity.performStart(Activity.java:5241)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2221)
05-18 23:41:10.916: E/AndroidRuntime(25636):    ... 11 more
05-18 23:41:10.916: E/AndroidRuntime(25636): 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
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.Fragment.instantiate(Fragment.java:597)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.Fragment.instantiate(Fragment.java:561)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.Activity.onCreateView(Activity.java:4778)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
05-18 23:41:10.916: E/AndroidRuntime(25636):    ... 24 more
05-18 23:41:10.916: E/AndroidRuntime(25636): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path: DexPathList[[zip file "/system/framework/com.google.android.maps.jar", zip file "/data/app/info.android-1.apk"],nativeLibraryDirectories=[/data/app-lib/info.android-1, /vendor/lib, /system/lib]]
05-18 23:41:10.916: E/AndroidRuntime(25636):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
05-18 23:41:10.916: E/AndroidRuntime(25636):    at android.app.Fragment.instantiate(Fragment.java:583)

有什么问题?

我将正确附加我的库区域的图像添加到项目中。我将Android的版本更改为4.0.3

enter image description here

2 个答案:

答案 0 :(得分:0)

问题是您正试图从谷歌服务中访问地图片段,而不是那里..

确保您已将Google服务的库添加到您的应用程序

之后也是干净的并且构建

答案 1 :(得分:0)

我在两天的战斗后得到了解决方案,我得解决了问题,首先我做的是在下一行的关键字之后将下一行放在AndroidManifest.xml中:

<meta-data android:name="com.google.android.gms.version" android:value="4323000" />

我删除了使用库(与谷歌地图Api V2不兼容。