layout을 상속받아서 커스텀 뷰를 만들려고 합니다.


그런데 context의 값이 null로 나와서 실행이 되지 않습니다.


context는 실행이 될때 자동으로 생성된다고 하는데 계속 null값을 찍어 어플이 돌아가지 않습니다.


아시는분 답변 좀 해주세요. 아래 소스는 예제 소스 보고 한건데 이러네요...


안드로이드 버전은 2.3.3에서 테스트 하고 있습니다.

 

XMl파일도 같이 올려봅니다.

 

 package com.android_gui;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;
 
public class customeView extends LinearLayout
{
 Context mContext = null;
 TextView mTitle = null;
 Button mButton = null;
 GridView mGridView = null;
 LayoutInflater li;
 
 public customeView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  //context = getContext();
  Log.i("DEBUG", "Context1["+mContext+"]");
  
  initcustomeView(mContext);
 }
 public customeView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  Log.i("DEBUG", "Context2["+mContext+"]");
  initcustomeView(mContext);
 }
 private void initcustomeView(Context context) {
  // TODO Auto-generated method stub
  mContext = context;
  Log.i("DEBUG", "Context6["+mContext+"]");
  
  String infService = Context.LAYOUT_INFLATER_SERVICE;
  li = (LayoutInflater)getContext().getSystemService(infService);
  //View v = li.inflate(R.layout.coustom_view, this, false);
  View v = li.inflate(R.layout.coustom_view, null);
  addView(v);
  
  mTitle = (TextView)findViewById(R.id.guiTitle);
  mButton = (Button)findViewById(R.id.guiButton);
  mGridView = (GridView)findViewById(R.id.griditem);
  
  Log.i("DEBUG", "Context3["+mContext+"]");
  setAdapter(new ImageAdapter(mContext));
  
  setFocusable(true);
  setFocusableInTouchMode(true);
  
  setBackgroundColor(Color.BLUE);
  
  mButton.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if(mButton.getText().toString().trim().equals(""))
    {
     mButton.setText("+");
     mGridView.setVisibility(View.GONE);
    }
    else
    {
     mButton.setText("-");
     mGridView.setVisibility(View.VISIBLE);
    }
   }
  });
 }
 
 public void setAdapter(ListAdapter adapter)
 {
  mGridView.setAdapter(adapter);
 }
 
 public class ImageAdapter extends BaseAdapter
 {
  
  public ImageAdapter(Context c)
  {
   mContext = c;
   Log.i("DEBUG", "Context4["+c+"]");
  }
  
  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return mThumb.length;
  }
  @Override
  public Object getItem(int position) {
   // TODO Auto-generated method stub
   return position;
  }
  @Override
  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub
   ImageView iv;
   if(convertView == null)
   {
    iv = new ImageView(mContext);
    iv.setLayoutParams(new LayoutParams(45, 45));
    iv.setAdjustViewBounds(false);
    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
    iv.setPadding(8, 8, 8, 8);
    iv.setBackgroundColor(Color.BLUE);
   }
   else {
    iv = (ImageView)convertView;
   }
   
   iv.setImageResource(mThumb[position]);
   
   return iv;
  }
  
  private Context mContext;
  private Integer[] mThumb = {
    R.drawable.t1_01, R.drawable.t202,
    R.drawable.t303, R.drawable.t404,
    R.drawable.t505, R.drawable.t606,
    R.drawable.t707, R.drawable.t808,
    R.drawable.t1_01, R.drawable.t202,
    R.drawable.t303, R.drawable.t404,
    R.drawable.t505, R.drawable.t606,
    R.drawable.t707, R.drawable.t808,
    R.drawable.t1_01, R.drawable.t202,
    R.drawable.t303, R.drawable.t404,
    R.drawable.t505, R.drawable.t606,
    R.drawable.t707, R.drawable.t808,
    R.drawable.t1_01, R.drawable.t202,
    R.drawable.t303, R.drawable.t404,
    R.drawable.t505, R.drawable.t606,
    R.drawable.t707, R.drawable.t808,
    R.drawable.t1_01, R.drawable.t202,
    R.drawable.t303, R.drawable.t404,
    R.drawable.t505, R.drawable.t606,
    R.drawable.t707, R.drawable.t808,
    R.drawable.t1_01, R.drawable.t202,
    R.drawable.t303, R.drawable.t404,
    R.drawable.t505, R.drawable.t606,
    R.drawable.t707, R.drawable.t808,
  };  
 }
}

 <?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="wrap_content"
    >
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:id="@+id/guiTitle" 
    android:text="@string/hello"
    />
    <Button
    android:id="@+id/guiButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="====Title===="/>
    </LinearLayout>
<GridView android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:id="@+id/griditem"
android:verticalSpacing="10dp"
android:padding="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:scrollbars="none"
android:gravity="center"/>
</LinearLayout>