有没有办法在每次用户点击某个按钮时创建一个对象?
我制作了一个对象数组,作为创建对象的容器 但程序崩溃!! 有工作方式吗?
package com.fawzyx.movie_rental_store;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.TextView;
import java.lang.String;
public class MemReg_activity extends Activity{
public member[] member ;
String name;
String id ;
String mail;
String ccard ;
String address ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reg_layout);
final EditText etname = (EditText)findViewById(R.id.etname);
EditText etmail = (EditText)findViewById(R.id.etmail);
EditText etid = (EditText)findViewById(R.id.etid);
EditText etccard=(EditText)findViewById(R.id.etccard);
EditText etaddress = (EditText)findViewById(R.id.etadress);
name = etname.getText().toString();
id = etid.getText().toString();
mail = etmail.getText().toString();
ccard = etccard.getText().toString();
address = etaddress.getText().toString();
Button btSubmit= (Button)findViewById(R.id.btsubmit);
btSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (etname.getText().toString().length()<5)
{
Toast.makeText(MemReg_activity.this, "Enter a valid name", Toast.LENGTH_LONG).show();
}
else
{
int x=0;
x++ ;
member[x] = new member(name, id, mail , ccard , address);
}
}
});
}
}
答案 0 :(得分:0)
成员未在任何地方初始化。
//initialize
member = new member[15]; // 15 is just an example.
另一方面,如果尺寸不固定,您应该考虑使用List
。
List<member> members = new ArrayList<member>();
并在onclick方法中,
members.add(new member());
要访问这些项目,
member m = members.get(0); // first item in the list
迭代所有项目,
for(member m : members){
// m is your member object
}