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. Progress bar not visible with a white background in 1.6 sdk

when i ran my old application in 1.6 i noticed that the progress bar
was not showing up on the screen.When i analyzed it further i found
the issue.The progress bar is getting activated but its not visible
since my background color is also white.When i changed the background
color i was able to see the progress bar.Is there no other way to make
the progress bar visible other than changing the background
color..Shouldn't the progress bar be visible under all background
colors??
--~--~---------~--~----~------------~-------~--~----~

2. Getting media information that is being played

Is there a way that I can get the media information (song title,
genre, artist, etc) that is being played in the default music player?

Does the default music player allows sharing of these kind of
information?

Thanks.

--~--~---------~--~----~------------~-------~--~----~

3. multiline TextView in TableLayout

4. zipalign : could be done at distribution or installation time ?

5. ADC2: Low volume of judges

6. zipalign : could be done at distribution or installation time ?

7. Root your G