android google maps错误膨胀类片段 - 尝试了我能找到的所有解决方案

时间:2016-04-27 14:14:02

标签: android maps fragment

我一直在尝试将Google地图添加到我的应用中,但一直收到错误消息。我知道这个主题已经遍布stackOverflow但我已经尝试了几乎所有的东西,我认为必须有一些我没有看到的东西。请理解我已经尝试了stackoverflow上的所有解决方案。如果你可以请试着找我,让我知道如果有什么我做得不对,我会很感激:)

我的MapActivity:

public class MapActivity  extends FragmentActivity implements OnMapReadyCallback{

    TextView temperature, forecast, clouds, wind, humidity;
    Switch mySwitch;
    String switchChosen;

    //all of these variables are used for pulling data from the database
    InputStream is=null;
    String result=null;
    String line=null;

    JSONObject jsonObj;

    ArrayList<String> items;

    ProgressDialog pDialog;


    AutoCompleteTextView enterCity;
    String[] languages={"Johannesburg","Tokyo","Pretoria","Rome","London","Mumbai", "Chicago", "Berlin", "Osaka", "Philadelphia", "Shanghai", "Zurich"};
    Button okButton;


    GoogleMap mMap;


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

        temperature = (TextView)findViewById(R.id.textView_temperature);
        forecast = (TextView)findViewById(R.id.textView_forecast);
        clouds = (TextView)findViewById(R.id.textView_clouds);
        wind = (TextView)findViewById(R.id.textView_wind);
        humidity = (TextView)findViewById(R.id.textView_humidity);


        enterCity=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
        enterCity.setAdapter(adapter);
        enterCity.setThreshold(1);

        okButton = (Button)findViewById(R.id.button_ok);
        okButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                //check if there is network connection
                if (isNetworkConnected()) {
                    new getWeather().execute();
                } else if (!isNetworkConnected()) {
                    if (pDialog.isShowing()) {
                        pDialog.dismiss();
                    }
                    Toast.makeText(getApplicationContext(), "Please Connect To A Network.", Toast.LENGTH_LONG).show();
                }
            }

        });


        mySwitch = (Switch)findViewById(R.id.switch1);

        mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {


                //check if there is network connection
                if (isNetworkConnected()) {
                    new getWeather().execute();
                } else if (!isNetworkConnected()) {
                    if (pDialog.isShowing()) {
                        pDialog.dismiss();
                    }
                    Toast.makeText(getApplicationContext(), "Please Connect To A Network.", Toast.LENGTH_LONG).show();
                }

            }
        });


        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);




    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney, Australia, and move the camera.
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    }


    class getWeather extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {

            Log.v("result", getData());

            try {


                jsonObj = new JSONObject(getData());
                Log.v("jsonObj", jsonObj.toString());



            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            try {

                Log.v("j.get", jsonObj.get("coord").toString());




                temperature.setText(jsonObj.getJSONObject("main").getString("temp"));
                forecast.setText(jsonObj.get("coord").toString());
                clouds.setText(jsonObj.getJSONObject("clouds").getString("all") + " %");
                wind.setText(jsonObj.getJSONObject("wind").getString("deg"));
                humidity.setText(jsonObj.getJSONObject("main").getString("humidity") + " %");



            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }



    public String getData()
    {

        //check the current state of the switch before we call the URL
        if(mySwitch.isChecked()){
            switchChosen = "metric";
        }
        else {
            switchChosen = "imperial";
        }


        try {
            URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q=" + enterCity.getText() + "&units=" + switchChosen + "&APPID=cc5dd2bb37b95fcf31f3343ea7285c3f");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder stringBuilder = new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line).append("\n");
                }
                bufferedReader.close();
                return stringBuilder.toString();
            }
            finally{
                urlConnection.disconnect();
            }
        }
        catch(Exception e) {
            Log.e("ERROR", e.getMessage(), e);
            return null;
        }

    }


    public boolean isNetworkConnected(){

        boolean connected = true;
        ConnectivityManager cm =
                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected() && netInfo.isAvailable()) {
            //Toast.makeText(getActivity(), "connected.", Toast.LENGTH_LONG).show();
            connected= true;
        }else{
            connected= false;
            //Toast.makeText(getActivity(), " Not connected.", Toast.LENGTH_LONG).show();
        }
        return connected;
    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);


        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {



            case R.id.action_settings:

                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }


}

我的activity_map.xml:

<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 xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />



</RelativeLayout>

我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jared.myweatherapp">

    <permission
        android:name="app.me.locationalarm.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="app.me.locationalarm.permission.MAPS_RECEIVE" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

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




    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MapActivity"
            android:label="@string/title_activity_map"
            android:theme="@style/AppTheme.NoActionBar" />
        <!--
             ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
        -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".RegisterActivity"
            android:label="@string/title_activity_register"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>

我的Logcat:

04-28 09:20:46.301 3727-3727/com.example.jared.myweatherapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.jared.myweatherapp, PID: 3727
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jared.myweatherapp/com.example.jared.myweatherapp.MapActivity}: android.view.InflateException: Binary XML file line #24: Binary XML file line #141: Error inflating class fragment
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                               Caused by: android.view.InflateException: Binary XML file line #24: Binary XML file line #141: Error inflating class fragment
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                                  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
                                                                                  at android.app.Activity.setContentView(Activity.java:2166)
                                                                                  at com.example.jared.myweatherapp.MapActivity.onCreate(MapActivity.java:83)
                                                                                  at android.app.Activity.performCreate(Activity.java:6237)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:148) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                               Caused by: android.view.InflateException: Binary XML file line #141: Error inflating class fragment
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                  at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
                                                                                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                                  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
                                                                                  at android.app.Activity.setContentView(Activity.java:2166) 
                                                                                  at com.example.jared.myweatherapp.MapActivity.onCreate(MapActivity.java:83) 
                                                                                  at android.app.Activity.performCreate(Activity.java:6237) 
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:148) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                               Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
                                                                                  at com.google.maps.api.android.lib6.c.ad.a(Unknown Source)
                                                                                  at com.google.maps.api.android.lib6.a.e.a(Unknown Source)
                                                                                  at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
                                                                                  at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
                                                                                  at com.google.android.gms.maps.internal.h.onTransact(SourceFile:62)
                                                                                  at android.os.Binder.transact(Binder.java:387)
                                                                                  at com.google.android.gms.maps.internal.ah.b(SourceFile:179)
                                                                                  at com.google.android.gms.maps.internal.CreatorImpl.b(SourceFile:100)
                                                                                  at com.google.android.gms.maps.internal.ag.onTransact(SourceFile:62)
                                                                                  at android.os.Binder.transact(Binder.java:387)
                                                                                  at com.google.android.gms.maps.internal.zzc$zza$zza.zzs(Unknown Source)
                                                                                  at com.google.android.gms.maps.SupportMapFragment$zzb.zzzW(Unknown Source)
                                                                                  at com.google.android.gms.maps.SupportMapFragment$zzb.zza(Unknown Source)
                                                                                  at com.google.android.gms.dynamic.zza.zza(Unknown Source)
                                                                                  at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
                                                                                  at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
                                                                                  at android.support.v4.app.Fragment.onInflate(Fragment.java:1142)
                                                                                  at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2287)
                                                                                  at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                                  at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
                                                                                at android.support

非常感谢

0 个答案:

没有答案