2013-02-26 128 views
0

我正在制作Android应用程序,通过使用位置管理器api需要用户当前位置...首先确保互联网连接,如果互联网未连接,则应用程序中会显示dailog框显示。它会得到羚羊和纬度。但是当我运行这个显示显示不同类型的错误,如致命的主要异常等..我很难但很顺利我找不到出路,因为它与最后一年的项目有关,所以PLZ帮助我。 。解决错误“致命异常

package com.example.directory; 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.ListView; 



public class MainActivity extends Activity { 
    ConnectionDetector cd; 
    AlertDialogManager alert = new AlertDialogManager(); 
    Boolean isInternetPresent = false; 
    GPSTracker gps; 
    ListView lv; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //cd = new ConnectionDetector(getApplicationContext()); 
     //isInternetPresent = cd.isConnectingToInternet(); 
     /*if (!isInternetPresent) { 
      // Internet Connection is not present 
      alert.showAlertDialog(MainActivity.this, "Internet Connection Error", 
        "Please connect to working Internet connection", false); 
      // stop executing code by return 
      return; 
     } 
     else{alert.showAlertDialog(MainActivity.this, "Internet Connection Error", 
       " working Internet connection", false);}*/ 
     gps = new GPSTracker(this); 
     if (gps.canGetLocation()) { 
      Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude()); 
     } else { 
      // Can't get user's current location 
      alert.showAlertDialog(MainActivity.this, "GPS Status", 
        "Couldn't get location information. Please enable GPS", 
        false); 
      // stop executing code by return 
      return; 
     } 
    // lv = (ListView) findViewById(R.id.list); 
    } 

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

} 
///////////////////// 
package com.example.directory; 

import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 

public class ConnectionDetector { 

    private Context _context; 

    public ConnectionDetector(Context context){ 
     this._context = context; 
    } 

    /** 
    * Checking for all possible internet providers 
    * **/ 
    public boolean isConnectingToInternet(){ 
     ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); 
      if (connectivity != null) 
      { 
       NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
       if (info != null) 
        for (int i = 0; i < info.length; i++) 
         if (info[i].getState() == NetworkInfo.State.CONNECTED) 
         { 
          return true; 
         } 

      } 
      return false; 
    } 
} 


////////////////// 
package com.example.directory; 

import android.app.AlertDialog; 
import android.app.Service; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.provider.Settings; 
import android.util.Log; 

public class GPSTracker extends Service implements LocationListener { 

    private final Context mContext; 

    // flag for GPS status 
    boolean isGPSEnabled = false; 

    // flag for network status 
    boolean isNetworkEnabled = false; 

    // flag for GPS status 
    boolean canGetLocation = false; 

    Location location = null; // location 
    double latitude; // latitude 
    double longitude; // longitude 

    // The minimum distance to change Updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

    // Declaring a Location Manager 
    protected LocationManager locationManager; 

    public GPSTracker(Context context) { 
     this.mContext = context; 
     getLocation(); 
    } 

    public Location getLocation() { 
     try { 
      locationManager = (LocationManager) mContext 
        .getSystemService(LOCATION_SERVICE); 

      // getting GPS status 
      isGPSEnabled = locationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      isNetworkEnabled = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled && !isNetworkEnabled) { 
       // no network provider is enabled 
      } else { 
       this.canGetLocation = true; 
       if (isNetworkEnabled) { 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network Enabled"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return location; 
    } 

    /** 
    * Stop using GPS listener Calling this function will stop using GPS in your 
    * app 
    * */ 
    public void stopUsingGPS() { 
     if (locationManager != null) { 
      locationManager.removeUpdates(GPSTracker.this); 
     } 
    } 

    /** 
    * Function to get latitude 
    * */ 
    public double getLatitude() { 
     if (location != null) { 
      latitude = location.getLatitude(); 
     } 

     // return latitude 
     return latitude; 
    } 

    /** 
    * Function to get longitude 
    * */ 
    public double getLongitude() { 
     if (location != null) { 
      longitude = location.getLongitude(); 
     } 

     // return longitude 
     return longitude; 
    } 

    /** 
    * Function to check GPS/wifi enabled 
    * 
    * @return boolean 
    * */ 
    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 
    @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) { 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    }} 
///////////////////////// 
package com.example.directory; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 

public class AlertDialogManager { 
    /** 
    * Function to display simple Alert Dialog 
    * @param context - application context 
    * @param title - alert dialog title 
    * @param message - alert message 
    * @param status - success/failure (used to set icon) 
    *    - pass null if you don't want icon 
    * */ 
    public void showAlertDialog(Context context, String title, String message, 
      Boolean status) { 
     AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

     // Setting Dialog Title 
     alertDialog.setTitle(title); 

     // Setting Dialog Message 
     alertDialog.setMessage(message); 

     if(status != null) 
      // Setting alert dialog icon 
      alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail); 

     // Setting OK Button 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
      } 
     }); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 
} 

log cat 
//////////////////////////// 
01-26 20:11:59.212: W/dalvikvm(2541): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
01-26 20:11:59.232: E/AndroidRuntime(2541): FATAL EXCEPTION: main 
01-26 20:11:59.232: E/AndroidRuntime(2541): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.directory/com.example.directory.MainActivity}: java.lang.SecurityException: ConnectivityService: Neither user 10042 nor current process has android.permission.ACCESS_NETWORK_STATE. 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.os.Looper.loop(Looper.java:130) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at java.lang.reflect.Method.invoke(Method.java:507) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at dalvik.system.NativeStart.main(Native Method) 
01-26 20:11:59.232: E/AndroidRuntime(2541): Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10042 nor current process has android.permission.ACCESS_NETWORK_STATE. 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.os.Parcel.readException(Parcel.java:1322) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.os.Parcel.readException(Parcel.java:1276) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.net.IConnectivityManager$Stub$Proxy.getAllNetworkInfo(IConnectivityManager.java:390) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.net.ConnectivityManager.getAllNetworkInfo(ConnectivityManager.java:267) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at com.example.directory.ConnectionDetector.isConnectingToInternet(ConnectionDetector.java:22) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at com.example.directory.MainActivity.onCreate(MainActivity.java:23) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
01-26 20:11:59.232: E/AndroidRuntime(2541):  ... 11 more 
+1

总是发送完整的堆栈跟踪以查找崩溃错误。 – 2013-02-26 20:36:16

+0

花一些时间学习如何调试您的应用程序。无论编程语言如何,这对所有程序员来说都是必不可少的技能。这将允许您非常容易地调试和确定空指针的原因,并最终为您节省大量时间。 – dymmeh 2013-02-26 20:45:03

+0

发布您的完整logcat – Shoshi 2013-02-26 21:03:12

回答

0
java.lang.SecurityException: ConnectivityService: Neither user 10042 nor current process has android.permission.ACCESS_NETWORK_STATE. 

此异常告诉你有一个权限丢失在您的清单:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
相关问题