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 6874063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:08:53+00:00 2026-05-27T04:08:53+00:00

I have those code: public class ContentEditText extends EditText { /* * Constructors */

  • 0

I have those code:

public class ContentEditText extends EditText {
/*
 * Constructors
 */
public ContentEditText(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

public ContentEditText(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ContentEditText(Context context)
{
    super(context);
}

/*
 * Listener
 */
@Override   
protected void onSelectionChanged(int selStart, int selEnd)
{ 
    Toast.makeText(getContext(), "selStart is " + selStart + "selEnd is " + selEnd, Toast.LENGTH_LONG).show();
}   
}

and

public class Main extends Activity {
private LinearLayout mainLayout;
private Button bBT;
private Button uBT;
private Button iBT;
private Button lBT;
private EditText titleET;
private ContentEditText contentET;
private Markup markup;

/** onCreate Function */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initLayout();     
    setContentView(mainLayout);           
}

private void initLayout()
{
    // initialize some components by XML layout
    mainLayout = (LinearLayout) findViewById(R.id.main_layout);
    bBT = (Button) findViewById(R.id.boldBT);
    uBT = (Button) findViewById(R.id.underlineBT);
    iBT = (Button) findViewById(R.id.italicBT);
    lBT = (Button) findViewById(R.id.listBT);
    titleET = (EditText) findViewById(R.id.titleET);
    // initialize a EditText programmatically
    contentET = new ContentEditText(this);
    contentET.setGravity(Gravity.TOP);
    contentET.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    // add the EditText to the main layout
    mainLayout.addView(contentET);
}

I thought I add contentET to the main layout by the right way, but it doesn’t work, the LogCat said “NullPointerException” for variable contentET, I don’t know why. Anyone can tell me where did i do wrong?
Thanks!

  • 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-27T04:08:53+00:00Added an answer on May 27, 2026 at 4:08 am

    Chang your code to this as the first call should be setContentView after the super. Since this is not the case in your above code you are trying to access the layout without inflating it there fore it throwing nullpointer exception

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_xml_file);           
        initLayout();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this code for these Constructors of the Weapon class: Weapon(const WeaponsDB
I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
I have a class : Schedule. public class Schedule { private int locationNum; private
I have this code: public partial class App : Application { protected override void
I have a custom control defined in code-behind: Public Class MyControl Inherits Button Private
I have those two classes: class DownloadLink { public string Name { get; private
I have some code: public class Foo { private HttpClient httpClient; public Foo() {
I have my OrganizationRequestContext interface, which works great: @Service(OrganizationDAO.class) public interface OrganizationRequestContext extends RequestContext
I have those classes: public class Flight { public string FlightNum { get; set;
I have a code base, in which for Matrix class, these two definitions are

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.