在我的手机上运行此代码时强制关闭

时间:2015-01-18 17:18:52

标签: java android api maps google-maps-android-api-2

尝试学习如何使用google maps api ,我正在关注youtube上的教程。当我运行给定代码时,应用程序关闭&按摩" ...已停止响应" 显示。请帮助。

以下是代码:

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends FragmentActivity {

    private GoogleMap mainMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpMap();    //line 27

    }

    private void setUpMap() {

        if(mainMap == null) {
            mainMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();    //line 34      
            if(mainMap != null) {
                startMap();
            }
        }

    }

    private void startMap() {       

        mainMap.setMyLocationEnabled(true);
        mainMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);  
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();     //retrieve provider
        String provider = locationManager.getBestProvider(criteria, true);      //name of best provider
        Location myLocation = locationManager.getLastKnownLocation(provider);           

        double myLat = myLocation.getLatitude();
        double myLong = myLocation.getLongitude();

        LatLng latLng = new LatLng(myLat, myLong);

        mainMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mainMap.animateCamera(CameraUpdateFactory.zoomTo(20));
        mainMap.addMarker(new MarkerOptions().position(new LatLng(myLat, myLong)).title("You are here"));

    }




    @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;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_settings) {           
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

}

这是logcat:

01-18 12:16:20.915: E/AndroidRuntime(5163): FATAL EXCEPTION: main
01-18 12:16:20.915: E/AndroidRuntime(5163): Process: com.example.map, PID: 5163
01-18 12:16:20.915: E/AndroidRuntime(5163): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.map/com.example.map.MainActivity}: java.lang.NullPointerException
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread.access$800(ActivityThread.java:142)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.os.Looper.loop(Looper.java:136)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread.main(ActivityThread.java:5118)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at java.lang.reflect.Method.invoke(Method.java:515)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at dalvik.system.NativeStart.main(Native Method)
01-18 12:16:20.915: E/AndroidRuntime(5163): Caused by: java.lang.NullPointerException
01-18 12:16:20.915: E/AndroidRuntime(5163):     at com.example.map.MainActivity.setUpMap(MainActivity.java:34)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at com.example.map.MainActivity.onCreate(MainActivity.java:27)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.Activity.performCreate(Activity.java:5275)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-18 12:16:20.915: E/AndroidRuntime(5163):     ... 11 more

这是layout.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp" />

</LinearLayout> 

3 个答案:

答案 0 :(得分:1)

或者:

  • getSupportFragmentManager()正在返回null,这不太可能,或者

  • findFragmentById()正在返回null

如果R.id.map文件中没有名为res/layout/activity_main.xml的片段,则可能会发生后者。

<强>更新

您的布局正在使用com.google.android.gms.maps.MapFragment。这不适用于FragmentActivity。如果您要使用com.google.android.gms.maps.SupportMapFragment和片段反向移植,请使用FragmentActivity

答案 1 :(得分:0)

您正在尝试使用首先未初始化的方法getMap()。没有它就试试吧。

mainMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));    //line 34

答案 2 :(得分:0)

因为您使用onCreate设置了地图。我可以看到任何导致该应用无法显示的内容,然后Android会报告它已停止响应等等。

尝试在onStart中抛出setUpMap()调用,将其包装在Try Catch中,看看会发生什么。为了获得更好的性能,请将该调用放在它自己的线程上。这样就不会影响启动时间。

@Override
protected void onStart() {
    setUpMap();
}

private void setUpMap() {
    try {
        // your code here
    } catch (Exception e) {

    }
}