0

public class MapActivity extends Activity implements LocationListene在 onCreate 方法中有我要加载的位置:

protected void onCreate(Bundle savedInstanceState) {

        try {
            super.onCreate(savedInstanceState);   
            setContentView(R.layout.mapview);

            ...
        }
}

地图视图.xml:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/ll_mapView"
              android:background="@android:color/white">

   <another_working_layout_here>

    <map.MyLocation
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    </map.MyLocation>

</LinearLayout>

作为一个从类加载视图:

public class MyLocation extends View {
    Paint paint = new Paint();

    public MyLocation(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(100, 100, 100, paint);
        this.invalidate();
    }
}

一般来说,我想使用onLocationChangedmapActivity 中的方法从 android gps 读取,添加更改点,然后在class MyLocation extends View使用 onDraw 方法完成的扩展视图上打印它。我确定我不完全理解这个概念,但不知道如何以不同的方式做到这一点。

我的错误是:

10-28 16:06:40.147: ERROR/myException(2869): android.view.InflateException: Binary XML file line #26: Error inflating class map.MyLocation
10-28 16:07:06.478: ERROR/ActivityManager(1120): ANR in com.example.gps_3 (com.example.gps_3/activities.MapActivity)
        Reason: keyDispatchingTimedOut
        Load: 0.26 / 0.25 / 0.76
        CPU usage from 39586ms to 535ms ago:
        2.2% 950/adbd: 0% user + 2.2% kernel
        0% 932/yaffs-bg-1: 0% user + 0% kernel
        0.1% 2869/com.example.gps_3: 0% user + 0.1% kernel / faults: 18 minor
        0.1% 1120/system_server: 0% user + 0% kernel
        0% 1391/com.android.phone: 0% user + 0% kernel / faults: 92 minor
        0% 930/yaffs-bg-1: 0% user + 0% kernel
        0% 1281/com.android.systemui: 0% user + 0% kernel / faults: 6 minor
        0% 1404/com.android.launcher: 0% user + 0% kernel
        0% 2789/com.android.browser: 0% user + 0% kernel / faults: 89 minor
        48% TOTAL: 0.6% user + 45% kernel + 1.8% softirq
        CPU usage from 403ms to 914ms later:
        1.9% 1120/system_server: 0% user + 1.9% kernel
        100% TOTAL: 0% user + 100% kernel
10-28 16:12:04.312: ERROR/dalvikvm(1120): JIT code cache full
10-28 16:12:04.322: ERROR/InputDispatcher(1120): channel 'b353cf20 com.example.gps_3/activities.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-28 16:12:04.322: ERROR/InputDispatcher(1120): channel 'b350af88 com.example.gps_3/activities.MapActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-28 16:12:05.092: ERROR/jdwp(2914): Failed sending reply to debugger: Broken pipe
10-28 16:13:04.613: ERROR/myException(2914): android.view.InflateException: Binary XML file line #26: Error inflating class map.MyLocation

编辑:

异常被调用:

  @Override
    protected void onCreate(Bundle savedInstanceState) {

        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mapview);
            myLocationManager = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);
            myLocationLister = this;
            myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationLister);
        } catch (Exception e) {
            Log.e("myException", e.toString());      
        }

    }

setContentView(R.layout.mapview);行。

我会很感激任何想法。谢谢

问题在于缺少构造函数:

public MyLocation(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

连接有用的主题: Android 自定义视图是否需要所有三个构造函数?

4

0 回答 0