Question

I have an list adapter that I use to make a list view

TaskAdapter adapter = new TaskAdapter(ToDoList.this, R.layout.item_task, tasks);
setListAdapter(adapter);
adapter.notifyDataSetChanged();

I'm trying to do an onclick for it, but it keeps crashing the app.

    ImageView iv_delete = (ImageView) findViewById(R.id.task_delete_image);
    iv_delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String tag_del = (String) v.getTag();
            dm.deleteItem(tag_del);
            finish();
            startActivity(getIntent());
        }
    });

Log

05-02 17:46:19.405: E/AndroidRuntime(3766): FATAL EXCEPTION: main
05-02 17:46:19.405: E/AndroidRuntime(3766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geotasker/com.example.geotasker.ToDoList}: java.lang.NullPointerException
05-02 17:46:19.405: E/AndroidRuntime(3766):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-02 17:46:19.405: E/AndroidRuntime(3766):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-02 17:46:19.405: E/AndroidRuntime(3766):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-02 17:46:19.405: E/AndroidRuntime(3766):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-02 17:46:19.405: E/AndroidRuntime(3766):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 17:46:19.405: E/AndroidRuntime(3766):     at android.os.Looper.loop(Looper.java:137)
05-02 17:56:16.281: E/AndroidRuntime(3861): FATAL EXCEPTION: main
05-02 17:56:16.281: E/AndroidRuntime(3861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geotasker/com.example.geotasker.ToDoList}: java.lang.NullPointerException
05-02 17:56:16.281: E/AndroidRuntime(3861):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at android.os.Looper.loop(Looper.java:137)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at java.lang.reflect.Method.invoke(Method.java:525)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at dalvik.system.NativeStart.main(Native Method)
05-02 17:56:16.281: E/AndroidRuntime(3861): Caused by: java.lang.NullPointerException
05-02 17:56:16.281: E/AndroidRuntime(3861):     at com.example.geotasker.ToDoList.onCreate(ToDoList.java:55)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at android.app.Activity.performCreate(Activity.java:5133)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-02 17:56:16.281: E/AndroidRuntime(3861):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-02 17:56:16.281: E/AndroidRuntime(3861):     ... 11 more

Activity:

public class ToDoList extends ListActivity {

    private ActionBarActivity abarAct;
    DataManager dm;
    ArrayList<Task> tasks;
    Task task;

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

        abarAct = new ActionBarActivity();

        tasks = new ArrayList<Task>();
        task = new Task();
        dm = new DataManager(this);

        new GetGeoTasks().execute();

        ImageView iv_delete = (ImageView) findViewById(R.id.task_delete_image);
        iv_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String tag_del = (String) v.getTag();
                dm.deleteItem(tag_del);
                finish();
                startActivity(getIntent());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.to_do_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_to_do_list, container, false);
            return rootView;
        }
    }

    private class GetGeoTasks extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            tasks = dm.getAllTasks();
            Log.d("demo", "doInBackground ACCESSED");

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            TaskAdapter adapter = new TaskAdapter(ToDoList.this, R.layout.item_task, tasks);
            setListAdapter(adapter);
            adapter.notifyDataSetChanged();

            Log.d("demo", "onPostExecute ACCESSED");

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    }
}

Any ideas at how I could access an ImageView inside of a layout that was created dynamically in a ArrayAdapter? Thanks for any help!

Was it helpful?

Solution

I would assume that line 55 is iv_delete.setOnClickListener(...). If task_delete_image is a View inside the ListView's item view, then you cannot use findViewById() here, as the ListView items haven't been created yet.

Your best alternative would be to modify your custom adapter (TaskAdapter), and set the OnClickListener in the getView() method for each item. See for example this answer.

On a totally unrelated subject, finishing the activity and creating a new one just to refresh doesn't seem like a good idea.

On another totally unrelated subject ;) as the commenters to the question said, the ActionBarActivity member doesn't make a lot of sense. Activity classes should never be directly instantiated, the framework will do that with calls to startActivity().

OTHER TIPS

The problem is ListActivity already has a built in onclicklistener you just put your code into it. Untested answer.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
            String tag_del = (String) v.getTag();
            dm.deleteItem(tag_del);
            finish();
            startActivity(getIntent()); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top