【问题标题】:Troubleshooting android displaying error"Unfortunately app has stopped working"解决android显示错误“不幸的是应用程序已停止工作”
【发布时间】:2014-06-14 11:19:37
【问题描述】:

我通过 Eclipse 在谷歌地图上创建了一个 android 应用程序,没有错误,但是当我在模拟器或任何 android 设备上安装并运行它时,显示错误消息“不幸的是应用程序已停止工作”。 请指导我进行故障排除。 这是 java 主要活动文件:-

      package com.example.droidloc;

  import java.util.List;

  import android.os.Bundle;
 import android.app.Activity;
   import android.app.AlertDialog;
  import android.content.DialogInterface;

   import com.google.android.maps.MapActivity;
   import com.google.android.maps.MapView;
  import com.google.android.maps.Overlay;

import android.view.Menu;
import android.view.MotionEvent;

 public class MainActivity extends MapActivity {
 MapView map;
long start;
long stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map=(MapView)findViewById(R.id.mainmap);
    map.setBuiltInZoomControls(true);
    touchy t =new touchy();
    List<Overlay> overlayList = map.getOverlays();
    overlayList.add(t);
}

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

class touchy extends Overlay
{
    @SuppressWarnings("deprecation")
    public boolean onTouchEvent(MotionEvent e,MapView m)
    {
        if(e.getAction()==MotionEvent.ACTION_DOWN)
        {
            start=e.getEventTime();
        }
        if(e.getAction()==MotionEvent.ACTION_UP)
        {
            stop=e.getEventTime();
        }
        if(stop-start>1500)
        {
            AlertDialog alert=new                           

                 AlertDialog.Builder(MainActivity.this).create();
            alert.setTitle("Pick an Option");
            alert.setMessage("Pick an Option dude");
            alert.setButton2("Pin a Point", new        

               DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1)      
     {
                    // TODO Auto-generated method stub

                }
            });
            alert.show();
            return true;

        }
        return false ;
    }
    }

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


      }
---------------------------------------------------------------------------------------

这是xml文件:-

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

---------------------------------------------------------------------------------------

这是清单文件:-

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

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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-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.example.droidloc.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="AIzaSyAlHKiaTa8KrABuGHIvcuZ4IiBFifqOu1s"/>
  </application>

  </manifest>

【问题讨论】:

  • 您还需要在应用程序标签中添加以下行:&lt;meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /&gt;
  • 您首先需要查看异常堆栈跟踪。
  • 请看这个:developers.google.com/maps/documentation/android/start我认为你混淆了新旧谷歌地图的实现。

标签: android


【解决方案1】:

我认为您的 Fragment 无法转换为 MapView。所以它可能是你的 classcastexception 。如果不是,您应该发布您的 logcat 输出。

尝试在您的布局中使用它可能会解决问题:

<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainmap" 
android:layout_below="@+id/innerrelativelay" />

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

【讨论】:

    【解决方案2】:

    有很多东西不见了。以下是我看到的列表,其中一些来自 cmets:

    1. 您正在扩展基于 google maps android 版本 1 的 mapActivity。但在 xml 中,您使用的是 google maps android 的 mapfragment v2。
    2. 您应该扩展 FragmentActivity 或任何其他活动,例如从 FragmentActivity 扩展的 ActionBarActivity。
    3. 清单文件中缺少元数据标记。
    4. Min sdk 版本为 8,因此您需要在 xml 中使用 SupportMapFragment,您正在使用 API 11 以上可用的 mapfragment。

    【讨论】:

    • 以上所有建议的调试都经过尝试和测试,但没有一个有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    相关资源
    最近更新 更多