将Google地图集成到我的Android项目中的例外情况

时间:2014-11-13 20:37:14

标签: android google-maps

我希望将谷歌地图集成到我的应用程序中。我的程序LoggedInActivity.class如下:

package org.hos.view;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.hos.application.LoggedInUser;
import org.hos.controller.ImageAdapter;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;


public class LoggedInActivity extends ActionBarActivity implements OnClickListener {

    Button logout;
    String currentDateString;
    TextView date;
    GridView gridView;
     ArrayList<Item> gridArray = new ArrayList<Item>();
      ImageAdapter ImageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logged_in);
        String Username = LoggedInUser.getUserId().toUpperCase();
        setTitle("Hello "+Username);
        currentDateString = DateFormat.getDateInstance().format(new Date());
        date=(TextView) findViewById(R.id.time_date);
        date.setText(currentDateString);
        new Thread(new ChangeDate()).start();

        GridView gridview = (GridView) findViewById(R.id.gridView1);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(LoggedInActivity.this, v.getId() + position, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.logged_in, menu);
        logout=(Button) findViewById(R.id.logout);
        logout.setOnClickListener(this);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        else if(id == R.id.view_graph)
        {
            Intent intent = new Intent(this,ViewGraphActivity.class); 
            startActivity(intent);
            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
        }
        return super.onOptionsItemSelected(item);
    }

    public void onClick(View v)
    {
        if(v.equals(logout))
        {
            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
        }
    }
    class ChangeDate implements Runnable
    {

        public void run() 
        {
            while(true)
            {
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable(){
                public void run()
                {
                    if(date.getText().toString()!=DateFormat.getDateInstance().format(new Date()))
                    {
                        date.setText(DateFormat.getDateInstance().format(new Date()));
                    }
                } });
            }
        }
    }
    public static void testButton(Context mContext, int i) 
    {
        if(i == 0)
        {
            mContext.startActivity(new Intent(mContext,ViewGraphActivity.class));
            ((Activity) mContext).overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
        }
        else if(i == 1)
        {
            mContext.startActivity(new Intent(mContext,GoogleMapsActivity.class));
            ((Activity) mContext).overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
        }
        else if(i == 2)
        {

        }
        else if(i == 3)
        {
            mContext.startActivity(new Intent(mContext,MainActivity.class));
            ((Activity) mContext).overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
        }
    }
}

正如你在上一部分所见

if(i == 1)

    {
     "starting the googlemap activity."
    }

我的googlemapactivity.class如下:

package org.hos.view;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class GoogleMapsActivity extends ActionBarActivity {
    private GoogleMap googleMap;
    //static final LatLng TutorialsPoint = new LatLng(21 , 57);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_maps);
        try { 
            if (googleMap == null) {
               googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            }
         googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
         //Marker TP = googleMap.addMarker(new MarkerOptions().position(TutorialsPoint).title("TutorialsPoint"));
         //TP.showInfoWindow();
      } catch (Exception e)
      {
         e.printStackTrace();
      }

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我的activity_goggle_maps.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="org.hos.view.GoogleMapsActivity" >

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

</RelativeLayout>

但是,当我点击按钮打开谷歌地图时,我收到以下错误:

11-13 12:28:57.373: E/AndroidRuntime(851): FATAL EXCEPTION: main
11-13 12:28:57.373: E/AndroidRuntime(851): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.hos.view/org.hos.view.GoogleMapsActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.os.Looper.loop(Looper.java:130)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-13 12:28:57.373: E/AndroidRuntime(851):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 12:28:57.373: E/AndroidRuntime(851):  at java.lang.reflect.Method.invoke(Method.java:507)
11-13 12:28:57.373: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-13 12:28:57.373: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-13 12:28:57.373: E/AndroidRuntime(851):  at dalvik.system.NativeStart.main(Native Method)
11-13 12:28:57.373: E/AndroidRuntime(851): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
11-13 12:28:57.373: E/AndroidRuntime(851):  at org.hos.view.GoogleMapsActivity.onCreate(GoogleMapsActivity.java:21)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-13 12:28:57.373: E/AndroidRuntime(851):  ... 11 more
11-13 12:28:57.373: E/AndroidRuntime(851): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/org.hos.view-1.apk]
11-13 12:28:57.373: E/AndroidRuntime(851):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
11-13 12:28:57.373: E/AndroidRuntime(851):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
11-13 12:28:57.373: E/AndroidRuntime(851):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.view.LayoutInflater.createView(LayoutInflater.java:471)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
11-13 12:28:57.373: E/AndroidRuntime(851):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
11-13 12:28:57.373: E/AndroidRuntime(851):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
11-13 12:28:57.373: E/AndroidRuntime(851):  ... 20 more

任何人都可以帮助我。我想将googlemaps集成到我的应用程序中以显示设备位置。 在此先感谢。

1 个答案:

答案 0 :(得分:0)

您正在使用ActionBarActivity,这意味着您正在使用Android支持库。因此,您必须在xml文件中使用SupportMapFragment而不是MapFragment

只需更改此行

即可
android:name="com.google.android.gms.maps.MapFragment"

到这个

android:name="com.google.android.gms.maps.SupportMapFragment"