我正在尝试将数组列表从一个类导入另一个类,但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);
    }
}

最佳答案

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

关于java - ArrayList未初始化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31323623/

10-11 22:16