当前位置: 首页 > 知识库问答 >
问题:

JAVAlang.RuntimeException:无法启动活动组件信息{.MainActivity}:android。看法充气异常:二进制XML文件行#7

卫博
2023-03-14

我想开发一个应用程序,在其中我可以自动搜索地点,并在谷歌地图上显示该地点,我遵循了这个链接,但当我运行应用程序时,然后崩溃并显示错误

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" />

主要活动

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

致命异常:主进程:com。实例阿瓦伊斯。mapapp1,PID:13874 java。lang.RuntimeException:无法启动活动组件信息{com.example.awais.mapapp1/com.example.awais.mapapp1.MainActivity}:android。看法充气异常:二进制XML文件行#7:二进制XML文件行#7:错误充气类com。实例阿瓦伊斯。mapapp1。android上的CustomAutoCompleteTextView。应用程序。活动线程。android上的performLaunchActivity(ActivityThread.java:2417)。应用程序。活动线程。android上的handleLaunchActivity(ActivityThread.java:2477)。应用程序。活动线程-android上的wrap11(ActivityThread.java)。应用程序。android上的ActivityThread$H.handleMessage(ActivityThread.java:1345)。操作系统。汉德勒。android上的dispatchMessage(Handler.java:102)。操作系统。活套。android上的loop(Looper.java:148)。应用程序。活动线程。java上的main(ActivityThread.java:5452)。朗,反思一下。方法在com上调用(本机方法)。Android内部的操作系统。ZygoteInit$MethodandArgscaler。在com上运行(zyteinit.java:762)。Android内部的操作系统。合子体。main(ZygoteInit.java:652)由以下原因引起:android。看法充气异常:二进制XML文件行#7:二进制XML文件行#7:错误充气类com。实例阿瓦伊斯。mapapp1。android上的CustomAutoCompleteTextView。看法更平坦。在android上充气(LayoutInflater.java:539)。看法更平坦。在android上充气(LayoutInflater.java:423)。看法更平坦。在com上充气(LayoutInflater.java:374)。Android内部的政策电话窗口。android上的setContentView(PhoneWindow.java:400)。应用程序。活动com上的setContentView(Activity.java:2172)。实例阿瓦伊斯。mapapp1。主要活动。android上的onCreate(MainActivity.java:55)。应用程序。活动android上的performCreate(Activity.java:6251)。应用程序。仪器。android上的callActivityOnCreate(Instrumentation.java:1110)。应用程序。活动线程。在android上执行LaunchActivity(ActivityThread.java:2370)。应用程序。活动线程。handleLaunchActivity(ActivityThread.java:2477)

共有1个答案

虞博涛
2023-03-14

您将需要通过下面的清单,您的问题将(可能)被修复:

  • 检查包和类名是否正确写入布局XML文件
  • 如果您的项目有多个模块,请检查类com的可用性。实例阿瓦伊斯。mapapp1。CustomAutoCompleteTextView在模块中,它在布局XML中使用
  • 如果您使用的是ProGuard/DexGuard或任何此类工具,请确保在XML未被触及的情况下(理想情况下,这种情况永远不会发生)不会重命名它
 类似资料: