2016-03-12 60 views
0

當我嘗試將位置發送到服務器時,我的應用會一直崩潰。GPS和網絡提供商始終返回null

我的主要活動is--

    package com.ambujkathotiya.swachhb.activity; 

import android.Manifest; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.Uri; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Environment; 
import android.os.StrictMode; 
import android.provider.MediaStore; 
import android.provider.Settings; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.Toast; 

import com.ambujkathotiya.swachhb.R; 

import java.io.File; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Locale; 

public class MainActivity extends AppCompatActivity implements  LocationListener { 

private static String TAG = MainActivity.class.getSimpleName(); 

String longi; 
String lat; 
LocationManager locationManager; 


// Camera activity request codes 
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100; 

public static final int MEDIA_TYPE_IMAGE = 1; 

private Uri fileUri; // file url to store image/video 


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


    //Main code 
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) { 
     StrictMode.ThreadPolicy tp = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(tp); 
    } 

    // Changing action bar background color 
    // These two lines are not needed 


    ImageButton btnCapturePicture = (ImageButton) findViewById(R.id.btnCapturePicture); 


    /** 
    * Capture image button click event 
    */ 
    btnCapturePicture.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // capture picture 
      ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo netinfo = connectivity.getActiveNetworkInfo(); 
      if (netinfo != null && netinfo.isConnected()) { 
       // Internet Connection is present 
       //Check if location services is switched on or not 

       LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

       if (locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
        //Location services enabled 
        //Call the camera 
        captureImage(); 
       } else { 
        //Location services not enabled 
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(intent); 
       } 

      } else { 
       // Internet connection is not present 
       Toast.makeText(getApplicationContext(), 
         "Sorry! You need an internet connection to proceed.", 
         Toast.LENGTH_LONG).show(); 

      } 

     } 
    }); 


    // Checking camera availability 
    if (!isDeviceSupportCamera()) { 
     Toast.makeText(getApplicationContext(), 
       "Sorry! Your device doesn't support camera", 
       Toast.LENGTH_LONG).show(); 
     // will close the app if the device does't have camera 
     finish(); 
    } 
} 

/** 
* Checking device has camera hardware or not 
* */ 
private boolean isDeviceSupportCamera() { 
    // this device has a camera 
// no camera on this device 
    return  getApplicationContext().getPackageManager().hasSystemFeature(
      PackageManager.FEATURE_CAMERA); 
} 

/** 
* Launching camera app to capture image 
*/ 
private void captureImage() { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

    // start the image capture Intent 
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
} 



/** 
* Here we store the file url as it will be null after returning from camera 
* app 
*/ 
@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 

    // save file url in bundle as it will be null on screen orientation 
    // changes 
    outState.putParcelable("file_uri", fileUri); 
} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 

    // get the file url 
    fileUri = savedInstanceState.getParcelable("file_uri"); 
} 



/** 
* Receiving activity result method will be called after closing the camera 
* */ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // if the result is capturing Image 
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
     if (resultCode == RESULT_OK) { 

      // successfully captured the image 
      // launching upload activity 
      getLocation(); 
      Log.d(TAG, "hi" + lat); 
      Log.d(TAG, "hello"+longi); 
      launchUploadActivity(true); 


     } else if (resultCode == RESULT_CANCELED) { 

      // user cancelled Image capture 
      Toast.makeText(getApplicationContext(), 
        "User cancelled image capture", Toast.LENGTH_SHORT) 
        .show(); 

     } else { 
      // failed to capture image 
      Toast.makeText(getApplicationContext(), 
        "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
        .show(); 
     } 

    } 

} 

private void launchUploadActivity(boolean isImage){ 

    Intent i = new Intent(MainActivity.this, UploadActivity.class); 
    i.putExtra("filePath", fileUri.getPath()); 
    i.putExtra("isImage", isImage); 
    i.putExtra("lat",lat); 
    i.putExtra("longi",longi); 
    startActivity(i); 
} 

/** 
* ------------ Helper Methods ---------------------- 
* */ 

/** 
* Creating file uri to store image/video 
*/ 
public Uri getOutputMediaFileUri(int type) { 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 

/** 
* returning image/video 
*/ 
private static File getOutputMediaFile(int type) { 

    // External sdcard location 
    File mediaStorageDir = new File(
      Environment 
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
      Config.IMAGE_DIRECTORY_NAME); 

    // Create the storage directory if it does not exist 
    if (!mediaStorageDir.exists()) { 
     if (!mediaStorageDir.mkdirs()) { 
      Log.d(TAG, "Oops! Failed create " 
        + Config.IMAGE_DIRECTORY_NAME + " directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
      Locale.getDefault()).format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator 
       + "IMG_" + timeStamp + ".jpg"); 
    } else { 
     return null; 
    } 

    return mediaFile; 
} 



@Override 
public void onLocationChanged(Location location) { 

} 


@Override 
public void onProviderDisabled(String provider) { 

} 


@Override 
public void onProviderEnabled(String provider) { 

} 


@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
} 

public void getLocation() 
{ 
    //Location code 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    //If location manager returns not null,then save the lat and long 
    if (locationManager != null) { 
     if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this); 
      Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
      Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      if (location != null) { 
       double latitude = location.getLatitude(); 
       double longitude = location.getLongitude(); 
       lat = Double.toString(latitude); 
       longi = Double.toString(longitude); 
      } 
      else if(location1 != null){ 
       double latitude = location1.getLatitude(); 
       double longitude = location1.getLongitude(); 
       lat = Double.toString(latitude); 
       longi = Double.toString(longitude); 
      } 
      else { 
       finish(); 
      } 
     } else { 
      finish(); 
     } 
     //Remove the request for location updates. 
     locationManager.removeUpdates(this); 
    } else { 

     finish(); 
    } 
} 

} 

這個應用程序被髮送到一個活動,我必須將數據傳輸到server.As儘快上傳我點擊上傳按鈕後,應用程序崩潰說doInBackground()中存在問題。我試着在日誌中顯示位置,它返回null。

回答

1

您的位置代碼被搞砸了。你可以調用requestLocationUpdates,但你不要等待它調用onLocationChanged--你假定它是即時的。事實並非如此。然後調用getLastLocation,它將在90%的時間內返回null,因爲它還沒有機會真正啓動位置代碼(位置發現需要時間,即使對於更快的網絡也是如此)。將它翻出來,只需要調用requestLocationUpdates來替換它,然後等待那些實際返回的位置 - 之後不要調用getLastLocation。事實上,你可能會忘記那個函數的存在。

+0

我是新開發的Android實際上。您能否指出我應該在哪裏進行更改?對不起,我很愚蠢。 –