2016-09-19 3 views
-1

私のアプリにアンドロイドPlaceAutocompleteFragment機能を統合しています。 私はドキュメントのgoogle提供に従っています...私のレイアウトにXMLコードを配置しました...そしてPlaceAutoCompleteエラーが発生しました.....私のフラグメントは、v4のフラグメントを拡張します。 import android.support.v4.app.Fragment;Android v4のフラグメントをPlaceAutocompleteFragmentにキャストすることはできません

私はここで、次のエラー enter image description here

を見ていますが、私はマイク・M.コメントから問題を解決してきた私のコード

import android.Manifest; 
import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.TimePickerDialog; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.Fragment; 
import android.support.v4.content.ContextCompat; 
import android.text.InputType; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.TimePicker; 
import android.widget.Toast; 

import com.google.android.gms.common.api.Status; 
import com.google.android.gms.location.places.Place; 
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment; 
import com.google.android.gms.location.places.ui.PlaceSelectionListener; 
import com.google.maps.GeoApiContext; 
import com.google.maps.GeocodingApi; 
import com.google.maps.model.GeocodingResult; 
import com.google.maps.model.LatLng; 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Locale; 

import rollwithme.com.rollwithme.R; 
import rollwithme.com.rollwithme.Services.CustomLocationManager; 
import rollwithme.com.rollwithme.Services.GPSTracker; 
import rollwithme.com.rollwithme.Services.LocationValue; 
import rollwithme.com.rollwithme.Utils.Constants; 
import rollwithme.com.rollwithme.Utils.Utils; 

/** 
* A simple {@link Fragment} subclass. 
*/ 
public class PlanFragment extends Fragment implements View.OnFocusChangeListener, View.OnClickListener { 

    private static final int PERMISSION_ACCESS_COARSE_LOCATION = 10102; 
    private static final String TAG = "PlanFragment"; 
    Calendar myCalendar = Calendar.getInstance(); 
    private View fragmentLayout; 
    private EditText dateEditText, timeEditText, startEditText, endEditText; 
    private ImageButton startCurrent; 

    GPSTracker gps; 

    public PlanFragment() { 
     // Required empty public constructor 
    } 

    public static PlanFragment newInstance() { 
     PlanFragment fragment = new PlanFragment(); 

     return fragment; 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     fragmentLayout = inflater.inflate(R.layout.fragment_plan, container, false); 
     return fragmentLayout; 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     checkPermission(); 

     startCurrent = (ImageButton) fragmentLayout.findViewById(R.id.startCurrent); 

     startEditText = (EditText) fragmentLayout.findViewById(R.id.start); 
     endEditText = (EditText) fragmentLayout.findViewById(R.id.end); 
     dateEditText = (EditText) fragmentLayout.findViewById(R.id.date); 
     timeEditText = (EditText) fragmentLayout.findViewById(R.id.time); 

     startEditText.setOnFocusChangeListener(this); 
     endEditText.setOnFocusChangeListener(this); 
     dateEditText.setInputType(InputType.TYPE_NULL); 
     timeEditText.setInputType(InputType.TYPE_NULL); 

     startCurrent.setOnClickListener(this); 

     final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { 

      @Override 
      public void onDateSet(DatePicker view, int year, int monthOfYear, 
            int dayOfMonth) { 
       // TODO Auto-generated method stub 
       myCalendar.set(Calendar.YEAR, year); 
       myCalendar.set(Calendar.MONTH, monthOfYear); 
       myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
       updateDate(); 
      } 

     }; 

     final TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() { 
      @Override 
      public void onTimeSet(TimePicker timePicker, int i, int i1) { 
       timeEditText.setText(i + ":" + i1); 
      } 
     }; 

     dateEditText.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //hideKeyboard(dateEditText); 
       new DatePickerDialog(getActivity(), date, myCalendar 
         .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), 
         myCalendar.get(Calendar.DAY_OF_MONTH)).show(); 
      } 
     }); 

     timeEditText.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //hideKeyboard(dateEditText); 
       new TimePickerDialog(getActivity(), timeSetListener, myCalendar 
         .get(Calendar.HOUR_OF_DAY), myCalendar.get(Calendar.MINUTE), true).show(); 
      } 
     }); 


     PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 

     autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
      @Override 
      public void onPlaceSelected(Place place) { 
       // TODO: Get info about the selected place. 
       Log.i(TAG, "Place: " + place.getName()); 
      } 

      @Override 
      public void onError(Status status) { 
       // TODO: Handle the error. 
       Log.i(TAG, "An error occurred: " + status); 
      } 
     }); 
    } 


    @Override 
    public void onFocusChange(View view, boolean b) { 
     switch (view.getId()) { 
      case R.id.date: 
      case R.id.time: 
      case R.id.start: 
      case R.id.end: 
       if (!b) { 
        hideKeyboard(view); 
       } 
       break; 
      default: 
       break; 
     } 
    } 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.startCurrent: 
       checkPermission(); 
