0

以下源代码没有被执行......我正在学习如何处理谷歌地图..请帮助

这是我的源代码::

package com.example.maptest;

import android.os.Bundle;
import android.location.*;
import android.content.Context;
import android.view.Menu;
import com.google.android.maps.*;


public class MainActivity extends MapActivity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Context context = getApplicationContext();

    LocationManager locationManager =
            (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    Location location = null;

    double lat = location.getLatitude() * 100000;
    double longi = location.getLongitude() * 100000;


    LocationProvider provider =
            locationManager.getProvider(LocationManager.NETWORK_PROVIDER);

}

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

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

这是 XML 文件

   <com.google.android.maps.MapView
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:apiKey="0GH71P-8YZB_XmnfIDs_-NOCtdlku2nwKABE61Q"
             />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    android:ems="10"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

</RelativeLayout>

我的 AndroidManifest 是::

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

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.maptest.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>
    <uses-library android:name="com.google.android.maps" />
</application>

我真的很困惑为什么它没有开始......请帮助

03-17 08:34:05.215: W/dalvikvm(2380): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
03-17 08:34:05.235: E/AndroidRuntime(2380): FATAL EXCEPTION: main 
03-17 08:34:05.235: E/AndroidRuntime(2380): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.maptest/com.example.maptest.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.maptest.MainActivity" on path: /data/app/com.example.maptest-2.apk 
03-17 08:34:05.235: E/AndroidRuntime(2380): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:210)
4

2 回答 2

2
Location location = null;

double lat = location.getLatitude() * 100000;
double longi = location.getLongitude() * 100000;

您在空引用上调用方法,难怪您的应用程序崩溃。您应该以某种方式初始化location变量,例如调用LocationManager.getLastKnownLocation(). 希望这可以帮助。

于 2013-03-17T08:43:12.130 回答
0

您没有初始化位置。所以我猜你得到了 NPE

Location location = getGPSLocation(this);
if(location != null) {
   double lat = location.getLatitude() * 100000;
   double longi = location.getLongitude() * 100000;
}

编辑:

private Location getGPSLocation(Context context) {
    try {
        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Criteria cr = new Criteria();
        cr.setCostAllowed(true);
        cr.setAccuracy(Criteria.NO_REQUIREMENT);
        cr.setPowerRequirement(Criteria.NO_REQUIREMENT);
        String provider = lm.getBestProvider(cr, true);
        if(provider == null)
            provider = lm.getBestProvider(cr, false);
        return lm.getLastKnownLocation(provider);
    }
    catch(Exception e) {
        Log.e("App", "Exception while fetch GPS at: " + e.getMessage());
    }
    return null;
}

并添加此权限 AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
于 2013-03-17T08:24:22.763 回答