没有NullPointerException的解决方案

时间:2014-05-03 14:47:10

标签: java android listview nullpointerexception

我想为ListView创建一个BaseAdapter。从我的数据库中,我得到一个包含ListView条目的ArrayList。 onCreate方法工作正常但如果我单击按钮将数据插入数据库并在ListView中显示它我的应用程序崩溃。我正在寻找解决方案,但无法找到它。也许你这样做 Logcat表示在设置TextView文本的位置NullPointerExceptionLaunchActivity$CustomAdapter.getView)。

LaunchActivity.java

public class LaunchActivity extends Activity {

    ImageView buttonAdd;
    EditText insertText;
    SubjectAdapter helper;
    ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        helper=new SubjectAdapter(this);
        listView = (ListView) findViewById(R.id.listViewLaunch);
        listView.setEmptyView(findViewById(R.id.emptyView));
        insertText = (EditText) findViewById(R.id.editTextLaunch);
        buttonAdd = (ImageView) findViewById(R.id.imageViewLaunch);

        buttonAdd.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String subject = insertText.getText().toString();

                long id=helper.insertData(subject);
                if(id<0){
                    Log.d("Personal", "insertData not successfull");
                } else { 
                    // Hiding the keyboard
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(insertText.getWindowToken(), 0);

                    // loading ListView with newly added data
                    loadListViewData();

                    Log.d("Personal", "insertData successfull");

                    Toast.makeText(LaunchActivity.this, subject+" hinzugefügt", Toast.LENGTH_LONG).show();

                    insertText.setText("");
                }
            }
        }); 
        loadListViewData();

    }

    public void loadListViewData() {
            // database handler
            SubjectAdapter helper = new SubjectAdapter(getApplicationContext());

            // Get Data from Database and set ArrayList
            ArrayList<String> list = (ArrayList<String>) helper.getData();

            // Creating adapter for ListView
            CustomAdapter adapter = new CustomAdapter(getApplicationContext(), list);

            // attaching data adapter to ListView
            listView.setAdapter(adapter);
    }

    class CustomAdapter extends BaseAdapter{

        ArrayList<String> list = new ArrayList<String>();
        Context context;

        // Constructor
        CustomAdapter(Context context, ArrayList<String> list){
            this.list = list;
            this.context = context;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

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

            // LayoutInflation and Recycling
            if(row == null){
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                inflater.inflate(R.layout.item_listview, parent, false);
            }

            TextView myText = (TextView) findViewById(R.id.textList);

            // Get String from ArrayList
            String temp = list.get(position);

            // Set Text for TextView inside ListView
            myText.setText(temp);

            return row;
        }

    }

}

2 个答案:

答案 0 :(得分:0)

First create object of ArrayList<String> in getData() and check code below:

 public void loadListViewData() {
            // database handler
            SubjectAdapter helper = new SubjectAdapter(getApplicationContext());

            // Get Data from Database and set ArrayList
            ArrayList<String> list = helper.getData();

            // Creating adapter for ListView
            CustomAdapter adapter = new CustomAdapter(getApplicationContext(), list);

            // attaching data adapter to ListView
            listView.setAdapter(adapter);
    }

    class CustomAdapter extends BaseAdapter{

        ArrayList<String> list = null;
        Context context;

        // Constructor
        CustomAdapter(Context context, ArrayList<String> list){
            this.list = list;
            this.context = context;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

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

            // LayoutInflation and Recycling
            if(row == null){
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                inflater.inflate(R.layout.item_listview, parent, false);
            }

            TextView myText = (TextView) findViewById(R.id.textList);

            // Get String from ArrayList
            String temp = list.get(position);

            // Set Text for TextView inside ListView
            myText.setText(temp);

            return row;
        }

    }
and make sure return type of getData() will be ArrayList<String>
hope it will help.

答案 1 :(得分:0)

如果row为null,则必须膨胀并将结果分配给行

row = inflater.inflate(R.layout.item_listview, parent, false);

此外,您还希望从myText获取row

TextView myText = (TextView) row.findViewById(R.id.textList);