Android位置搜索错误

时间:2010-11-28 01:01:41

标签: android geolocation

我是Android新手,我遇到了这个问题我正在尝试制作一个程序,从最好的提供商处获取我的位置。之后它会从链接中获取图像,我不知道代码中的错误位置。

这些是我得到的错误

第102行 此行有多个标记      - 语法错误,插入“}”以完成Statement      - 语法错误,插入“else语句”完成      IfStatement

第142行 令牌上的语法错误,删除这些令牌

第159行: 此行有多个标记      - 令牌“setText”上的语法错误,=此后的预期      令牌

第162行

: 令牌“}”上的语法错误,删除此令牌

提前谢谢

这是清单文件:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.paad.whereami"> <application android:icon="@drawable/icon"> <activity android:name=".WhereAmI" 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> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> android:name="android.permission.INTERNET <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> </manifest>

main.xml中:

'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView  
    android:id="@+id/myLocationText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"/>
  <ImageView 
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout>

,这是代码:

package com.paad.whereami;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

import android.app.Activity;
import android.os.Bundle;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.Timer;

import android.os.SystemClock;





public class WhereAmI extends Activity {

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

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setSpeedRequired(true);
    String provider = locationManager.getBestProvider(criteria, true);    

      Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(location);

    locationManager.requestLocationUpdates(provider, 2000, 1, 
                                           locationListener);
  }

  private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status, 
                                Bundle extras){ }
  };

  private void updateWithNewLocation(Location location) {
    String latLongString;
    TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    String addressString = "No address found";

    if (location != null) {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      latLongString = "Lat:" + lat + "\nLong:" + lng;


      setContentView(R.layout.main);
        Bitmap bitmap = DownloadImage(
        "http://blogs.ocweekly.com/navelgazing/boston.jpg");
        ImageView img = (ImageView) findViewById(R.id.img);
      img.setImageBitmap(bitmap);
      private Bitmap DownloadImage(String URL) {
          Bitmap bitmap = null;

          InputStream in = null;

          try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
          } catch (IOException e1) {
              e1.printStackTrace();
          }
          return bitmap;                
        }
      private InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString); 
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))                     
          throw new IOException("Not an HTTP connection");
        try{
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect(); 

            response = httpConn.getResponseCode();                 
            if (response == HttpURLConnection.HTTP_OK) {
              in = httpConn.getInputStream();                                 
            }                     
          } catch (Exception ex) {
              throw new IOException("Error connecting");            
            }
            return in;     
        }  
      }    

      }

    else {
      latLongString = "No location found";
    }
    myLocationText.setText("Your Current Position is:\n" + 
                            latLongString + "\n" + addressString);
  }
}

1 个答案:

答案 0 :(得分:0)

不看代码,我的第一个猜测是你试图在模拟器中运行它并没有通过ddms或telnet“geo fix”设置位置?

更新看起来像是复制了粘贴的

private Bitmap DownloadImage(String URL)

private InputStream OpenHttpConnection(String urlString)

在错误的位置。你的花括号错了。目前,您在函数内定义了一个函数。