1

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: Here you can seen that there are 2 Views in the layout but only one (MapView) is shown on the tree :S

Genom
  • 207
  • 1
  • 12

2 Answers2

0

Can't I add other Views to a mapactivity?

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have cleaned my project. But the problem still occurs. By the way the hierarchy view didn't work by me. I got these error messages: [2011-08-20 01:47:23 - hierarchyviewer]Unable to get view server version from device 10009c031579 [2011-08-20 01:47:23 - hierarchyviewer]Unable to get view server protocol version from device 10009c031579 [2011-08-20 01:47:26 - hierarchyviewer]Unable to debug device 10009c031579 [2011-08-20 01:48:06 - hierarchyviewer]Unable to get view server version from device 10009c031579 By the way I have never used it before. – Genom Aug 19 '11 at 23:51
  • 1
    @Genom: You can only use Hierarchy View with an emulator. – CommonsWare Aug 20 '11 at 01:01
  • I added the hierarchy tree in my question. – Genom Aug 20 '11 at 08:30
  • @Genom: Android is not inflating the layout where you have the `SeekBar`, then. Perhaps you saved that XML to a different file. – CommonsWare Aug 20 '11 at 10:03
  • Sorry but I don't get it. They both are in the main.xml which I set as the content view with: setContentView(R.layout.main); SeekBar is right under the mapview. Hierarchy View shows that there is a second view. I also have tried to use text view and that also was null. – Genom Aug 20 '11 at 10:44
  • 1
    @Genom: Scroll to the top of this question. Look at your layout. It is a `RelativeLayout` holding onto a `MapView` and a `SeekBar`. Now look at the screenshot from Hierarchy View in this question. It shows that the parent of the `MapView` is a `LinearLayout`. Hence, your XML layout in this question and the hierarchy shown by Hierarchy View in this question **are not from the same resource**. – CommonsWare Aug 20 '11 at 11:13
  • I have only one mapView in my project and I have never used a FrameLayout. I think those FrameLayout and LinearLayout have been created by the RelativeLayout. I think what RelativeLayout does is to calculate correct LinearLayouts (and maybe a FrameLayout as Parent). They cannot be from different sources. And as you see there are 2 Views in the LinearLayout. One of them has been shown as child element in the hierarchy view tree: mapView. I suppose the second is the SeekBar, which hasn't been shown. – Genom Aug 20 '11 at 11:31
  • @Genom: "I have never used a FrameLayout" -- that is the parent of your content view (basically, what the parent is of `setContentView()`). " I think those FrameLayout and LinearLayout have been created by the RelativeLayout." -- no. "I think what RelativeLayout does is to calculate correct LinearLayouts" -- no. "They cannot be from different sources" -- yes, they can. – CommonsWare Aug 20 '11 at 11:54
0

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.

Genom
  • 207
  • 1
  • 12