当我点击按钮(意图)打开它时,谷歌地图在我的应用程序上崩溃

时间:2014-11-11 03:38:08

标签: android google-maps android-intent

我有一个Android应用程序,里面有一些活动,包括谷歌地图片段,我在主屏幕上有一个按钮来打开这个地图活动,当我点击按钮它只是一直崩溃应用程序。

贝娄是主要班级。

package com.test.finalproject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.FragmentActivity;

  public class MainActivity extends  ActionBarActivity implements View.OnClickListener {

Button buttonLogin;
Button buttonRegister;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    buttonLogin = (Button)findViewById(R.id.buttonLogin);
    buttonLogin.setOnClickListener(this);
    buttonLogin = (Button)findViewById(R.id.buttonRegister);
    buttonLogin.setOnClickListener(this);
}

private void buttonLoginClick()
{
    startActivity (new Intent("GoogleMaps"));
}

private void buttonRegisterClick()
{
    startActivity (new Intent("TextToo"));
}

@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;
}

@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);
}



@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.buttonLogin:
            buttonLoginClick();
            break;

    }
    switch (v.getId())
    {
    case R.id.buttonRegister:
        buttonRegisterClick();
        break;
        }
    }
  }

这是布局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:orientation="vertical">

<TextView
    android:background="@null"
    android:editable="false"
    android:cursorVisible="false"

    />

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

</RelativeLayout>

这是清单。

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.finalproject"
android:versionCode="7"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
    android:name="com.test.finalproject.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.test.finalproject.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gfs.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-library android:name="com.google.android.maps" />
    <activity android:name="com.facebook.LoginActivity" >
    </activity>
    <activity
        android:name=".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.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyB1_6ny8uuUeslw566zO6ghBCOE3_rxklE" />

    <activity
        android:name=".MainApp"
        android:label="@string/title_activity_main_app" >
        <intent-filter>
            <action android:name="MainApp" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Register"
        android:label="@string/title_activity_register" >
        <intent-filter>
            <action android:name="Register" />

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

    <activity
        android:name=".GoogleMaps"
        android:label="@string/title_activity_google_maps" >
        <intent-filter>
            <action android:name="GoogleMaps" />

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

    <activity
        android:name=".TextTo"
        android:label="@string/title_activity_text_to" >
        <intent-filter>
            <action android:name="TextToo" />

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

</manifest>

GoogleMaps活动:

package com.test.finalproject;

import com.google.android.maps.MapActivity;

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

public class GoogleMaps extends MapActivity {

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

@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);
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
   }
}
  

这是一个日志的图像,它有更多的内容,我只是不知道如何导出它,我已经搜索了几天,并尝试了许多不起作用的不同解决方案。如果可以,请帮助我,谢谢你。

