使用MapView的RelativeLayout中的视图为空

问题描述:

我有一个带有mapview的MapActivity。它工作正常。现在我想将一个SeekBar添加到我的MapView中。它必须在底部,但在地图上。用户将滚动设置距离。随你。我的问题是,我试图放在那里的任何视图都是null,当我通过findViewById()到达它们时。使用MapView的RelativeLayout中的视图为空

<?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一直没有被绘制。我不能将其他视图添加到mapactivity?

感谢您的任何想法!

一个重要的编辑:图形布局查看器显示SeekBar,因为它应该在mapview的底部。

我仍然得到NullPointerException异常: ERROR/AndroidRuntime(12738):致命异常:主要

ERROR/AndroidRuntime(12738):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.MyApps.MapLocations/com.MyApps.MapLocations.MyActivity}:显示java.lang.NullPointerException

ERROR/AndroidRuntime(12738):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

ERROR/AndroidRuntime(12738) :在android.app.ActivityThread.handleLaunchActivity(ActivityThread.jav一:2679)

ERROR/AndroidRuntime(12738):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)

ERROR/AndroidRuntime(12738):在android.app.ActivityThread $ H .handleMessage(ActivityThread.java:2033)

ERROR/AndroidRuntime(12738):在android.os.Handler.dispatchMessage(Handler.java:99)

ERROR/AndroidRuntime(12738):在android.os .Looper.loop(Looper.java:123)

ERROR/AndroidRuntime(1273 8):在android.app.ActivityThread.main(ActivityThread.java:4627)

ERROR/AndroidRuntime(12738):在java.lang.reflect.Method.invokeNative(本机方法)

ERROR/AndroidRuntime (12738):at java.lang.reflect.Method.invoke(Method.java:521)

ERROR/AndroidRuntime(12738):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java :871)

ERROR/AndroidRuntime(12738):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

ERROR/AndroidRuntime(12738):在dalvik.system.NativeStart.main(本地方法)

错误/ AndroidRuntime(12738):引起:java.lang。的NullPointerException

ERROR/AndroidRuntime(12738):在com.MyApps.MapLocations.MyActivity.setupSeekbar(MyActivity.java:343)

ERROR/AndroidRuntime(12738):在com.MyApps.MapLocations.MyActivity.onCreate (MyActivity.java:89)

ERROR/AndroidRuntime(12738):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

ERROR/AndroidRuntime(12738):在android.app.ActivityThread .performLaunchActivity(ActivityThread.java:2627)

ERROR/AndroidRuntime(12738):...... 11多个

这里是HierarchyViewer结果: Here you can seen that there are 2 Views in the layout but only one (MapView) is shown on the tree :S

+0

您在main.xml中包含的问题中显示的布局是什么? – elevine

+0

是的,这个相关布局,mapview和seekbar在main.xml中,并且它们已被保存。 – Genom

+0

请将空指针异常的堆栈跟踪添加到您的问题中。 – elevine

我只是将main.xml文件复制为map.xml并删除了main.xml。它的工作。我仍然不明白为什么发生。也许“主”会导致一些问题。也许MapActivities已经为自己定义了一个“主”布局,它正在中断我的“主要”布局。但这是解决方案。非常感谢您的耐心!

重要编辑:我已经明确地发现了这个问题。 main.xml文件保留在没有任何SeekBar的layout-normal和layout-normal-long文件夹中。我忘了编辑这些。

我不能添加其他浏览到mapactivity?

是的,你可以。

清理项目(项目 - >从Eclipse主菜单清除,或在命令行中清除ant clean)。然后使用Hierarchy View来查看正在发生的事情。这听起来好像你的ID是不合格的(在这种情况下clean会有帮助),或者你没有保存XML或其他东西。

+0

我已经清理了我的项目。但问题仍然存在。顺便说一下,层次结构视图不适合我。我得到了这些错误消息: [2011-08-20 01:47:23 - hierarchyviewer]无法从设备获取视图服务器版本10009c031579 [2011-08-20 01:47:23 - hierarchyviewer]无法获取视图服务器协议版本从设备10009c031579 [2011-08-20 01:47:26 - hierarchyviewer]无法调试设备10009c031579 [2011-08-20 01:48:06 - hierarchyviewer]无法从设备获取视图服务器版本10009c031579 顺便说一句,我从来没有用过它。 – Genom

+1

@Genom:您只能使用Hierarchy View和模拟器。 – CommonsWare

+0

我在我的问题中添加了层次结构树。 – Genom