Domanda

I want to create a BaseAdapter for a ListView. From my database I get an ArrayList that contains the entries for the ListView. The onCreate Method works fine but if I click the button to insert data to the database and display it in the ListView my app crashes. I'm looking for the Solution but can't find it. Maybe you do.
Logcat says there is a NullPointerException at the point where the text of the TextView is set (LaunchActivity$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;
        }
    
    }

}
È stato utile?

Soluzione

If row is null you have to inflate and assign the result to row!

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

Also you'll want to get myText from row:

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

Altri suggerimenti

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.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top