Using Linkify in ListView's ArrayAdapter causes RuntimeException

前端 未结 3 1371
说谎
说谎 2021-01-16 09:18

I have a TextView in my ArrayAdapter that may contain some hyperlinks. For those links I use Linkify:

public View get         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-16 09:40

    Looking at the exception in the log, it seems that you used the application context when you allocate your ArrayAdapter. For example, if your code looks similar to the following:

        listView.setAdapter(new ArrayAdapter(context,
                android.R.layout.simple_list_item_1,
                data) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // ...
            }
        });
    

    You must have initialized the context variable above with the application context, like this:

        Context context = getApplicationContext();
    

    To avoid the error, you should have initialized it with your Activity instance instead:

        Context context = this;
    

    Or, if your code is in a Fragment:

        Context context = getActivity();
    

提交回复
热议问题