-2

logact

05-04 17:53:15.820: E/AndroidRuntime(7709): FATAL EXCEPTION: main
05-04 17:53:15.820: E/AndroidRuntime(7709): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.registerscreen/com.example.registerscreen.FriendListActivity}: java.lang.NullPointerException
05-04 17:53:15.820: E/AndroidRuntime(7709):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at android.os.Looper.loop(Looper.java:132)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at android.app.ActivityThread.main(ActivityThread.java:4123)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at java.lang.reflect.Method.invokeNative(Native Method)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at java.lang.reflect.Method.invoke(Method.java:491)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at dalvik.system.NativeStart.main(Native Method)
05-04 17:53:15.820: E/AndroidRuntime(7709): Caused by: java.lang.NullPointerException
05-04 17:53:15.820: E/AndroidRuntime(7709):  at com.example.registerscreen.FriendListActivity.onCreate(FriendListActivity.java:68)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
05-04 17:53:15.820: E/AndroidRuntime(7709):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)

I am able to get my phone contact on list view with name.On right side of list view I have added a button like invite button.Now I want when I click on this button then this button disappear and the check box is appeared with tick mark.

I tried many things but I am unable to do this.

  public class FriendListActivity extends Activity {

SimpleCursorAdapter adapter;
ListView lvContacts;
Button b1;
CheckBox chk;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 this.requestWindowFeature(Window.FEATURE_NO_TITLE);
 setContentView(R.layout.activity_friend_listview);
 

 lvContacts = (ListView) findViewById(R.id.lv_friend_list);
 ContentResolver cr = getContentResolver();

 // Read Contacts
 Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI,
 new String[] { ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME }, null, null,
   null);

 // Attached with cursor with Adapter
 adapter = new SimpleCursorAdapter(this, R.layout.activity_phonecontact,
   c, new String[] { ContactsContract.Contacts.DISPLAY_NAME },
   new int[] { R.id.tv_name });
        lvContacts.setAdapter(adapter);
        

        
        b1 = (Button)findViewById(R.id.btn_invite);
        chk = (CheckBox)findViewById(R.id.chk_frnd);
        b1.setOnClickListener(new OnClickListener() 
        {
  
                   @Override
                   public void onClick(View arg0) {
                   b1.setVisibility(View.GONE);    
                   chk.setVisibility(View.VISIBLE);
               } 
               });
              }
          
        
      }

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <ImageView
        android:id="@+id/img_frnd_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/include1"
        android:background="@null"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:contentDescription="@string/imageView_contact_image"
        android:src="@drawable/contactimage" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/include1"
        android:layout_toRightOf="@+id/img_frnd_list"
        android:paddingLeft="10dp"
        android:paddingTop="35dp"
        android:textSize="@dimen/contact_text_size"
        android:text="@string/Contact_Name" />
   
    <Button
         android:text="Play" 
         android:id="@+id/btn_invite" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingTop="80dp"
        android:visibility="visible"
        android:layout_below="@+id/include1"
        android:paddingRight="10dp" >
</Button>

    <CheckBox
        android:id="@+id/chk_frnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:visibility="gone"
        android:layout_alignParentRight="true"
        android:paddingTop="80dp"
        android:layout_below="@+id/include1"
        android:paddingRight="10dp" />
    
</RelativeLayout>
vivek thakur
  • 129
  • 2
  • 14

4 Answers4

1
//initialize the checkbox and button like this:
chk = (CheckBox)findViewById(R.id.chk_frnd);
b1 = (Button)findViewById(R.id.btn_invite);
0

add this line :

chk = (CheckBox)findViewById(R.id.chk_frnd);
b1 = (Button)findViewById(R.id.btn_invite);

and use something like this :

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    button.setVisibility(View.GONE);    
    checkbox.setVisibility(View.VISIBLE);
} 
}); 
Meysam
  • 694
  • 6
  • 20
  • I have posted my complete code please give a look...and tell me where i am wrong.... – vivek thakur May 04 '15 at 12:15
  • @vivekthakur where is button btn_invite in your xml layout ? where you declare the checkbox in java code ? – Meysam May 04 '15 at 12:21
  • i have added the complete updated code ...please check – vivek thakur May 04 '15 at 12:38
  • @vivekthakur its beacuse you declare your code in onCreate method, move all code after setContentView(R.layout.activity_friend_listview); outside oncreate.ex : create a method move your code on it and call method in onCreate. – Meysam May 04 '15 at 12:56
  • @meysam: What difference does it make? Creating a function won't solve the problem. – Prerak Sola May 04 '15 at 13:16
0

You need to instantiate the CheckBox object chk.

chk = (CheckBox)findViewById(R.id.chk_frnd);

Add the above line after the line

b1 = (Button)findViewById(R.id.btn_invite);

Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
  • i have intiated the checkbox,,,but the same error is occured...null pointer exception.. – vivek thakur May 04 '15 at 12:24
  • I cannot see the initiation in the code that you have provided. Can you please also post the logcat in your question, so that I can see where the exception is? – Prerak Sola May 04 '15 at 12:26
0

Initialize the checkbok below this Line:

 b1 = (Button)findViewById(R.id.btn_invite);

//initialize the checkbox like this:

chk = (CheckBox)findViewById(R.id.chk_frnd);
Rathan Kumar
  • 2,567
  • 2
  • 17
  • 24