我有一个带有地图视图的 MapActivity。它工作正常。现在我想在我的 MapView 上添加一个 SeekBar。它必须在底部,但在地图视图上。用户将滚动以设置距离。任何。我的问题是,当我通过 findViewById() 到达它们时,我试图放置的任何视图都是空的。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="mykey"/>
<SeekBar
android:id="@+id/DistanceBar"
android:max="2000"
android:progress="2000"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="14dip"></SeekBar>
</RelativeLayout>
我试图达到这样的效果:
public class MyActivity extends MapActivity implements LocationListener {
...
SeekBar DistanceBar;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
DistanceBar = (SeekBar) findViewById(R.id.DistanceBar); //This is afterwards null.
我到处进行了研究,找不到类似的问题。有人使用了非常相似的东西。所以我忽略了它是空的,我没有在 onCreate() 中调用 SeekBar。但是 SeekBar 根本没有被绘制。我不能将其他视图添加到地图活动吗?
感谢您的任何想法!
一个重要的编辑:图形布局查看器显示 SeekBar,因为它应该在地图视图的底部。
我仍然得到 NullPointerException: ERROR/AndroidRuntime(12738): FATAL EXCEPTION: main
错误/AndroidRuntime(12738): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.MyApps.MapLocations/com.MyApps.MapLocations.MyActivity}: java.lang.NullPointerException
错误/AndroidRuntime(12738):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
错误/AndroidRuntime(12738):在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
错误/AndroidRuntime(12738):在 android.app.ActivityThread.access$2300(ActivityThread.java:125)
错误/AndroidRuntime(12738):在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
错误/AndroidRuntime(12738):在 android.os.Handler.dispatchMessage(Handler.java:99)
错误/AndroidRuntime(12738): 在 android.os.Looper.loop(Looper.java:123)
错误/AndroidRuntime(12738):在 android.app.ActivityThread.main(ActivityThread.java:4627)
错误/AndroidRuntime(12738):在 java.lang.reflect.Method.invokeNative(Native Method)
错误/AndroidRuntime(12738):在 java.lang.reflect.Method.invoke(Method.java:521)
错误/AndroidRuntime(12738):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
错误/AndroidRuntime(12738):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
错误/AndroidRuntime(12738):在 dalvik.system.NativeStart.main(本机方法)
错误/AndroidRuntime(12738):由:java.lang.NullPointerException 引起
错误/AndroidRuntime(12738):在 com.MyApps.MapLocations.MyActivity.setupSeekbar(MyActivity.java:343)
错误/AndroidRuntime(12738):在 com.MyApps.MapLocations.MyActivity.onCreate(MyActivity.java:89)
错误/AndroidRuntime(12738):在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
错误/AndroidRuntime(12738):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
错误/AndroidRuntime(12738): ... 11 更多
这是 HierarchyViewer 的结果: