ArrayList未初始化

时间:2015-07-09 16:46:11

标签: java android arraylist initialization

我试图将一个数组列表从一个类导入另一个类,但Android Studio表示它未初始化。我有关于存储在CourseNameClass中的数组列表的信息,以便可以在两个匿名类之间共享信息。我让应用程序在单击一个按钮时获取信息,并在单击另一个按钮时切换到另一个活动。我该如何解决这个问题?这是我的代码:

public class EnterCourseActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enter_course);

    buttonClick();
}

public void buttonClick() {
    final CourseNameClass courseNameShared = new CourseNameClass(" ");

    Button enterAnotherButton = (Button) findViewById(R.id.enterAnotherButton);
    enterAnotherButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            EditText inputText = (EditText)findViewById(R.id.inputCourseName);
            String courseNameString = inputText.getText().toString();
            courseNameShared.courseNameInClass = courseNameString;
            inputText.setText("");
        }
    });

    Button doneButton = (Button) findViewById(R.id.doneButton);
    doneButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            CourseNameClass coursesArrayListInClass;
            Intent i = new Intent(EnterCourseActivity.this, MainActivity.class);
            i.putStringArrayListExtra("course", coursesArrayListInClass);

            startActivity(i);
        }
    });
}

class CourseNameClass{
    String courseNameInClass;

    public CourseNameClass(String courseNameInClass){
        this.courseNameInClass = courseNameInClass;
        ArrayList<String> coursesArrayListInClass = new ArrayList<String>();
        coursesArrayListInClass.add(courseNameInClass);
    }
}

2 个答案:

答案 0 :(得分:0)

public void buttonClick() {
    final CourseNameClass courseNameShared = new CourseNameClass(" ");

    Button enterAnotherButton = (Button) findViewById(R.id.enterAnotherButton);
    enterAnotherButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            EditText inputText = (EditText)findViewById(R.id.inputCourseName);
            String courseNameString = inputText.getText().toString();
            courseNameShared.courseNameInClass = courseNameString;
            inputText.setText("");
        }
    });

    Button doneButton = (Button) findViewById(R.id.doneButton);
    doneButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Intent i = new Intent(EnterCourseActivity.this, MainActivity.class);
        i.putStringArrayListExtra("course", courseNameShared.getCoursesArrayList());

        startActivity(i);
    }
}


class CourseNameClass {
    String courseNameInClass;

    public CourseNameClass(String courseNameInClass){
        this.courseNameInClass = courseNameInClass;
    }

    public ArrayList<String> getCoursesArrayList() {
        ArrayList<String> coursesArrayListInClass = new ArrayList<String>();
        coursesArrayListInClass.add(courseNameInClass);
        return coursesArrayListInClass;
    }
}

答案 1 :(得分:0)

您在ArrayList<String> coursesArrayListInClass构造函数中创建CourseNameClass,然后再也不使用它。您在CourseNameClass coursesArrayListInClass;方法中创建的OnClick()是新CourseNameClass对象,这是您未初始化的对象。但它不是ArrayList对象!您为不同的对象使用了相同的名称!

要使用ArrayList<String> coursesArrayListInClass,您可以在CourseNameClass中创建新方法:

class CourseNameClass{
    String courseNameInClass;
    ArrayList<String> coursesArrayListInClass;

    public CourseNameClass(String courseNameInClass){
        this.courseNameInClass = courseNameInClass;
        coursesArrayListInClass = new ArrayList<String>();
        coursesArrayListInClass.add(courseNameInClass);
    }

    public ArrayList<String> getArrayList(){
        return coursesArrayListInClass;
    }
}