尝试实现 GPS 定位器时的运行时异常



我正在努力将GPS定位服务添加到我的应用程序中。但是我得到这个错误,我似乎无法修复。我已经包含了我所有相关的编码。如果有人对我的任何编码有任何与错误无关的评论,我也将不胜感激。

日志猫

 08-09 11:19:24.602: E/AndroidRuntime(715): FATAL EXCEPTION: main
08-09 11:19:24.602: E/AndroidRuntime(715): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.proto1/com.example.com.proto1.menu}: android.view.InflateException: Binary XML file line #77: Error inflating class com.google.android.maps.MapView
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.os.Looper.loop(Looper.java:137)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-09 11:19:24.602: E/AndroidRuntime(715):  at java.lang.reflect.Method.invokeNative(Native Method)
08-09 11:19:24.602: E/AndroidRuntime(715):  at java.lang.reflect.Method.invoke(Method.java:511)
08-09 11:19:24.602: E/AndroidRuntime(715):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-09 11:19:24.602: E/AndroidRuntime(715):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-09 11:19:24.602: E/AndroidRuntime(715):  at dalvik.system.NativeStart.main(Native Method)
08-09 11:19:24.602: E/AndroidRuntime(715): Caused by: android.view.InflateException: Binary XML file line #77: Error inflating class com.google.android.maps.MapView
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.view.LayoutInflater.createView(LayoutInflater.java:613)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-09 11:19:24.602: E/AndroidRuntime(715):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.app.Activity.setContentView(Activity.java:1867)
08-09 11:19:24.602: E/AndroidRuntime(715):  at com.example.com.proto1.menu.onCreate(menu.java:62)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.app.Activity.performCreate(Activity.java:5008)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-09 11:19:24.602: E/AndroidRuntime(715):  ... 11 more
08-09 11:19:24.602: E/AndroidRuntime(715): Caused by: java.lang.reflect.InvocationTargetException
08-09 11:19:24.602: E/AndroidRuntime(715):  at java.lang.reflect.Constructor.constructNative(Native Method)
08-09 11:19:24.602: E/AndroidRuntime(715):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-09 11:19:24.602: E/AndroidRuntime(715):  at android.view.LayoutInflater.createView(LayoutInflater.java:587)
08-09 11:19:24.602: E/AndroidRuntime(715):  ... 22 more
08-09 11:19:24.602: E/AndroidRuntime(715): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
08-09 11:19:24.602: E/AndroidRuntime(715):  at com.google.android.maps.MapView.<init>(MapView.java:291)
08-09 11:19:24.602: E/AndroidRuntime(715):  at com.google.android.maps.MapView.<init>(MapView.java:264)
08-09 11:19:24.602: E/AndroidRuntime(715):  at com.google.android.maps.MapView.<init>(MapView.java:247)
08-09 11:19:24.602: E/AndroidRuntime(715):  ... 25 more

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.com.proto1"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:icon="@drawable/theeye"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".mainj"
            android:label="@string/title_activity_mainj" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MENU" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Infoactive"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.INFOSCREEN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".VoicePrompts"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VOICEPROMPTS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".VPon"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VPON" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".VPoff"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VPOFF" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <!-- android name must match the name of the java you want to use -->
        <activity
            android:name=".VoiceRecognition"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.RECOGNITIONMENU" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Recognition"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="ACTION_RECOGNIZE_SPEECH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SpeakingAndroid"
            android:label="tts" >
            <intent-filter>
                <action android:name="android.intent.action.SPEAK" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MyGPSActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="GPS_LOCATION" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <library
            name="com.google.android.maps"
            file="/system/framework/com.google.android.maps.jar" />
        <uses-library android:name="com.google.android.maps" />
    </application>
</manifest>

