Search code examples
androidcheckedtextview

CheckedTextView not clickable


Hi i setup a CheckedTextView but I can't get the onClick event functioning. I put the onClick code in the onCreate of the main.layout but I get a nullpointer at line 101, which is chkBox.setOnClickListener(new View.OnClickListener(). The Listview is created in the onPostExecute of a AsyncTask. Can someone please help?

My CheckedTextView:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"  
         android:id="@+id/listCheckboxview"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_weight="1" android:gravity="left"  
         android:textColor="#0075AB"  android:textStyle="bold"  android:textSize="14dip" 
         android:checkMark="?android:attr/listChoiceIndicatorMultiple"   
         android:clickable="true" 
         android:focusable="true" 
         android:text=""  
         /> 

My onClick event:

CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.listCheckboxview); 
        chkBox.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) 
        { 
            ((CheckedTextView) v).toggle(); 
        } 
    });

Solution

  • I put the onClick code in the onCreate of the main.layout but i get a nullpointer at line 101, which is chkBox.setOnClickListener(new View.OnClickListener()

    This means that chkBox is null, which means that Android is not finding R.id.listCheckboxview. Make sure you are calling findViewById() on the right thing (here, you appear to be calling it on the activity, but your question mentions a ListView). Also, try cleaning your project (Project > Clean from the Eclipse main menu, or ant clean from the command line), as sometimes the R constants get out of sync.