0

I want to Develop an Application in which I can Auto search places and display the place on google map, I followed the this link but when I run the App then crash and display the error

XMl

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<com.example.awais.mapapp1.CustomAutoCompleteTextView
    android:id="@+id/atv_places"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:hint="@string/str_atv_places"
    android:singleLine="true" />

<fragment
    android:id="@+id/map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_below="@id/atv_places" />

MainActivity

public class MainActivity extends FragmentActivity {

AutoCompleteTextView atvPlaces;

DownloadTask placesDownloadTask;
DownloadTask placeDetailsDownloadTask;
ParserTask placesParserTask;
ParserTask placeDetailsParserTask;

GoogleMap mMap;

final int PLACES=0;
final int PLACES_DETAILS=1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Getting a reference to the AutoCompleteTextView
    atvPlaces = (AutoCompleteTextView) findViewById(R.id.atv_places);
    atvPlaces.setThreshold(1);
    // Adding textchange listener
    atvPlaces.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Creating a DownloadTask to download Google Places matching "s"
            placesDownloadTask = new DownloadTask(PLACES);

            // Getting url to the Google Places Autocomplete api
            String url = getAutoCompleteUrl(s.toString());

            // Start downloading Google Places
            // This causes to execute doInBackground() of DownloadTask class
            placesDownloadTask.execute(url);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });

    // Setting an item click listener for the AutoCompleteTextView dropdown list
    atvPlaces.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int index,
                                long id) {

            ListView lv = (ListView) arg0;
            SimpleAdapter adapter = (SimpleAdapter) arg0.getAdapter();

            HashMap<String, String> hm = (HashMap<String, String>) adapter.getItem(index);
            // Creating a DownloadTask to download Places details of the selected place
            placeDetailsDownloadTask = new DownloadTask(PLACES_DETAILS);
            // Getting url to the Google Places details api
            String url = getPlaceDetailsUrl(hm.get("reference"));
            // Start downloading Google Place Details
            // This causes to execute doInBackground() of DownloadTask class
            placeDetailsDownloadTask.execute(url);

        }
    });
}

private String getAutoCompleteUrl(String place){
    // Obtain browser key from https://code.google.com/apis/console
    String key = "key=AIzaSyCf6mbMvnLml4v-9Qls_oTmq4LvN_Qu2ec";
    // place to be be searched
    String input = "input="+place;
    // place type to be searched
    String types = "types=geocode";
    // Sensor enabled
    String sensor = "sensor=false";
    // Building the parameters to the web service
    String parameters = input+"&"+types+"&"+sensor+"&"+key;
    // Output format
    String output = "json";
    // Building the url to the web service
    String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;
    return url;
}

private String getPlaceDetailsUrl(String ref){

    // Obtain browser key from https://code.google.com/apis/console
    String key = "key=AIzaSyCf6mbMvnLml4v-9Qls_oTmq4LvN_Qu2ec";
    // reference of place
    String reference = "reference="+ref;
    // Sensor enabled
    String sensor = "sensor=false";
    // Building the parameters to the web service
    String parameters = reference+"&"+sensor+"&"+key;
    // Output format
    String output = "json";
    // Building the url to the web service
    String url = "https://maps.googleapis.com/maps/api/place/details/"+output+"?"+parameters;

    return url;
}

/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException {
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try{
        URL url = new URL(strUrl);
        // Creating an http connection to communicate with url
        urlConnection = (HttpURLConnection) url.openConnection();
        // Connecting to url
        urlConnection.connect();
        // Reading data from url
        iStream = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

        StringBuffer sb  = new StringBuffer();

        String line = "";
        while( ( line = br.readLine())  != null){
            sb.append(line);
        }

        data = sb.toString();

        br.close();

    }catch(Exception e){
        Log.d("Exception while downloading url", e.toString());
    }finally{
        iStream.close();
        urlConnection.disconnect();
    }
    return data;
}

// Fetches data from url passed
private class DownloadTask extends AsyncTask<String, Void, String> {

