片段内没有工作AlertDialog.Builder ...我的代码如下

时间:2014-02-01 16:03:59

标签: android android-fragments

显示错误

在我写的ACTION_LOCATION_SOURCE_SETTINGS&我在哪里写完()方法..... plz帮助
错误是:ACTION_LOCATION_SOURCE_SETTINGS无法解析或不是字段             对于类型new DialogInterface.OnClickListener(){}

,方法finish()未定义
 package com.mamun.tasktest;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;


public class FragmentB extends Fragment implements OnClickListener {


    private Button btnLocation;
    private  LocationManager manager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_b, null, false);

        btnLocation = (Button) view.findViewById(R.id.btnLocation);
        btnLocation.setOnClickListener(this);
        manager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
        return view;
    }

    @Override
    public void onClick(View v) {
        if(isMapAvailalble())
        {

            if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                  builder.setTitle("Location Manager");
                  builder.setMessage("GPS is currently disabled.\nWould you like to change these settings now?");
                  builder.setPositiveButton("Yes",
                      new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                          startActivity(i);
                        }
                      });
                  builder.setNegativeButton("No",
                      new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          finish();
                        }
                      });
                  builder.create().show();
                }




        Intent i = new Intent(getActivity(), MapActivity.class);
        startActivity(i);
        }


    }

    public boolean isMapAvailalble()
    {
        int resultcode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
        if(ConnectionResult.SUCCESS==resultcode)
        {
            return true;
        }
        else if(GooglePlayServicesUtil.isUserRecoverableError(resultcode))
        {
            Dialog d = GooglePlayServicesUtil.getErrorDialog(resultcode, getActivity(), 1);
            d.show();
        }
        else
        {
            Toast.makeText(getActivity()," Google Map API is not supported in your device",Toast.LENGTH_LONG).show();
        }
        return false;
    }







}

2 个答案:

答案 0 :(得分:1)

更改为:

getActivity().finish();

答案 1 :(得分:0)

您确定要在声明中Settings定义此类import android.media.audiofx.BassBoost.Settings;吗?

对于finish()您需要在Activity对象上调用它,因此您需要放置getActivity().finish()