将可绘制图像与图像视图图标进行比较 android?
Comparing drawable image with imageview icon android?
我正在将图像视图与可绘制图像进行比较。如果相同,那么我需要写一个逻辑。
如果不一样,我需要处理另外一个逻辑。
所以我尝试的是,
private ImageView emailIcon, phoneIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentView = inflater.inflate(R.layout.activity_summary_activities, null);{
emailIcon = (ImageView) findViewById(R.id.email_icon);
phoneIcon = (ImageView) findViewById(R.id.sms_icon);
phoneIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setBackgroundResource(R.drawable.round);
emailIcon.setBackgroundResource(R.drawable.ic_mark_white);
}
});
emailIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setBackgroundResource(R.drawable.ic_mark_white);
emailIcon.setBackgroundResource(R.drawable.round);
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (phoneIcon.getDrawable().getConstantState() == SummaryActivities.this
.getResources().getDrawable(R.drawable.ic_mark_white)
.getConstantState()) {
Log.e("going here", "going here");
}
}
但我越来越崩溃了,
java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'android.graphics.drawable.Drawable$ConstantState android.graphics.drawable.Drawable.getConstantState()'
在 com.view.SummaryActivities$5.onClick(SummaryActivities.java:130)
在xml中:
<ImageView
android:id="@+id/sms_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="3dp"
android:background="@drawable/ic_mark_white" />
<ImageView
android:id="@+id/email_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="2dp"
android:background="@drawable/ic_mark_white" />
在 onCreate() 中添加 contentView
并初始化 submitBtn
,如下所示
private ImageView emailIcon, phoneIcon;
private submitBtn; //change here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentView = inflater.inflate(R.layout.activity_summary_activities, null);{
emailIcon = (ImageView) contentView.findViewById(R.id.email_icon);
phoneIcon = (ImageView) contentView.findViewById(R.id.sms_icon);
submitBtn = (Button)contentView.findViewById(R.id.button_id); //change here
phoneIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setBackgroundResource(R.drawable.round);
emailIcon.setBackgroundResource(R.drawable.ic_mark_white);
}
});
emailIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setBackgroundResource(R.drawable.ic_mark_white);
emailIcon.setBackgroundResource(R.drawable.round);
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (phoneIcon.getDrawable().getConstantState() == SummaryActivities.this
.getResources().getDrawable(R.drawable.ic_mark_white)
.getConstantState()) {
Log.e("going here", "going here");
}
}
使用 setImageResource 代替 setBackgroundResource
private ImageView emailIcon, phoneIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentView = inflater.inflate(R.layout.activity_summary_activities,
null);{
emailIcon = (ImageView) findViewById(R.id.email_icon);
phoneIcon = (ImageView) findViewById(R.id.sms_icon);
phoneIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setImageResource(R.drawable.round);
emailIcon.setImageResource(R.drawable.ic_mark_white);
}
});
emailIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setImageResource(R.drawable.ic_mark_white);
emailIcon.setImageResource(R.drawable.round);
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (phoneIcon.getDrawable().getConstantState() ==
SummaryActivities.this
.getResources().getDrawable(R.drawable.ic_mark_white)
.getConstantState()) {
Log.e("going here", "going here");
}
}
我正在将图像视图与可绘制图像进行比较。如果相同,那么我需要写一个逻辑。
如果不一样,我需要处理另外一个逻辑。
所以我尝试的是,
private ImageView emailIcon, phoneIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentView = inflater.inflate(R.layout.activity_summary_activities, null);{
emailIcon = (ImageView) findViewById(R.id.email_icon);
phoneIcon = (ImageView) findViewById(R.id.sms_icon);
phoneIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setBackgroundResource(R.drawable.round);
emailIcon.setBackgroundResource(R.drawable.ic_mark_white);
}
});
emailIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setBackgroundResource(R.drawable.ic_mark_white);
emailIcon.setBackgroundResource(R.drawable.round);
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (phoneIcon.getDrawable().getConstantState() == SummaryActivities.this
.getResources().getDrawable(R.drawable.ic_mark_white)
.getConstantState()) {
Log.e("going here", "going here");
}
}
但我越来越崩溃了,
java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'android.graphics.drawable.Drawable$ConstantState android.graphics.drawable.Drawable.getConstantState()' 在 com.view.SummaryActivities$5.onClick(SummaryActivities.java:130)
在xml中:
<ImageView
android:id="@+id/sms_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="3dp"
android:background="@drawable/ic_mark_white" />
<ImageView
android:id="@+id/email_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="2dp"
android:background="@drawable/ic_mark_white" />
在 onCreate() 中添加 contentView
并初始化 submitBtn
,如下所示
private ImageView emailIcon, phoneIcon;
private submitBtn; //change here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentView = inflater.inflate(R.layout.activity_summary_activities, null);{
emailIcon = (ImageView) contentView.findViewById(R.id.email_icon);
phoneIcon = (ImageView) contentView.findViewById(R.id.sms_icon);
submitBtn = (Button)contentView.findViewById(R.id.button_id); //change here
phoneIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setBackgroundResource(R.drawable.round);
emailIcon.setBackgroundResource(R.drawable.ic_mark_white);
}
});
emailIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setBackgroundResource(R.drawable.ic_mark_white);
emailIcon.setBackgroundResource(R.drawable.round);
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (phoneIcon.getDrawable().getConstantState() == SummaryActivities.this
.getResources().getDrawable(R.drawable.ic_mark_white)
.getConstantState()) {
Log.e("going here", "going here");
}
}
使用 setImageResource 代替 setBackgroundResource
private ImageView emailIcon, phoneIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentView = inflater.inflate(R.layout.activity_summary_activities,
null);{
emailIcon = (ImageView) findViewById(R.id.email_icon);
phoneIcon = (ImageView) findViewById(R.id.sms_icon);
phoneIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setImageResource(R.drawable.round);
emailIcon.setImageResource(R.drawable.ic_mark_white);
}
});
emailIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
phoneIcon.setImageResource(R.drawable.ic_mark_white);
emailIcon.setImageResource(R.drawable.round);
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (phoneIcon.getDrawable().getConstantState() ==
SummaryActivities.this
.getResources().getDrawable(R.drawable.ic_mark_white)
.getConstantState()) {
Log.e("going here", "going here");
}
}