应用程序崩溃"单击按钮"

时间:2015-11-17 09:42:11

标签: android listview android-listview

我是android的新手,帮助代码如何使用BaseAdapter通过单击Button将EditText值添加到ListView。当我在EditText中输入值并单击添加按钮时,值必须放在ListViw中(ListView必须只有BaseAdapter)。这里尝试找到错误的代码,帮我完成我的代码

public class AddlistActivity extends Activity {
ListView lstName;
Button btnAdd;
EditText edtName;
String myData;
Context context=AddlistActivity.this;
ArrayList<ListDataActivity>list=new ArrayList<ListDataActivity>();

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

    lstName=(ListView)findViewById(R.id.lst_name);
    btnAdd=(Button)findViewById(R.id.btn_add);
    edtName=(EditText)findViewById(R.id.edt_name);
    getDataInList();

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myData=edtName.getText().toString().trim();
            getDataInList();
            ((BaseAdapter)lstName.getAdapter()).notifyDataSetChanged();
        }
    });

    lstName.setAdapter(new MyBaseAdapterActivity(context,list));
}
private void getDataInList()
{
    for (int i = 0 ; i < myData.length();i++)
    {
        ListDataActivity listDataActivity = new ListDataActivity();
        listDataActivity.setMyData(myData);
        list.add(listDataActivity);
    }
}}

ListDataActivity.java

public class ListDataActivity {
String myData;

public String getMyData()
{
    return myData;
}
public  void setMyData(String myDataa)
{
    myData=myDataa;
}}

MyBaseAdapterActivity.java

public class MyBaseAdapterActivity extends BaseAdapter {

ArrayList<ListDataActivity> list =new ArrayList<ListDataActivity>();
LayoutInflater inflater;
Context context;

public MyBaseAdapterActivity(Context context,ArrayList<ListDataActivity>list)
{
    this.list=list;
    this.context=context;
    inflater = LayoutInflater.from(this.context);
}

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

@Override
public ListDataActivity getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    MyViewHolder myViewHolder;
    HashMap<String , Long >hashMap= new HashMap<String , Long>();

    if (convertView==null)
    {
        convertView=inflater.inflate(R.layout.activity_addlist,parent,false);
        myViewHolder=new MyViewHolder(convertView);
        convertView.setTag(myViewHolder);
    }else {
        myViewHolder =(MyViewHolder)convertView.getTag();
    }

    myViewHolder.editText=detials(convertView,R.id.edt_name,list.get(position).getMyData());

    return convertView;
}

private EditText detials(View v , int resId , String text)
{
    EditText et =(EditText)v.findViewById(resId);
    et.setText(text);
    return et;
}

private static class MyViewHolder{
   EditText editText;

    public MyViewHolder(View item)
    {
    }
}}

logcat的

11-17 15:26:19.885    2787-2787/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.rahul.listviewwithbaseadapter, PID: 2787
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rahul.listviewwithbaseadapter/com.example.rahul.listviewwithbaseadapter.AddlistActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
        at com.example.rahul.listviewwithbaseadapter.AddlistActivity.getDataInList(AddlistActivity.java:48)
        at com.example.rahul.listviewwithbaseadapter.AddlistActivity.onCreate(AddlistActivity.java:33)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

2 个答案:

答案 0 :(得分:1)

您的代码中存在一些逻辑问题。基于我的假设,您正在尝试创建一个动态列表,该列表基于按钮单击事件上的EditText中的单词而增长。以下是代码:

在布局文件夹中调用 list_cells.xml

创建XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp">

<TextView
        android:id="@+id/list_cell_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />

</RelativeLayout>

<强> AddlistActivity.java

public class AddlistActivity extends Activity {

    ListView lstName;
    Button btnAdd;
    EditText edtName;
    String myData;
    Context context = AddlistActivity.this;
    ArrayList<ListDataActivity>list = new ArrayList<ListDataActivity>();
    MyBaseAdapterActivity baseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addlist);

        lstName=(ListView)findViewById(R.id.lst_name);
        btnAdd=(Button)findViewById(R.id.btn_add);
        edtName=(EditText)findViewById(R.id.edt_name);
        baseAdapter = new MyBaseAdapterActivity(AddlistActivity.this, list);
        lstName.setAdapter(baseAdapter);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myData = edtName.getText().toString().trim();
                setDataIntoList(myData);
                baseAdapter.updateMyList(list);
            }
        });
    }

    private void setDataIntoList(String data)
    {
     ListDataActivity tempListDataActivity = new ListDataActivity();
     tempListDataActivity.setMyData(data);
     list.add(tempListDataActivity);
    }
}

<强> ListDataActivity.java

public class ListDataActivity {

    String myData;

    public String getMyData()
    {
        return myData;
    }

    public  void setMyData(String myDataa)
    {
        myData = myDataa;
    }
}

<强> MyBaseAdapterActivity.java

public class MyBaseAdapterActivity extends BaseAdapter {

    static class MyViewHolder {
        EditText editText;
    }

    private ArrayList<ListDataActivity> list;
    private MyViewHolder myViewHolder;


    public MyBaseAdapterActivity(Context context,ArrayList<ListDataActivity>list)
    {
        this.list = list;
        this.context = context;
    }

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

    @Override
    public ListDataActivity getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void updateMyList(ArrayList<ListDataActivity> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_cells, parent,false);

            myViewHolder = new MyViewHolder();
            myViewHolder.textView = (TextView) convertView.findViewById(R.id.list_cell_textview);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder =(MyViewHolder)convertView.getTag();
        }

        myViewHolder.textView.setText(list.get(position).getMyData());

        return convertView;
    }
}

以上代码应该有效。如果有错误,请再次回复此处,我会尽力为您提供帮助。

答案 1 :(得分:1)

  

引起:java.lang。 NullPointerException :尝试调用虚拟   方法&#39; int java.lang.String.length()&#39;在null对象引用上           在com.example.rahul.listviewwithbaseadapter.AddlistActivity.getDataInList(AddlistActivity.java:48)

当您尝试在myData.length()方法中调用getDataInList()时,您有 NullPointerException ,因为myDatanull

您忘记在myData的第一次通话时初始化getDataInList()