我想为setOnCLickListener
创建If Conditional,当Comment的( ImageButton )
值类似
forApproval.getVisibility() == View.VISIBLE
ImageButton Comment = (ImageButton) fidViewById(R.id.apprComment);
因为我在一个布局和不同的功能中有不同的设计,如果我创建* ex 2layout它没有效率*我认为
final LinearLayout forApproval = (LinearLayout) findViewById(R.id.ForApproval);
final LinearLayout forRequest = (LinearLayout) findViewById(R.id.ForRequest);
ImageButton Comment = null;
ImageButton Edit = null;
if (forApproval.getVisibility() == View.VISIBLE) {
Comment = (ImageButton) findViewById(R.id.apprComment);
}
// I Want to create multi value in Edit, example in here
if (forRequest.getVisibility() == View.VISIBLE) {
Edit = (ImageButton) findViewById(R.id.reqEdit);
}
if (forApproval.getVisibility() == View.VISIBLE) {
Edit = (ImageButton) findViewById(R.id.apprEdit);
}
// I have some ImageButton in Layout, each ImageButton has a different id
// and i want to make if conditional example like ImageButton ( in LinearLayout )
// is VISIBLE value of Edit is ImageButton with id reqEdit otherwise is
// ImageButton in ( LinearLayout VISIBLE value of Edit is ImageButton with id apprEdit
Edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO your Script
}
});
Comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO your Script
}
});
但我有错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at dca.application.taaku.hr.AttendandItemId.onCreate(AttendandItemId.java:194)
问题在于:
Edit.setOnClickListener(new View.OnClickListener() {
任何人都可以建议我吗?
答案 0 :(得分:3)
试试这个:将Click条件赋予clicklistener
Comment = (ImageButton) findViewById(R.id.apprComment);
Edit = (ImageButton) findViewById(R.id.reqEdit);
if (forRequest.getVisibility() == View.VISIBLE) {
Edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO your Script
}
});
}
if (forApproval.getVisibility() == View.VISIBLE) {
Comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO your Script
}
});
}
答案 1 :(得分:1)
试试这个
Edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (forRequest.getVisibility() == View.VISIBLE) {
// perform your action here
}
}
});
Comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (forApproval.getVisibility() == View.VISIBLE) {
// perform your action here
}
}
});
答案 2 :(得分:1)
NullPointer异常是因为forApproval和forRequest视图都不可见,因此ImageButton Edit为null。尝试在崩溃点之前调试forRequest.getVisibility()和forApproval.getVisibility(),以便解决当前问题。但如果这些代码在onCreate()中,那么你将面临更多的问题,最好以不同的方式命名reqEdit和apprEdit。
答案 3 :(得分:0)
只有当forApproval可见时才会初始化注释,因为在其他情况下它是null,如果两者都不可见则为编辑相同
试试这个
final LinearLayout forApproval = (LinearLayout) findViewById(R.id.ForApproval);
final LinearLayout forRequest = (LinearLayout) findViewById(R.id.ForRequest);
ImageButton Comment ;
ImageButton Edit = null;
if (forRequest.getVisibility() == View.VISIBLE) {
Edit = (ImageButton) findViewById(R.id.reqEdit);
}
if (forApproval.getVisibility() == View.VISIBLE) {
Comment = (ImageButton) findViewById(R.id.apprComment);
Edit = (ImageButton) findViewById(R.id.apprEdit);
}
if(Comment!=null){
Comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO your Script
}
});}
if(Edit!=null){
Edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO your Script
}
});}
答案 4 :(得分:0)
我有2个解决方案,
SOLUTION 1
int REQUEST=1,APPROVAL=2, FORWHAT=0;
if (forRequest.getVisibility() == View.VISIBLE) {
Edit = (ImageButton) findViewById(R.id.reqEdit);
FORWHAT=REQUEST;
}
if (forApproval.getVisibility() == View.VISIBLE) {
Edit = (ImageButton) findViewById(R.id.apprEdit);
FORWHAT=APPROVAL;
}
Edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO your Script
if(FORWHAT==REQUEST){
//TODO request code
}else if(FORWHAT==APPROVAL){
//TODO approval code
}else{
//TODO something else
}
}
});
<强> SOLUTION 2
强>
ImageButton comment; //variable starts with small letters.
ImageButton editRequest,editApproval;
comment = (ImageButton) findViewById(R.id.apprComment);
editRequest = (ImageButton) findViewById(R.id.reqEdit);
editApproval = (ImageButton) findViewById(R.id.apprEdit);
comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO your Script
}
});
editRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO your Script
}
});
editApproval.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO your Script
}
});
if (forApproval.getVisibility() == View.VISIBLE) {
comment.setVisibility(View.VISIBLE);
}
// I Want to create multi value in Edit, example in here
if (forRequest.getVisibility() == View.VISIBLE) {
editRequest.setVisibility(View.VISIBLE);
editApproval.setVisibility(View.GONE);
}
if (forApproval.getVisibility() == View.VISIBLE) {
editApproval.setVisibility(View.VISIBLE);
editRequest.setVisibility(View.GONE);
}
答案 5 :(得分:0)
我认为你需要一个动态变量来创建OnClick的条件。我建议你在崩溃点之前尝试调试forRequest.getVisibility()和forApproval.getVisibility(),这样你就可以看到结果并解决If Conditional的当前问题