    private int downloadType=0;
    // Constructor
    public DownloadTask(int type){
        this.downloadType = type;
    }

    @Override
    protected String doInBackground(String... url) {

        // For storing data from web service
        String data = "";
        try{
            // Fetching the data from web service
            data = downloadUrl(url[0]);
        }catch(Exception e){
            Log.d("Background Task",e.toString());
        }
        return data;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        switch(downloadType){
            case PLACES:
                // Creating ParserTask for parsing Google Places
                placesParserTask = new ParserTask(PLACES);

                // Start parsing google places json data
                // This causes to execute doInBackground() of ParserTask class
                placesParserTask.execute(result);

                break;

            case PLACES_DETAILS :
                // Creating ParserTask for parsing Google Places
                placeDetailsParserTask = new ParserTask(PLACES_DETAILS);

                // Starting Parsing the JSON string
                // This causes to execute doInBackground() of ParserTask class
                placeDetailsParserTask.execute(result);
        }
    }
}

/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>> implements OnMapReadyCallback {

    int parserType = 0;

    public ParserTask(int type){
        this.parserType = type;
    }

    @Override
    protected List<HashMap<String, String>> doInBackground(String... jsonData) {

        JSONObject jObject;
        List<HashMap<String, String>> list = null;

        try{
            jObject = new JSONObject(jsonData[0]);

            switch(parserType){
                case PLACES :
                    PlaceJSONParser placeJsonParser = new PlaceJSONParser();
                    // Getting the parsed data as a List construct
                    list = placeJsonParser.parse(jObject);
                    break;
                case PLACES_DETAILS :
                    PlaceDetailsJSONParser placeDetailsJsonParser = new PlaceDetailsJSONParser();
                    // Getting the parsed data as a List construct
                    list = placeDetailsJsonParser.parse(jObject);
            }

        }catch(Exception e){
            Log.d("Exception",e.toString());
        }
        return list;
    }

    @Override
    protected void onPostExecute(List<HashMap<String, String>> result) {

        switch(parserType){
            case PLACES :
                String[] from = new String[] { "description"};
                int[] to = new int[] { android.R.id.text1 };
                // Creating a SimpleAdapter for the AutoCompleteTextView
                SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);
                // Setting the adapter
                atvPlaces.setAdapter(adapter);
                break;
            case PLACES_DETAILS :
                HashMap<String, String> hm = result.get(0);
                // Getting latitude from the parsed data
                double latitude = Double.parseDouble(hm.get("lat"));
                // Getting longitude from the parsed data
                double longitude = Double.parseDouble(hm.get("lng"));
                // Getting reference to the SupportMapFragment of the activity_main.xml
                SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                // Getting GoogleMap from SupportMapFragment
                fm.getMapAsync(this);

                LatLng point = new LatLng(latitude, longitude);

                CameraUpdate cameraPosition = CameraUpdateFactory.newLatLng(point);
                CameraUpdate cameraZoom = CameraUpdateFactory.zoomBy(5);
                // Showing the user input location in the Google Map
               mMap.moveCamera(cameraPosition);
             mMap.animateCamera(cameraZoom);

                MarkerOptions options = new MarkerOptions();
                options.position(point);
                options.title("Position");
                options.snippet("Latitude:"+latitude+",Longitude:"+longitude);
                // Adding the marker in the Google Map
              mMap.addMarker(options);

                break;
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap=googleMap;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

logcat

FATAL EXCEPTION: main Process: com.example.awais.mapapp1, PID: 13874 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.awais.mapapp1/com.example.awais.mapapp1.MainActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.example.awais.mapapp1.CustomAutoCompleteTextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5452) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:762) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.example.awais.mapapp1.CustomAutoCompleteTextView at android.view.LayoutInflater.inflate(LayoutInflater.java:539) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:400) at android.app.Activity.setContentView(Activity.java:2172) at com.example.awais.mapapp1.MainActivity.onCreate(MainActivity.java:55) at android.app.Activity.performCreate(Activity.java:6251) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5452)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:762)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)  Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.awais.mapapp1.CustomAutoCompleteTextView at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:400)  at android.app.Activity.setContentView(Activity.java:2172)  at com.example.awais.mapapp1.MainActivity.onCreate(MainActivity.java:55)  at android.app.Activity.performCreate(Activity.java:6251)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5452)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:762)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)  Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.awais.mapapp1.CustomAutoCompleteTextView" on path: DexPathList[[zip file "/data/app/com.example.awais.mapapp1-2/base.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.awais.mapapp1-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.awais.mapapp1-2/lib/arm, /vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) at android.view.LayoutInflater.createView(LayoutInflater.java:583) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)  at android.view.LayoutInflater.inflate(LayoutInflater.java:515)  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:400)  at android.app.Activity.setContentView(Activity.java:2172)  at com.example.awais.mapapp1.MainActivity.onCreate(MainActivity.java:55)  at android.app.Activity.performCreate(Activity.java:6251)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5452)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:762)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)  Suppressed: java.lang.ClassNotFoundException: com.example.awais.mapapp1.CustomAutoCompleteTextView at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.

ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
  • Please post the error stacktrace – Atish Agrawal Apr 11 '17 at 09:35
  • The issue is with `com.example.awais.mapapp1.CustomAutoCompleteTextView`. – azizbekian Apr 11 '17 at 09:37
  • 1
    The issue: `java.lang.ClassNotFoundException: Didn't find class "com.example.awais.mapapp1.CustomAutoCompleteTextView" ` - make sure that class and package is correct. If it is a Gradle dependency that is missing, make sure you add it. – Zoe Apr 11 '17 at 09:37
  • @LunarWatcher Both are correct – Mahmood T Jan Apr 11 '17 at 09:41
  • @azizbekian i change it but also get this error – Mahmood T Jan Apr 11 '17 at 09:42
  • PLease add the layout and the code for CustomAutoCOmpleteTextView, – Zoe Apr 11 '17 at 09:48
  • See this link this may be causing problem http://stackoverflow.com/questions/3739661/error-inflating-when-extending-a-class – User Apr 11 '17 at 09:55
  • @LunarWatcher please check now – Mahmood T Jan Apr 11 '17 at 09:59
  • Where is CustomAutoCOmpleteView? – Zoe Apr 11 '17 at 10:06
  • //Setting an item click listener for the AutoCompleteTextView dropdown list atvPlaces.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> arg0, View arg1, int index, long id) { ListView lv = (ListView) arg0; SimpleAdapter adapter = (SimpleAdapter) arg0.getAdapter(); HashMap hm = (HashMap) adapter.getItem(index); – Mahmood T Jan Apr 11 '17 at 10:10
  • @LunarWatcher please Please Sir just click the below link i follow the below link A to Z there is my code and i am beginner please help me – Mahmood T Jan Apr 11 '17 at 10:12
  • Is CustomAutoCompleteView your class, or is it something you imported? SHow the code for CustomAutoCompleteVIew, not how you implement it – Zoe Apr 11 '17 at 10:13
  • @LunarWatcher sir i uploaded my MainActivity please check now – Mahmood T Jan Apr 11 '17 at 10:20
  • MainActivity is not the view you are using in the XML layout! – Zoe Apr 11 '17 at 10:23
  • @LunarWatcher now what should i do? – Mahmood T Jan Apr 11 '17 at 10:27
  • see @KamranAhmed's answer. His is the closest you get without adding the code for CustomAutoCOmpleteTextView. Until the code (or dependency) for that class is in the question, the only answers you will get are guesses – Zoe Apr 11 '17 at 10:29

1 Answers1

1

You will need to go through the checklist below and your issue will (probably) be fixed:

  • Check if the package and class name is written properly in your layout XML file
  • If your project has multiple modules, check for availability of the class com.example.awais.mapapp1.CustomAutoCompleteTextView in the module where it is being used in the layout XML
  • If you are using ProGuard/DexGuard or any such tools, ensure that it is not being renamed while the XML is untouched (that should ideally never happen).
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55