jjzjj

android - 添加 "mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();"后应用程序不断崩溃

coder 2023-12-28 原文

我正在尝试学习如何使用 Google API V2 开发谷歌地图 Android 应用程序。当我没有添加

时它起作用
private GoogleMap mMap = null;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

MyGoogleMapDemo.java 中 但是当我添加它们时,应用程序一直在崩溃。我正在使用

  • 谷歌 API 4.2
  • Android SDK 工具 21.1
  • Android SDK 平台-工具 16.0.1
  • Eclipse SDK 3.7.1

而且我还将 android-support-v4.jar 添加到我的项目中。 我很期待

MyGoogleMapDemo.java

package org.lxh.demo;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MyGoogleMapDemo extends FragmentActivity 
{

private GoogleMap mMap = null;

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

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

}

ma​​in.xml

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

    map:cameraBearing="112.5"
    map:cameraTargetLat="-33.796923"
    map:cameraTargetLng="150.922433"
    map:cameraTilt="30"
    map:cameraZoom="13"
    map:mapType="normal"
    map:uiCompass="true"
    map:uiRotateGestures="true"
    map:uiScrollGestures="true"
    map:uiTiltGestures="true"
    map:uiZoomControls="false"
    map:uiZoomGestures="true" />

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.lxh.demo"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

    <permission
        android:name="org.lxh.demo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCHCxLLeMk3zRL4TYICdbYjB6nQk-oCvRs" />

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

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

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

</manifest>

日志.txt

02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapUtilization:0.25
    02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapIdealFree:8388608
    02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapConcurrentStart:2097152
    02-17 22:56:52.585: W/dalvikvm(11229): threadid=1: thread exiting with uncaught exception (group=0x4106d498)
    02-17 22:56:52.585: E/AndroidRuntime(11229): FATAL EXCEPTION: main
    02-17 22:56:52.585: E/AndroidRuntime(11229): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.lxh.demo/org.lxh.demo.MyGoogleMapDemo}: java.lang.NullPointerException
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread.access$600(ActivityThread.java:136)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.os.Handler.dispatchMessage(Handler.java:99)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.os.Looper.loop(Looper.java:137)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread.main(ActivityThread.java:4797)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at java.lang.reflect.Method.invoke(Method.java:511)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at dalvik.system.NativeStart.main(Native Method)
    02-17 22:56:52.585: E/AndroidRuntime(11229): Caused by: java.lang.NullPointerException
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at org.lxh.demo.MyGoogleMapDemo.onCreate(MyGoogleMapDemo.java:24)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.Activity.performCreate(Activity.java:5024)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    ... 11 more

最佳答案

您的 LogCat 中可能还有与您的问题相关的其他消息。我的猜测是你的问题是你有一个格式错误的 <fragment>元素。你有:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"
    ... />

你不应该两者 android:name class , 他们当然不应该指向不同的类。 android:name是首选方法。由于您继承自 FragmentActivity ,我猜你真的想要 android:name指向SupportMapFragment , 所以进行更改并删除 class属性。您可能还想摆脱 xmlns:map和所有 map:属性,直到您使其余代码正常工作。

关于android - 添加 "mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();"后应用程序不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14922678/

有关android - 添加 "mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();"后应用程序不断崩溃的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  5. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  6. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  7. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  8. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  9. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  10. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

随机推荐