简体   繁体   中英

How do I declare “Member Fields” in Java?

This question probably reveals my total lack of knowledge in Java. But let me first show you what I thought was the correct way to declare a "member field":

public class NoteEdit extends Activity {

private Object mTitleText;
private Object mBodyText;

I'm following a google's notepad tutorial for android ( here ) and they simply said: "Note that mTitleText and mBodyText are member fields (you need to declare them at the top of the class definition)." I thought I got it and then realized that this little snippet of code wasn't working.

if (title != null) {
            mTitleText.setText(title);
        }
        if (body != null) {
            mBodyText.setText(body);
        }

So either I didn't set the "member fields" correctly which I thought all that was needed was to declare them private Objects at the top of the NoteEdit class or I'm missing something else. Thanks in advance for any help.

UPDATE

I was asked to show where these fields were being intialized here is another code snippet hope that it's helpful...

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.note_edit);
    Long mRowId;
    mTitleText = (EditText) findViewById(R.id.title);
    mBodyText = (EditText) findViewById(R.id.body);

So basically the error that is showing up is coming from eclipse: "The method setText(String) is undefined for the type Object"

When you declare fields and variables, it's usually helpful to give them a more specific static type than Object. Because you have declared mTitleText as an Object , the compiler only knows how to invoke methods on the general Object class definition . setText is not such a method, so it's not legal to call it without a cast or other trickery.

What you should do is figure out the type that your field should be. I don't know Android, but I presume that there is a text label class which defines your setText method. If you change your fields to be defined as that,

private EditText mTitleText;

you will find that things should work much better :-)

Unless you set those two fields somewhere else, they are not being initialized. So when you use them later, they are null and are causing exceptions. Java reference types initialize to null . Basic data types, which are not null able, initialize to 0.

Your mBodyText field needs to be typed to allow access to the setText method.

eg:

private BodyText mBodyText;

its not working because your objects not initialized. mTitleText text should be a TextView then you need to initialize it

mTitleText = findViewById( R.id.yourviewid );

and then do

mTitleText.setText(title);

They are marked as private so you can't the them outside of your class. If you want to get access outside of your class you have to use the keyword public in front of your field. BUT this is not recommended use properties instead

In this case you need to assign values to the two member variables. I believe the NoteEdit class has a layout xml file associated with it. You need to assign the text field objects from that layout to the objects before you try to reference their properties.

mTitleText = ( TextView ) findViewById( R.id.name_of_the_field_in_the_layout_file )

The answer above is also correct. You should assign types to those variables at the top of your class, not just make them Objects

Declaring the name and type of a reference is one thing; initializing it to point to a valid memory location on the heap is another. You need to initialize data members in a constructor for your class.

The default constructor initializes references to null by default. If you haven't written a constructor to initialize your data member references, that could be an explanation why you're having trouble.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM