开发者

Android location search error

开发者 https://www.devze.com 2023-01-27 01:28 出处:网络
I\'m new to Android and I have this problem I\'m trying to make a program that will get my location from the best available provider. After that it gets me the image from the link and I don\'t know wh

I'm new to Android and I have this problem I'm trying to make a program that will get my location from the best available provider. After that it gets me the image from the link and I don't know where the error is in the code.

those are the errors i get

at line 102 Multiple markers at this line - Syntax error, insert "}" to complete Statement - Syntax error, insert "else Statement" to complete IfStatement

at line 142 Syntax error on tokens, delete 开发者_JS百科these tokens

at line 159: Multiple markers at this line - Syntax error on token "setText", = expected after this token

at line 162: Syntax error on token "}", delete this token

Thank you in advance

this is the manifest file:

<?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>

'

and this is the code:

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


Without looking at the code, my first guess would be that you try to run it in the emulator and didn't set a position via ddms or telnet "geo fix"?

Update Looks like you copy pasted

private Bitmap DownloadImage(String URL)

and

private InputStream OpenHttpConnection(String urlString)

at the wrong position. Your curly braces are wrong. At the moment you define a function inside a function.

0

精彩评论

暂无评论...
验证码 换一张
取 消