Android Extending View and creating from XML Null pointer exception
I've created a custom v开发者_运维问答iew by extending View, it draws fine but When i try and refer to the object in the xml so I can say add a touch listener if reports a null pointer exception. My xml file is extremely simple
<com.projector.interaction.layout.MapMouseView2
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mouse"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</com.projector.interaction.layout.MapMouseView2>
I then try and referene the View by
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.testlayout);
MapMouseView2 view = (MapMouseView2)findViewById(R.id.mouse);
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("Debug","Touch Not Working");
return true;
}
});
} catch (Exception e) {
Log.e("Error","Error in TestActivity",e);
}
}
But it gives me a null pointer exception. Then only fix I can find is to wrap my view in the xml file inside a LinearLayout and reference the view by
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.testlayout);
LinearLayout lay = (LinearLayout)findViewById(R.id.lin);
MapMouseView2 mouseView = (MapMouseView2)lay.getChildAt(0);
mouseView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("Debug","Touch Not Working");
return true;
}
});
} catch (Exception e) {
Log.e("Error","Error in TestActivity",e);
}
}
Can anybody please tell me what I am doing wrong???
The View class is here
public class MapMouseView2 extends View{
WindowManager mWinMgr;
Rect zoom;
Rect panLeftRight;
Rect panUpDown;
public MapMouseView2(Context context,AttributeSet attrs){
super(context);
Log.d("Debug","Context,AttribyteSet");
try {
mWinMgr = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
zoom = new Rect(0, 0, 45, mWinMgr.getDefaultDisplay().getHeight());
panLeftRight = new Rect(0, 458, mWinMgr.getDefaultDisplay().getWidth(),
mWinMgr.getDefaultDisplay().getHeight());
panUpDown = new Rect(mWinMgr.getDefaultDisplay().getWidth() - 45, 0,
mWinMgr.getDefaultDisplay().getWidth(), mWinMgr
.getDefaultDisplay().getHeight());
Log.d("Debug","Leaving Constructor");
} catch (Exception e) {
Log.e("Error","Error in MapMouseView",e);
}
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
try {
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
// make the entire canvas white
paint.setColor(Color.BLACK);
canvas.drawPaint(paint);
paint.setColor(Color.BLUE);
canvas.drawRect(zoom, paint);
paint.setColor(Color.RED);
canvas.drawRect(panLeftRight, paint);
paint.setColor(Color.RED);
canvas.drawRect(panUpDown, paint);
//canvas.drawCircle(x,y,30,threadPaint);
} catch (Exception e) {
Log.e("Error","Exception in OnDraw",e);
}
}
}
I think you should call super(context, attrs);
in constructor of MapMouseView2
.
edit:
If you don't do that your view doesn't get id.
In the second approach you refer to the view by position not name, so it works.
精彩评论