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. Problem Using Tamil font(Latha.ttf)

I need to use tamil on textviews in my app so I create a TypeFace from
assets and set it to the textview.
The characters appear out of order for combined characters. I load the
strings from the resources.(string.xml in utf-8 encoding)
How to solve this issue?

-- 

2. Image zooming functionality in Android just like the iphone

Hi All

I am working on an android app where i need to implement an image zooming
functionality(zoom will work by moving fingers apart and bringing them
together will zoom out the image).This is just like the way iphone does.

Could anyone please let me know is this possible?,Does Android provide any
inbuilt support in SDK for doing this?.Is there any kind of tutorial for
doing this?.

-- 
Thanks and Regards
Ajmer Singh

-- 

3. Desire nyangkut di booting up

4. TableRow not on top of each others

5. OOT - Aplikasi Paling Hebat

6. drawing 2D - vector based paths arcs etc

7. TextView.setError() strange behavior