0

I feel like either I'm missing a vital point in OOP, or my constructor is wrong. I keep getting a null pointer exception when creating my object.

The logcat is saying that its coming from the first line of the '// default constructor'.

Here is the code for my class:

package com.chriswahlfeldt.homeworkapp;

        import android.app.Activity;
        import android.view.View;
        import android.widget.EditText;

public class MyHomework extends Activity {

    private EditText title, description;
    private View homeworkView, activityView;

    // default constructor
    public MyHomework() {

        homeworkView = getLayoutInflater().inflate(R.layout.add_homework, null);
        activityView = getLayoutInflater().inflate(R.layout.activity_my, null);

        title = (EditText) homeworkView.findViewById(R.id.classTitleET);
        title.setText("");

        description = (EditText) homeworkView.findViewById(R.id.descriptionET);
        title.setText("");

    }

    public View getContentView_activity_my() { return activityView; }

    public View getContentView_add_homework() { return homeworkView; }

    public String getTitleTxt() { return title.getText().toString(); }

    public String getDescriptionTxt() { return description.getText().toString(); }

    public void setTitleTxt(String thatString) { title.setText(thatString); }

    public void setDescriptionTxt(String thatString) { title.setText(thatString); }
}

And here is where it is used:

package com.chriswahlfeldt.homeworkapp;

        import android.app.Activity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;

public class MyActivity extends Activity
{

    private MyHomework hW = new MyHomework();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(hW.getContentView_activity_my());

        final Button addHWBtn = (Button) findViewById(R.id.HWBtn);

        addHWBtn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        setContentView(hW.getContentView_add_homework());
                    }
                });
    }
}

.xml file: activity_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity"
android:id="@+id/mainRelLayout" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/HWBtn"
        android:textSize="25sp"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="+Homework"/>

</RelativeLayout> 

.xml file: add_homework.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:alpha=".8">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/classTitleET"
        android:layout_gravity="top"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:capitalize="sentences"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="10dp"
        android:hint="Class Title"
        android:textSize="20sp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="8"
        android:id="@+id/descriptionET"
        android:layout_below="@+id/classTitleET"
        android:layout_alignLeft="@+id/classTitleET"
        android:layout_alignStart="@+id/classTitleET"
        android:layout_alignRight="@+id/classTitleET"
        android:layout_alignEnd="@+id/classTitleET"
        android:hint="Homework Description"
        android:textSize="20sp"
        android:height="120dp"
        android:gravity="top" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/createBtn"
        android:hint="Create"
        android:textSize="20sp"
        android:layout_below="@+id/descriptionET"
        android:layout_alignLeft="@+id/descriptionET"
        android:layout_alignStart="@+id/descriptionET"
        android:layout_alignRight="@+id/descriptionET"
        android:layout_alignEnd="@+id/descriptionET" />

</RelativeLayout>

Thx!

cwahlfeldt
  • 580
  • 5
  • 14
  • 1
    this would be a good [reference](http://stackoverflow.com/questions/3302177/android-activity-constructor-vs-oncreate) – Ker p pag Sep 23 '14 at 03:10
  • Also, [don't pass null to `inflate()`!](http://www.doubleencore.com/2013/05/layout-inflation-as-intended/) – Kevin Coppock Sep 23 '14 at 04:58

3 Answers3

2

yout can't call getLayoutInflater() directly , you need to call it on an instance of the LayoutInflater class. try this

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
homeworkView = inflater.inflate(R.layout.add_homework, null);

you can replace context with "this" if you are calling this inan activity

nPn
  • 16,254
  • 9
  • 35
  • 58
  • This worked but I had to pass a Context to my constructor when creating the object in the on create. Thx for pushing me in the right direction! – cwahlfeldt Sep 23 '14 at 03:48
  • @waffles its probably better that you read the [reference material on Activities](http://developer.android.com/guide/components/activities.html) before continuing - Android doesn't like it when you use the main constructor and prefers that you use onCreate() etc – panini Sep 23 '14 at 03:51
2

It's a bad practice to put the code inside a constructor for activities. You can put in the OnCreate method.

If you need this instance really.

You can do the following

public class MyHomework extends Activity {

    private EditText title, description;
    private View homeworkView, activityView;
    private static MyHomework instance;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         instance = this;
    }
    public static MyHomework getActivity() {
         return instance;
    }
}

 

public class MyActivity extends Activity
{

    private MyHomework hW = MyHomework.getActivity();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        .....
    }
}
Nissa
  • 4,636
  • 8
  • 29
  • 37
Nalam
  • 21
  • 1
0
  1. You cannot call activity methods or use the activity as a Context before onCreate() in the activity lifecycle. Instance initialization including constructor is too early. As a consequence, you rarely need to use an explicit constructor with activities.

  2. Never instantiate activity classes with new. Related to the point above: such objects are not really good for anything since they are not properly initialized as activities and their activity lifecycle methods such as onCreate() are not invoked.

It looks like your MyHomework should not be an activity at all:

  • Remove the `extends Activity``

  • Pass in a Context as an argument to methods that need it. For example,

    public MyHomework(Context context) {
        homeworkView = LayoutInflater.from(context).inflate(...)
    

    Invocation from MyActivity onCreate() (not member variable init, too early):

    homework = new MyHomework(this)
    
laalto
  • 150,114
  • 66
  • 286
  • 303