【问题标题】:switching between activity and mapActivity + entering the location settings在activity和mapActivity之间切换+进入位置设置
【发布时间】:2012-09-14 20:39:10
【问题描述】:

你好再次感谢你的回答,但这不是我想要的

这是我点击按钮时出现的错误

09-15 06:25:41.936: D/error on click(442): 这是 onclick 方法中的错误 09-15 06:25:41.936: D/error on click(442): 这是 if 语句中的错误 09-15 06:25:41.947: D/AndroidRuntime(442): 关闭虚拟机 09-15 06:25:41.947: W/dalvikvm(442): threadid=1: 线程以未捕获的异常退出 (group=0x4001d800) 09-15 06:25:41.957:​​E/AndroidRuntime(442):致命异常:主要 09-15 06:25:41.957: E/AndroidRuntime(442): java.lang.NullPointerException 09-15 06:25:41.957: E/AndroidRuntime(442): 在 com.androidhive.googlemaps.ShowMap.onClick(ShowMap.java:42) 09-15 06:25:41.957: E/AndroidRuntime(442): 在 android.view.View.performClick(View.java:2408) 09-15 06:25:41.957: E/AndroidRuntime(442): 在 android.view.View$PerformClick.run(View.java:8816) 09-15 06:25:41.957: E/AndroidRuntime(442): 在 android.os.Handler.handleCallback(Handler.java:587) 09-15 06:25:41.957: E/AndroidRuntime(442): 在 android.os.Handler.dispatchMessage(Handler.java:92) 09-15 06:25:41.957: E/AndroidRuntime(442): 在 android.os.Looper.loop(Looper.java:123) 09-15 06:25:41.957: E/AndroidRuntime(442): 在 android.app.ActivityThread.main(ActivityThread.java:4627) 09-15 06:25:41.957: E/AndroidRuntime(442): 在 java.lang.reflect.Method.invokeNative(Native Method) 09-15 06:25:41.957: E/AndroidRuntime(442): 在 java.lang.reflect.Method.invoke(Method.java:521) 09-15 06:25:41.957: E/AndroidRuntime(442): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-15 06:25:41.957: E/AndroidRuntime(442): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-15 06:25:41.957: E/AndroidRuntime(442): at dalvik.system.NativeStart.main(Native Method) 09-15 06:25:45.266: I/Process(442): 发送信号。 PID:442 SIG:9

这是按钮点击的代码

public void onClick(View v) {

     gps = new NetworkSettings();

     Log.d("error on click ", "here  is the error in the onclick method ");

     if(isGPSEnabled)
     {
     // Log.d("error on click ", "here  is the error in the if statement ");

      Intent intent = new Intent(this, AndroidGoogleMapsActivity.class);
        mContext.startActivity(intent);
     }

     Log.d("error on click ", "here  is the error out the if statement ");



         gps.showSettingsAlert();

}

这就是 showSettingsAlert() 方法

公共无效 showSettingsAlert() { alertDialog = new AlertDialog.Builder(mContext);

    Log.d("error on click ", "here  is the error inthe top of the method ");

    // Setting Dialog Title
    alertDialog.setTitle("GPS is settings");

    // Setting Dialog Message
    alertDialog
            .setMessage("GPS is not enabled. Do you want to go to settings menu?");





    // On pressing Settings button
    alertDialog.setPositiveButton("Settings",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(
                            Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    mContext.startActivity(intent);

                }
            });


    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });


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

我知道错误出现在单击按钮中,但我不知道它是什么以及如何解决它,所以请帮助我

【问题讨论】:

    标签: java android google-maps android-activity map


    【解决方案1】:

    寻找它......它可以给你任何想法......

            public Location getLocation() {
        //android.os.Debug.waitForDebugger();
        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 (isNetworkEnabled) {
                this.canGetLocation = true;
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                //      locationManager.removeUpdates(GPSTracker.this);
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
    
                    }
            //  }
            }
    
            }
            else if (isGPSEnabled){
                // if GPS Enabled get lat/long using GPS Services
                this.canGetLocation = true;
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                        //  locationManager.removeUpdates(GPSTracker.this);
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
    
                    }
                }
            }
            else{
                //return location;
                this.canGetLocation = false;
            }
    
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return location;
    }
    

    【讨论】:

    • 谢谢你的回答,但这不是我想要的,所以我通过添加一些看起来更容易理解的错误代码来编辑我的问题
    【解决方案2】:
    Intent intent = new Intent();
    intent.setClass(AndroidGPSTrackingActivity.this, mapslayout.class);
    startActivity(intent);
    

    清单:

    <uses-library
    android:name="com.google.android.maps"
    android:required="true" />
    

    这样使用,错误就解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多