在activity和mapActivity之间切换+输入位置设置

时间:2012-09-14 20:39:11

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

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

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

09-15 06:25:41.936:D /单击错误(442):这是onclick方法中的错误 09-15 06:25:41.936:D /单击错误(442):这是if语句中的错误 09-15 06:25:41.947:D / AndroidRuntime(442):关闭VM 09-15 06:25:41.947:W / dalvikvm(442):threadid = 1:线程退出未捕获异常(组= 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):at 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):at java.lang.reflect.Method.invokeNative(Native Method) 09-15 06:25:41.957:​​E / AndroidRuntime(442):at java.lang.reflect.Method.invoke(Method.java:521) 09-15 06:25:41.957:​​E / AndroidRuntime(442):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 09-15 06:25:41.957:​​E / AndroidRuntime(442):at 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()方法

public void 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();
}

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

Intent intent = new Intent();
intent.setClass(AndroidGPSTrackingActivity.this, mapslayout.class);
startActivity(intent);

清单:

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

像这样使用,错误将会解决。