编辑:我没有足够的声望发布图片:(

编辑:LogCat;

     11-11 10:50:55.196: I/Process(15717): Sending signal. PID: 15717 SIG: 9
11-11 10:50:55.926: I/PersonaManager(15873): getPersonaService() name persona_policy
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:48 height:48 bitmap id is 180 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:13 height:41 bitmap id is 181 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:48 height:48 bitmap id is 182 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:72 height:72 bitmap id is 183 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:18 height:18 bitmap id is 184 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:72 height:72 bitmap id is 185 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:18 height:18 bitmap id is 186 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:72 height:72 bitmap id is 187 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:72 height:72 bitmap id is 188 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:72 height:72 bitmap id is 189 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 190 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 191 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 192 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 193 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 194 
11-11 10:50:55.956: D/skia(15873): GFXPNG PNG bitmap created width:21 height:93 bitmap id is 195 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:18 height:18 bitmap id is 196 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:21 height:93 bitmap id is 197 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:21 height:93 bitmap id is 198 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 199 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 200 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 201 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 202 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 203 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 204 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:40 height:40 bitmap id is 205 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 206 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 207 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 208 
11-11 10:50:55.966: D/skia(15873): GFXPNG PNG bitmap created width:78 height:96 bitmap id is 209 
11-11 10:50:55.976: D/skia(15873): GFXPNG PNG bitmap created width:96 height:96 bitmap id is 210 
11-11 10:50:55.976: D/skia(15873): GFXPNG PNG bitmap created width:144 height:144 bitmap id is 211 
11-11 10:50:55.976: E/MoreInfoHPW_ViewGroup(15873): Parent view is not a TextView
11-11 10:50:56.036: I/Adreno-EGL(15873): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
11-11 10:50:56.036: I/Adreno-EGL(15873): OpenGL ES Shader Compiler Version: E031.24.00.08+13
11-11 10:50:56.036: I/Adreno-EGL(15873): Build Date: 03/20/14 Thu
11-11 10:50:56.036: I/Adreno-EGL(15873): Local Branch: 0320_AU200_patches
11-11 10:50:56.036: I/Adreno-EGL(15873): Remote Branch: 
11-11 10:50:56.036: I/Adreno-EGL(15873): Local Patches: 
11-11 10:50:56.036: I/Adreno-EGL(15873): Reconstruct Branch: 
11-11 10:50:56.066: D/OpenGLRenderer(15873): Enabling debug mode 0
11-11 10:50:56.186: I/System.out(15873): Thread-15991(HTTPLog):isShipBuild true
11-11 10:50:56.186: I/System.out(15873): Thread-15991(HTTPLog):SmartBonding Enabling is true, SHIP_BUILD is true, log to file is false, DBG is false
11-11 10:50:56.946: I/PersonaManager(15873): getPersonaService() name persona_policy
11-11 10:50:56.976: E/MoreInfoHPW_ViewGroup(15873): Parent view is not a TextView
11-11 10:50:56.996: I/u(15873): Making Creator dynamically
11-11 10:50:56.996: I/Google Maps Android API(15873): Google Play services client version: 6171000
11-11 10:50:57.006: I/Google Maps Android API(15873): Google Play services package version: 6188038
11-11 10:50:57.426: D/REQUEST(15873): Using server: https://clients4.google.com/glm/mmap/api
11-11 10:50:57.426: D/AndroidRuntime(15873): Shutting down VM
11-11 10:50:57.426: W/dalvikvm(15873): threadid=1: thread exiting with uncaught exception (group=0x4182ada0)
11-11 10:50:57.436: E/AndroidRuntime(15873): FATAL EXCEPTION: main
11-11 10:50:57.436: E/AndroidRuntime(15873): Process: com.test.finalproject, PID: 15873
11-11 10:50:57.436: E/AndroidRuntime(15873): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.finalproject/com.test.finalproject.GoogleMaps}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.ActivityThread.access$900(ActivityThread.java:172)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.os.Looper.loop(Looper.java:136)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.ActivityThread.main(ActivityThread.java:5586)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at java.lang.reflect.Method.invokeNative(Native Method)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at java.lang.reflect.Method.invoke(Method.java:515)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at dalvik.system.NativeStart.main(Native Method)
11-11 10:50:57.436: E/AndroidRuntime(15873): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.Activity.setContentView(Activity.java:2031)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.test.finalproject.GoogleMaps.onCreate(GoogleMaps.java:15)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.Activity.performCreate(Activity.java:5451)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
11-11 10:50:57.436: E/AndroidRuntime(15873):    ... 11 more
11-11 10:50:57.436: E/AndroidRuntime(15873): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
11-11 10:50:57.436: E/AndroidRuntime(15873): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.maps.api.android.lib6.c.ca.a(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.maps.api.android.lib6.c.c.a(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.maps.api.android.lib6.c.dw.a(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.maps.api.android.lib6.c.v.a(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.maps.api.android.lib6.c.u.a(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.android.gms.maps.internal.u.onTransact(SourceFile:107)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.os.Binder.transact(Binder.java:361)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.android.gms.dynamic.a$4.b(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.android.gms.dynamic.a.a(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.Fragment.performCreateView(Fragment.java:1700)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.app.Activity.onCreateView(Activity.java:4997)
11-11 10:50:57.436: E/AndroidRuntime(15873):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
11-11 10:50:57.436: E/AndroidRuntime(15873):    ... 21 more

3 个答案:

答案 0 :(得分:1)

问题很可能是xml中的textview。你既没有layout_width也没有layout_height属性。

<TextView
android:background="@null"
android:editable="false"
android:cursorVisible="false"

/>

将其更改为:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:editable="false"
android:cursorVisible="false"

/>

如上面评论中所述,在清单中的应用标记内添加Google Play服务的元数据标记:

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

答案 1 :(得分:1)

你得到例外:

  

尝试实例化一个不是片段的com.google.android.gms.maps.SupportMapFragment类,如下所示:java.lang.ClassCastException

您正在使用MapActivity:GoogleMaps extends MapActivity表示您需要处理MapFragment而不是SupportMapFragment

您可以通过两种方式处理此问题:

<强> 1  将SupportMapFragment替换为MapFragment

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

在这种情况下,您需要导入:

import android.app.Fragment;
import android.app.FragmentTransaction;

2。MapActivity替换为FragmentActivity

在这种情况下,您需要导入:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

答案 2 :(得分:0)

您的TextView没有正确的属性,您必须指定layout_width和layout_height!