Android活动无法启动

时间:2014-04-01 12:30:45

标签: android google-maps onclick android-activity

我有一个带摄像头和地图功能的应用。它工作正常,直到我试图实现它,以便我的位置坐标将显示我的地图(纬度和经度)但现在当我点击地图按钮它只是崩溃!我的应用程序的工作方式是,当我登录时,我看到一个欢迎屏幕,有3个按钮,一个摄像头,地图和注销按钮。相机和注销按钮工作正常但是地图按钮只是崩溃整个应用程序,因为我插入代码以获取我的坐标到LocationActivity类。以下是相关代码。任何帮助将不胜感激。

欢迎课程

package com.example.myapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.parse.ParseUser;

public class Welcome extends Activity {

// Declare Variable
Button logoutButton;
Button cameraButton;
Button mapButton;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from welcome.xml
    setContentView(R.layout.welcome);  

    // Retrieve current user from Parse.com
    ParseUser currentUser = ParseUser.getCurrentUser();

    // Convert currentUser into String
    String struser = currentUser.getUsername().toString();

    // Locate TextView in welcome.xml
    TextView txtuser = (TextView) findViewById(R.id.txtuser);

    // Set the currentUser String into TextView
    txtuser.setText("You are logged in as " + struser);


    // Locate Buttons in welcome.xml
    logoutButton = (Button) findViewById(R.id.logout); 
    cameraButton = (Button) findViewById(R.id.camera);
    mapButton = (Button) findViewById(R.id.map);

    // Logout Button Click Listener
    logoutButton.setOnClickListener(new OnClickListener() {

        /** Called when the user clicks the Logout button */
        public void onClick(View arg0) {
            // Logout current user
            ParseUser.logOut();
            finish();
     }

});        

    // Camera Button Click Listener
    cameraButton.setOnClickListener(new View.OnClickListener() {

        /** Called when the user clicks the Camera button */

        public void onClick(View view) {
            // Send user to Camera.class
            Intent intent = new Intent(Welcome.this, Camera.class);
             startActivity(intent);

        }

     });

 // Map Button Click Listener
    mapButton.setOnClickListener(new View.OnClickListener() {

        /** Called when the user clicks the Get Location button */

        public void onClick(View view) {
            // Send user to LocationActivity.class
            Intent intent = new Intent(Welcome.this, LocationActivity.class);
             startActivity(intent);

        }

  });

    }
  }

LocationActivity类

package com.example.myapp;

import java.io.IOException;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class LocationActivity extends Activity implements LocationListener {

private static final String TAG = "MapActivity";

LocationManager locationManager;
Geocoder geocoder;
TextView locationText;
MapView map;    
MapController mapController;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_map);

 // Get the message from the intent
    Intent intent = getIntent();

    locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
    map = (MapView)this.findViewById(R.id.map);
    map.setBuiltInZoomControls(true);

    mapController = map.getController();
    mapController.setZoom(16);

    locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);

    geocoder = new Geocoder(this);

    Location location =     locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
        Log.d(TAG, location.toString());
        this.onLocationChanged(location);   
    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);
}

protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    Log.d(TAG, "onLocationChanged with location " + location.toString());
    // Displays lat, long, altitude and bearing
    String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getBearing());
    this.locationText.setText(text);

    try {
        // This gets a list of addresses 
        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10);
        for (Address address : addresses) {
            this.locationText.append("\n" + address.getAddressLine(0));
        }

        // Convert latitude and longitude into int that the GeoPoint constructor can understand
        int latitude = (int)(location.getLatitude() * 1000000);
        int longitude = (int)(location.getLongitude() * 1000000);

        GeoPoint point = new GeoPoint(latitude,longitude);
        mapController.animateTo(point);

    } catch (IOException e) {
        Log.e("LocateMe", "Could not get Geocoder data", e);
    }
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}{




    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

activity_display_map xml布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Waiting for location..."
android:id="@+id/lblLocationInfo">

<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0vFrUOhHMkbahT9zXqiz_DuNVWfPqlEyqcO8ftg">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.MapFragment">

 </fragment>
 </com.google.android.maps.MapView>
 </TextView>
 </LinearLayout>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission        android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
 Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:name="ParseApplication"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:allowBackup="true"
  android:theme="@style/AppTheme" >

    <meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

    <activity
        android:name="com.example.myapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.example.myapp.LoginSignupActivity" >
    </activity>

    <activity android:name="com.example.myapp.Welcome" >
    </activity>

    <activity
        android:name="com.example.myapp.Camera"
        android:label="@string/title_camera"
    android:parentActivityName="com.example.myapp.MainActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myapp.MainActivity" />
    </activity>

     <activity android:name=".LocationActivity"
              android:label="@string/app_name">
        </activity>
    <uses-library android:name="com.google.android.maps" />
   </application>
 </manifest> 

字符串xml

<?xml version="1.0" encoding="utf-8"?>

<string name="hello">myapp</string>
<string name="app_name">myapp</string>
<string name="Username">Username</string>
<string name="Password">Password</string>
<string name="LoginBtn">Login</string>
<string name="SignupBtn">Sign Up</string>
<string name="LogoutBtn">Log Out</string>
 <string name="CameraBtn">Camera</string>
 <string name="MapBtn">Create a Geofence</string>
<string name="Welcome">Welcome!</string>

你好世界!    点击图像打开相机!!

  <string name="title_camera">My Message</string>
  <string name="title_map">My Message</string>

  </resources>

Logcat UPDATED

   04-01 15:05:22.548: E/AndroidRuntime(18009): FATAL EXCEPTION: main
 04-01 15:05:22.548: E/AndroidRuntime(18009): java.lang.RuntimeException: Unable to  start activity ComponentInfo{com.example.travsafe/com.example.travsafe.LocationActivity}: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.google.android.maps.MapView
 04-01 15:05:22.548: E/AndroidRuntime(18009):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
 04-01 15:05:22.548: E/AndroidRuntime(18009):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
 04-01 15:05:22.548: E/AndroidRuntime(18009):   at android.app.ActivityThread.access$600(ActivityThread.java:156)
 04-01 15:05:22.548: E/AndroidRuntime(18009):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
 04-01 15:05:22.548: E/AndroidRuntime(18009):   at android.os.Handler.dispatchMessage(Handler.java:99)
 04-01 15:05:22.548: E/AndroidRuntime(18009):   at android.os.Looper.loop(Looper.java:153)
  04-01 15:05:22.548: E/AndroidRuntime(18009):  at android.app.ActivityThread.main(ActivityThread.java:5297)
  04-01 15:05:22.548: E/AndroidRuntime(18009):  at java.lang.reflect.Method.invokeNative(Native Method)
  04-01 15:05:22.548: E/AndroidRuntime(18009):  at java.lang.reflect.Method.invoke(Method.java:511)
  04-01 15:05:22.548: E/AndroidRuntime(18009):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
  04-01 15:05:22.548: E/AndroidRuntime(18009):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
  04-01 15:05:22.548: E/AndroidRuntime(18009):  at dalvik.system.NativeStart.main(Native Method)
  04-01 15:05:22.548: E/AndroidRuntime(18009): Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.google.android.maps.MapView
  04-01 15:05:22.548: E/AndroidRuntime(18009):  at com.example.travsafe.LocationActivity.onCreate(LocationActivity.java:44)
  04-01 15:05:22.548: E/AndroidRuntime(18009):  at android.app.Activity.performCreate(Activity.java:5141)
  04-01 15:05:22.548: E/AndroidRuntime(18009):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
  04-01 15:05:22.548: E/AndroidRuntime(18009):  at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
  04-01 15:05:22.548: E/AndroidRuntime(18009):  ... 11 more
  04-01 15:05:24.547: I/Process(18009): Sending signal. PID: 18009 SIG: 9

2 个答案:

答案 0 :(得分:1)

这里有一个静态初始化程序块:

{
    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

将该代码移至onCreate() - 对于此类代码,活动init太早,如堆栈跟踪所示:

04-01 14:05:42.713: E/AndroidRuntime(14793): Caused by: java.lang.NullPointerException
04-01 14:05:42.713: E/AndroidRuntime(14793):    at android.app.Activity.initActionBar(Activity.java:1880)
04-01 14:05:42.713: E/AndroidRuntime(14793):    at android.app.Activity.getActionBar(Activity.java:1867)
04-01 14:05:42.713: E/AndroidRuntime(14793):    at  com.example.travsafe.LocationActivity.<init>(LocationActivity.java:131)

在您更新的问题中,问题及其解决方案在异常消息中可见:

java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY"  android:value="your API key"/> is in the <application> element of AndroidManifest.xml

答案 1 :(得分:0)

您是否在Manifest.xml文件中指定了APIkey ..
< meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your_API_KEY"/>
在onCreate里面得到你的地图实例..
GoogleMap map = ((MapFragment) getFragmentManager() .findFragmentById(R.id.map)).getMap(); 我希望它能帮助你:)

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Waiting for location..."
android:id="@+id/lblLocationInfo">
<fragment
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.MapFragment">

 </fragment>

 </TextView>
 </LinearLayout>