访问构造函数时出现NullPointerException

时间:2014-09-23 03:03:01

标签: java android oop constructor nullpointerexception

我觉得要么我错过了OOP中的一个重要点,要么我的构造函数是错误的。我在创建对象时不断收到空指针异常。

logcat说它来自'//默认构造函数'的第一行。

以下是我班级的代码:

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); }
}

以下是使用它的地方:

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文件: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文件: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!

3 个答案:

答案 0 :(得分:2)

你不能直接调用getLayoutInflater(),你需要在LayoutInflater类的实例上调用它。 试试这个

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

你可以用&#34;这个替换上下文&#34;如果你打电话给这个inan活动

答案 1 :(得分:2)

将代码放在构造函数中进行活动是一种不好的做法。您可以放入OnCreate方法。

如果你真的需要这个实例。

您可以执行以下操作

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);
        .....
    }
}

答案 2 :(得分:0)

  1. 您无法在活动生命周期中调用活动方法或在Context之前将活动用作onCreate()。包含构造函数的实例初始化为时尚早。因此,您很少需要在活动中使用显式构造函数。

  2. 绝不使用new实例化活动类。与上述内容相关:此类对象并不适合任何事情,因为它们未正确初始化为活动,并且未调用其onCreate()等活动生命周期方法。

  3. 看起来您的MyHomework根本不应该是一项活动:

    • 删除`extends Activity``

    • Context作为参数传递给需要它的方法。例如,

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

      MyActivity onCreate()调用(非成员变量init,太早):

      homework = new MyHomework(this)