Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 881173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:13:05+00:00 2026-05-15T12:13:05+00:00

I’ve created a custom view by extending View, it draws fine but When i

  • 0

I’ve created a custom view 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);
    }


}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:13:07+00:00Added an answer on May 15, 2026 at 12:13 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.