안드로이드 프로그래밍 질문입니다.

익명 사용자의 이미지

baseAdapter를 이용해 adapter를 확장하여 Arraylist로 목록 보기를 만드려고 하는데, 오류가 납니다ㅠㅠ

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object referenced
위와 같은 오류가 나고,

코드는 아래와 같습니다.

public class MainActivity extends AppCompatActivity { private ArrayList myDataList; private MyAdapter myAdapter; private ListView listView;

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

myDataList.add(new MyData(1, "홍길동", "01062548978"));
myDataList.add(new MyData(2, "성", "010458945"));
myDataList.add(new MyData(3, "경", "010789654"));

myAdapter = new MyAdapter(this, R.layout.custom_adapter_view, myDataList);
listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(myAdapter);
}
}

public class MyData { private int _id; private String name; private String phone;

public MyData(int _id, String name, String phone){
this._id = _id;
this.name = name;
this.phone = phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public void setName(String name) {
this.name = name;
}

public void set_id(int _id) {
this._id = _id;
}

public String getPhone() {
return phone;
}

public String getName() {
return name;
}

public int get_id() {
return _id;
}
}

public class MyAdapter extends BaseAdapter{ private Context context; private int layout; private ArrayList myDataList; private LayoutInflater layoutInflater;

public MyAdapter(Context context, int layout, ArrayList myDataList){
this.context = context;
this.layout = layout;
this.myDataList = myDataList;
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int pos, View view, ViewGroup viewGroup) {
final int position = pos;

if (view == null){
view = layoutInflater.inflate(layout, viewGroup, false);
}
TextView textNo = (TextView)view.findViewById(R.id.textNo);
TextView textName = (TextView)view.findViewById(R.id.textName);
TextView textPhone = (TextView)view.findViewById(R.id.textPhone);
Button btn_result = (Button)view.findViewById(R.id.btn_result);

textNo.setText(Integer.valueOf(myDataList.get(position).get_id()).toString());
textName.setText(myDataList.get(position).getName());
textPhone.setText(myDataList.get(position).getPhone());

btn_result.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view){
Toast.makeText(context, myDataList.get(position).getPhone() + "선택", Toast.LENGTH_SHORT).show();
}
});

return view;
}

@Override
public long getItemId(int pos) {
return myDataList.get(pos).get_id();
}

@Override
public Object getItem(int pos) {
return myDataList.get(pos);
}

@Override
public int getCount() {
return myDataList.size();
}
}

익명 사용자의 이미지

해결했습니다~

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.