主 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="100" >
    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="5dp" >
    </ListView>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:gravity="bottom"
        android:orientation="horizontal"
        android:weightSum="100" >
        <Button
            android:id="@+id/aboutbutton"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="50"
            android:gravity="center"
            android:text="@string/a"
            android:textSize="25dp" />
        <Button
            android:id="@+id/talk"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="50"
            android:gravity="center"
            android:text="talk"
            android:textSize="25dp" />
    </LinearLayout>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal"
        android:weightSum="100" >
        <Button
            android:id="@+id/voicebutton"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="50"
            android:gravity="center"
            android:text="@string/starttalking"
            android:textSize="25dp" />
        <Button
            android:id="@+id/btn_speak"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="50"
            android:gravity="center"
            android:text="@string/start"
            android:textSize="25dp" />
    </LinearLayout>
    <com.google.android.maps.MapView
        android:id="@+id/myGMap"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="API_Key_String"
        android:clickable="true"
        android:enabled="true" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />
</RelativeLayout>

菜单(主) 爪哇

package com.example.com.proto1;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.speech.tts.TextToSpeech;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import com.example.com.proto1.MyGPSActivity.LocationResult;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
@SuppressWarnings("unused")
public class menu extends Activity implements TextToSpeech.OnInitListener,
        OnClickListener {
    LocationResult locationResult = new LocationResult() {
        @Override
        public void gotLocation(Location location) {
            // Got the location!
        }
    };
    MyGPSActivity myLocation = new MyGPSActivity();
    // defined
    TextToSpeech mTts;
    public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
    // remember to include a listview on the xml or the voice recognition code
    // will not work
    public ListView mList;
    // TTS object
    Button speakButton, infoButton, voiceButton, talkButton;
    // TTS object
    public TextToSpeech myTTS;
    // status check code
    public int MY_DATA_CHECK_CODE = 0;
    @Override
    protected void onCreate(Bundle aboutmenu) {
        super.onCreate(aboutmenu);
        setContentView(R.layout.mainx);
        SpeakingAndroid speak = new SpeakingAndroid();
        VoiceRecognition voiceinput = new VoiceRecognition();
        // get a reference to the button element listed in the XML layout
        speakButton = (Button) findViewById(R.id.btn_speak);
        infoButton = (Button) findViewById(R.id.aboutbutton);
        voiceButton = (Button) findViewById(R.id.voicebutton);
        talkButton = (Button) findViewById(R.id.talk);
        // listen for clicks
        infoButton.setOnClickListener(this);
        speakButton.setOnClickListener(this);
        talkButton.setOnClickListener(this);
        // check for TTS data
        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
        // calling method
        voiceinputbuttons();
        // Check to see if a recognition activity is present
        // if running on AVD virtual device it will give this message. The mic
        // required only works on an actual android device//
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() != 0) {
            voiceButton.setOnClickListener(this);
        } else {
            voiceButton.setEnabled(false);
            voiceButton.setText("Recognizer not present");
        }
    }
    // setup TTS
    public void onInit(int initStatus) {
        // check for successful instantiation
        // returns a fail statement if speech doesn't work
        if (initStatus == TextToSpeech.SUCCESS) {
            if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        } else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...",
                    Toast.LENGTH_LONG).show();
        }
    }
    public void informationmenu() {
        speakWords("information screen");
        startActivity(new Intent("android.intent.action.INFOSCREEN"));
    }
    public void voicemenu() {
        speakWords("voice recognition menu");
        startActivity(new Intent("android.intent.action.RECOGNITIONMENU"));
    }
    public void mainmenu() {
        speakWords("main menu");
        startActivity(new Intent("android.intent.action.MENU"));
    }
    // creating method
    public void voiceinputbuttons() {
        speakButton = (Button) findViewById(R.id.btn_speak);
        mList = (ListView) findViewById(R.id.list);
    }
    // respond to button clicks
    public void onClick(View v) {
        switch (v.getId()) {
        // use switch case so each button does a different thing
        // accurately(similar to an if statement)
        case R.id.btn_speak:
            String words1 = speakButton.getText().toString();
            // speakwords(xxxx); is the piece of code that actually calls the
            // text to speech
            speakWords(words1);
            myLocation.getLocation(this, locationResult);
            break;
        case R.id.aboutbutton:
            String words2 = infoButton.getText().toString();
            speakWords(words2);
            Intent infoIntent = new Intent("android.intent.action.INFOSCREEN");
            startActivity(infoIntent);
            break;
        case R.id.voicebutton:
            speakWords("Speak Now");
            startVoiceRecognitionActivity(); // call for voice recognition
                                                // activity
            break;
        case R.id.talk:
            speakWords("This is the main menu.");
            break;
        }
    }
    // speak the user text
    // setting up the speakWords code
    public void speakWords(String speech) {
        // speak straight away
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
    }
    /**
     * Fire an intent to start the speech recognition activity.
     */
    public void startVoiceRecognitionActivity() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "Speech recognition demo");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }
    /**
     * Handle the results from the recognition activity.
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
                && resultCode == RESULT_OK) {
            // Fill the list view with the strings the recognizer thought it
            // could have heard
            ArrayList<String> matches = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, matches));
            // matches is the result of voice input. It is a list of what the
            // user possibly said.
            // Using an if statement for the keyword you want to use allows the
            // use of any activity if keywords match
            // it is possible to set up multiple keywords to use the same
            // activity so more than one word will allow the user
            // to use the activity (makes it so the user doesn't have to
            // memorize words from a list)
            // to use an activity from the voice input information simply use
            // the following format;
            // if (matches.contains("keyword here") { startActivity(new
            // == Intent("name.of.manifest.ACTIVITY")
            if (matches.contains("information")) {
                informationmenu();
            }
            if (matches.contains("info screen")) {
                informationmenu();
            }
            if (matches.contains("info")) {
                informationmenu();
            }
            if (matches.contains("about")) {
                informationmenu();
            }
            if (matches.contains("home")) {
                mainmenu();
            }
            if (matches.contains("menu")) {
                mainmenu();
            }
            if (matches.contains("home screen")) {
                mainmenu();
            }
            if (matches.contains("speak")) {
                startActivity(new Intent("android.intent.action.SPEAK"));
            }
            if (matches.contains("close")) {
                finish();
            }
            if (matches.contains("stop")) {
                finish();
            }
            if (matches.contains("finish")) {
                finish();
            }
            if (matches.contains("voice")) {
                voicemenu();
            }
            if (matches.contains("recognition")) {
                voicemenu();
            }
            if (matches.contains("voice recognition")) {
                voicemenu();
            }
        }
        // still in the onActivityResult: This is for the text to speech part
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // the user has the necessary data - create the TTS
                myTTS = new TextToSpeech(this, this);
            } else {
                // no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent
                        .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        myTTS.shutdown();
    }
}

全球定位系统爪哇

package com.example.com.proto1;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class MyGPSActivity {
    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    boolean gps_enabled = false;
    boolean network_enabled = false;
    public boolean getLocation(Context context, LocationResult result) {
        // I use LocationResult callback class to pass location value from
        // MyLocation to user code.
        locationResult = result;
        if (lm == null)
            lm = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);
        // exceptions will be thrown if provider is not permitted.
        try {
            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
        }
        try {
            network_enabled = lm
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }
        // don't start listeners if no provider is enabled
        if (!gps_enabled && !network_enabled)
            return false;
        if (gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                    locationListenerGps);
        if (network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                    locationListenerNetwork);
        timer1 = new Timer();
        timer1.schedule(new GetLastLocation(), 20000);
        return true;
    }
    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {
        }
        public void onProviderEnabled(String provider) {
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {
        }
        public void onProviderEnabled(String provider) {
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
            lm.removeUpdates(locationListenerGps);
            lm.removeUpdates(locationListenerNetwork);
            Location net_loc = null, gps_loc = null;
            if (gps_enabled)
                gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (network_enabled)
                net_loc = lm
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            // if there are both values use the latest one
            if (gps_loc != null && net_loc != null) {
                if (gps_loc.getTime() > net_loc.getTime())
                    locationResult.gotLocation(gps_loc);
                else
                    locationResult.gotLocation(net_loc);
                return;
            }
            if (gps_loc != null) {
                locationResult.gotLocation(gps_loc);
                return;
            }
            if (net_loc != null) {
                locationResult.gotLocation(net_loc);
                return;
            }
            locationResult.gotLocation(null);
        }
    }
    public static abstract class LocationResult {
        public abstract void gotLocation(Location location);
    }
}

将以下行放在 AndroidManifest.xml 文件的应用程序元素中。

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

最新更新