Why Return null when using findViewById in Activity to find and handle CustomView?

by krekar » Fri, 04 Dec 2009 17:41:19 GMT


Sponsored Links
 hi, there...

I wrote CustomView Class, and load it in layout.

so i trid to find CustomView using findViewById()  in activiy to get
handle.

but findViewById() was return null.

I need your help .

Written Code is like below:

---------------------------------------------------------------------------------------------------------------------

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/ 
android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView
    android:layout_width="fill_parent"
    android:layout_height="10dp"
    android:text="@string/hello" />
 <com.test.MapCanvas
  android:id="@+id/map_canvas"
  android:layout_width="fill_parent"
  android:layout_height="100dp"
     android:focusable="true"
  android:clickable="true" />
</LinearLayout>

---------------------------------------------------------------------------------------------------------------------

MapCanvas.java:

package com.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

public class MapCanvas extends View {
     // ...
    public MapCanvas(Context context) {
     super(context);
     initialize();
    }

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

    private void initialize() {
        // ...
      }


 @Override
    protected void onDraw(Canvas canvas) {
        // ...
    }
}

---------------------------------------------------------------------------------------------------------------------

Test.java (Activity)

package com.test;

import android.app.Activity;
import android.os.Bundle;
import com.test.MapCanvas;

public class Test extends Activity {
    /** Called when the activity is first created. */
 private MapCanvas mapCanvas;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapCanvas = (MapCanvas)this.findViewById(R.id.map_canvas); //
*** null return.
    }
}

---------------------------------------------------------------------------------------------------------------------

--



Why Return null when using findViewById in Activity to find and handle CustomView?

by dom » Wed, 16 Dec 2009 14:21:47 GMT


 you should use
    public MapCanvas(Context context, AttributeSet attrs) {
     super(context, attrs);// id is in the attrs, if you didn't pass
the id to the view class, the class have no id.
     initialize();
    }




--


Sponsored Links


Other Threads

1. Making submenus look like the expanded menu

Is this supported by the framework? I find the expanded menu look&feel
much more usable.

Thanks.
--~--~---------~--~----~------------~-------~--~----~

2. android-platform is now partially moderated

We have recently turned on moderation control for the android-kernel
mailing list.  The goal is only to reduce the amount of spam on the
list, there is no plan for any active moderation or membership
control.

New memberships will still be granted automatically, there is no
intention to limit group membership, and new members will be able to
receive and read messages immediately.

For new members, your first post will be moderated.  Please allow 24
hours for your first post to go through, and please don't keep
reposting.  Once you have posted successfully, you will be allowed to
post freely.

I hope this improves things for all members of android-kernel.

--
Cdric
Google

--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-kernel+unsubscr...@googlegroups.com
website: 

3. How to execute a application automatically after android booting?

4. Problem when changing the default colors and drawables of Android

5. G1

6. Hessian Web Service and Android App

7. Cupcake's bug ? Caused by: android.content.res.Resources