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. HELP : Icon Aplikasi Ilang

SSB

*kabuuuuur

Nexus One !
Cuma bisa pake', gak bisa yang lain-lain !

-----Original Message-----
From: ayip s <ayip.mi...@gmail.com>
Sender: id-android@googlegroups.com
Date: Mon, 31 May 2010 18:29:24 
To: id-android@googlegroups.com<id-android@googlegroups.com>
Reply-To: id-android@googlegroups.com
Subject: [id-android] HELP : Icon Aplikasi Ilang

Dear Master/Suhu,

Barusan lepas mSD untuk copy file di PC, tapi saat pasang mSD kembali ke N1,
sebagian aplikasi yangg download di bl**kmarket hilang sedang aplikasi yang
hilang yg tampil discreen berubah logo jadi android logo, saat cek di
menu-setting-application, semua masih ada?
Gimana cara nampilin mereka kembali, thanks in adv

-- 
"Indonesian Android Community [id-android]" 

2. Modifying date and time pickers

I'd like to get started with Android development by scratching my
biggest itch: the date and time pickers.  I'd like to make them much
more efficient.  Before I dive in, I have some questions:

 * If I change those controls, will I be able to get the result onto my
   Droid without rooting it or waiting for a patch to be accepted
   upstream and pushed to my phone by my carrier?

 * Is such a patch likely to be accepted?

 * Why are those controls implemented as dialog boxes rather than
   intents?

-- 
Brian

-- 

3. Retriving a different icon when my app targets minSdkVersion 3 and 4

4. Wallpaper ROM dimana? WTA: Uninstall ADWLauncher rc1 di CM 5.0.7

5. Printing keyCode

6. Scrollable widgets in alternative Home applications

7. oot: pau...@telkom.ne