2015-05-04 57 views
-2

logact如何在android中設置複選框和按鈕的可見性?

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): \t at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at android.app.ActivityThread.access$500(ActivityThread.java:122) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at android.os.Handler.dispatchMessage(Handler.java:99) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at android.os.Looper.loop(Looper.java:132) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at android.app.ActivityThread.main(ActivityThread.java:4123) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at java.lang.reflect.Method.invokeNative(Native Method) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at java.lang.reflect.Method.invoke(Method.java:491) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t 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): \t at com.example.registerscreen.FriendListActivity.onCreate(FriendListActivity.java:68) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 
 
05-04 17:53:15.820: E/AndroidRuntime(7709): \t at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)

我能夠得到列表視圖我的電話接觸列表視圖的name.On右側我添加喜歡邀請button.Now我想要一個按鈕當我點擊這個按鈕時,這個按鈕就消失了,複選框出現了刻度標記。

我嘗試了很多東西,但我無法做到這一點。

public class FriendListActivity extends Activity { 
 

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

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

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

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

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

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

 
\t

<?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>

+2

你有什麼試過?什麼事情沒有發生? –

+0

@Prera​​kSola我發佈了我的完整代碼,請給我看看...並告訴我我錯在哪裏...... –

+0

你得到了什麼錯誤?任何力量關閉? –

回答

0

加入這一行:

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

和使用這樣的:

button.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
    button.setVisibility(View.GONE);  
    checkbox.setVisibility(View.VISIBLE); 
} 
}); 
+0

我已經發布我的完整代碼,請給一個看...並告訴我我錯在哪裏...... –

+0

@vivekthakur你的XML佈局中的按鈕btn_invite在哪裏?你在java代碼中聲明覆選框? – meysam

+0

@vivekthakur檢查我的更新回答 – meysam

0

您需要實例化CheckBox對象chk

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

添加上面的行的行後面

​​

+0

我已經發出複選框,,,但同樣的錯誤發生......空指針例外.. –

+0

我看不到您已提供的代碼中的啓動。你能否也請在你的問題中發佈logcat,以便我可以看到異常是在哪裏? –

+0

我已添加我的logcat ...請檢查.. –

0

初始化此線下方的checkbok:

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

//初始化複選框這樣的:

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