带有 MapView 的relativelayout中的视图为空
我有一个带有地图视图的 MapActivity。效果很好。现在我想将 SeekBar 添加到我的 MapView 上。它必须位于底部但位于地图视图上。用户将滚动以设置距离。任何。我的问题是,当我通过 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.
我到处都进行了研究,但找不到类似的问题。有人使用了非常相似的东西。所以我忽略了它是null,我没有在onCreate()中调用SeekBar。但SeekBar根本没有被绘制。我不能向地图活动添加其他视图吗?
感谢您的任何想法!
重要编辑:图形布局查看器显示 SeekBar,因为它应该位于地图视图的底部。
我仍然得到 NullPointerException: 错误/AndroidRuntime(12738):致命异常:主
错误/AndroidRuntime(12738):java.lang.RuntimeException:无法启动活动ComponentInfo {com.MyApps.MapLocations / com.MyApps.MapLocations.MyActivity}:java.lang.NullPointerException
错误/Android运行时(12738):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
错误/AndroidRuntime(12738):在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
错误/AndroidRuntime(12738):在 android.app.ActivityThread。访问 $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(本机方法)
错误/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)
ERROR/AndroidRuntime(12738): ... 11 更多
这是 HierarchyViewer 的结果:
I have a MapActivity with a mapview. It works fine. Now I want to add a SeekBar onto my MapView. It has to be at the bottom but on the mapview. User will scroll to set the distance. Whatever. My problem is, that any view I am trying to put there is null, when I reach them by 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>
I am trying to reach that like this:
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.
I have made a research everywhere and couldn't find a similar problem. Someone used something very similar. So I ignored that it is null, I didn't call the SeekBar in onCreate(). But the SeekBar hasn't been drawen at all. Can't I add other Views to a mapactivity?
Thanks for any idea!
An IMPORTANT EDIT: The graphical layout viewer shows the SeekBar as it should at the bottom of mapview.
I still get NullPointerException:
ERROR/AndroidRuntime(12738): FATAL EXCEPTION: main
ERROR/AndroidRuntime(12738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyApps.MapLocations/com.MyApps.MapLocations.MyActivity}: java.lang.NullPointerException
ERROR/AndroidRuntime(12738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
ERROR/AndroidRuntime(12738): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
ERROR/AndroidRuntime(12738): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
ERROR/AndroidRuntime(12738): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
ERROR/AndroidRuntime(12738): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(12738): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(12738): at android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(12738): at java.lang.reflect.Method.invokeNative(Native Method)
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): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
ERROR/AndroidRuntime(12738): at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(12738): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(12738): at com.MyApps.MapLocations.MyActivity.setupSeekbar(MyActivity.java:343)
ERROR/AndroidRuntime(12738): at com.MyApps.MapLocations.MyActivity.onCreate(MyActivity.java:89)
ERROR/AndroidRuntime(12738): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR/AndroidRuntime(12738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
ERROR/AndroidRuntime(12738): ... 11 more
Here is the result from HierarchyViewer:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以。
清理您的项目(从 Eclipse 主菜单中的“项目”->“清理”或在命令行中的
ant clean
)。然后使用 Hierarchy View 来查看发生了什么。听起来要么是您的 ID 不正常(在这种情况下,清理会有所帮助),要么是您未能保存 XML 或其他内容。Yes, you can.
Clean your project (Project -> Clean from the Eclipse main menu or
ant clean
at the command line). Then use Hierarchy View to see what is going on. It sounds like either your IDs are off kilter (in which case the clean will help) or perhaps you failed to save the XML or something.我刚刚将 main.xml 文件复制为 map.xml 并删除了 main.xml。它奏效了。我仍然不明白为什么会这样。也许“主要”会引起一些问题。也许 MapActivities 已经为自己定义了一个“主”布局,它中断了我的“主”布局。但这是某种解决方案。非常感谢您的耐心等待!
重要编辑:我确实发现了问题。 main.xml 文件保留在layout-normal 和layout-normal-long 文件夹中,没有任何SeekBar。我忘了编辑那些。
I just have copied the main.xml file as map.xml and deleted main.xml. And it worked. I still don't understand why it happened. Maybe the "main" causes some problem. Maybe MapActivities have defined for themselves a "main" layout which is interrupting my "main" layout. But this was the solution somehow. Thanks a lot for your patience!
IMPORTANT EDIT: I have definitely found the problem. main.xml file remained in layout-normal and layout-normal-long folders without any SeekBar. I forgot to edit those.