//    gps = new GPSTracker(getActivity()); 
// 
//    // check if GPS enabled 
//    if(gps.canGetLocation()){ 
// 
// 
//     double latitude = gps.getLatitude(); 
//     double longitude = gps.getLongitude(); 
// 
//     // \n is for new line 
//     Toast.makeText(getActivity(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
//    }else{ 
//     // can't get location 
//     // GPS or Network is not enabled 
//     // Ask user to enable GPS/network in settings 
//     gps.showSettingsAlert(); 
//    } 
       break; 
      default: 
       break; 
     } 
    } 

    private void updateDate() { 
     String myFormat = "MM/dd/yy"; //In which you need put here 
     SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); 

     dateEditText.setText(sdf.format(myCalendar.getTime())); 
    } 

    public void hideKeyboard(View view) { 
     InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    } 


    void checkPermission() { 
     if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_ACCESS_COARSE_LOCATION); 

     } else { 
      gps = new GPSTracker(getActivity(), getActivity()); 

      // Check if GPS enabled 
      if (gps.canGetLocation()) { 

       double latitude = gps.getLatitude(); 
       double longitude = gps.getLongitude(); 

       // \n is for new line 
       GeoApiContext context = new GeoApiContext().setApiKey(Constants.API_KEY_GMAP); 
       try { 
        GeocodingResult coding[] = GeocodingApi.newRequest(context).latlng(new LatLng(gps.getLatitude(), gps.getLongitude())).await(); 
        startEditText.setText(coding[0].formattedAddress + ""); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } else { 
       // Can't get location. 
       // GPS or network is not enabled. 
       // Ask user to enable GPS/network in settings. 
       gps.showSettingsAlert(); 
      } 
     } 
//  if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) 
//    != PackageManager.PERMISSION_GRANTED) { 
//   ActivityCompat.requestPermissions(getActivity(), new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, 
//     PERMISSION_ACCESS_COARSE_LOCATION); 
//  } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     switch (requestCode) { 
      case PERMISSION_ACCESS_COARSE_LOCATION: 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        // permission was granted, yay! Do the 

        // contacts-related task you need to do. 

        gps = new GPSTracker(getActivity(), getActivity()); 

        // Check if GPS enabled 
        if (gps.canGetLocation()) { 

         double latitude = gps.getLatitude(); 
         double longitude = gps.getLongitude(); 

         // \n is for new line 
         //Toast.makeText(getActivity(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
         GeoApiContext context = new GeoApiContext().setApiKey(Constants.API_KEY_GMAP); 
         try { 
          GeocodingResult coding[] = GeocodingApi.newRequest(context).latlng(new LatLng(gps.getLatitude(), gps.getLongitude())).await(); 
          startEditText.setText(coding[0].formattedAddress + ""); 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
        } else { 
         // Can't get location. 
         // GPS or network is not enabled. 
         // Ask user to enable GPS/network in settings. 
         gps.showSettingsAlert(); 
        } 

       } else { 

        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 

        Toast.makeText(getActivity(), "You need to grant permission", Toast.LENGTH_SHORT).show(); 
       } 

       break; 
     } 
    } 

    private void getCurrentLocation() { 
     CustomLocationManager.getCustomLocationManager().getCurrentLocation(getActivity(), locationValue); 
    } 

    public LocationValue locationValue = new LocationValue() { 
     @Override 
     public void getCurrentLocation(Location location) { 
      // You will get location here if the GPS is enabled 
      if(location != null) { 
       Log.d("LOCATION", location.getLatitude() + ", " + location.getLongitude()); 
      } 
     } 
    }; 

// @Override 
// public void onResume() { 
//  super.onResume(); 
// 
// } 
// 
// @Override 
// public void onDestroy() { 
//  super.onDestroy(); 
////  if (gps != null) 
////   gps.stopUsingGPS(); 
// } 
} 
+1

可能性のある重複した[inconvertable種類を解決する方法は、「パッケージ名」に「Android.support.v4.app.fragment」をキャストすることはできません](http://stackoverflow.com/questions/v8-app-fragment-t)/ 28898996/how-to-solve-inconvertable-types-can-cast-android-support-v4-app-fragment-t) –

答えて

2

です。

はSupportPlaceAutocompleteFragment代わりのPlaceAutoComplete

を使用していた古いプラットフォームをありがとう

0

がSupportPlaceAutoCompleteFragmentを使用し、私は

のアプリのGradleでGradleの でGoogleプレイスを追加することを確認し、以下のサンプルコードを追加しました

コンパイル 'com.google.android.gms:play-services-places:10.0.0'

あなたのフラグメントで今

:の

SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment) 
      getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
     @Override 
     public void onPlaceSelected(Place place) { 
      // TODO: Get info about the selected place. 
      Log.i(TAG, "Place: " + place.getName()); 
     } 

     @Override 
     public void onError(Status status) { 
      // TODO: Handle the error. 
      Log.i(TAG, "An error occurred: " + status); 
     } 
    }); 
関連する問題