我正在尝试练习,这意味着我需要使用按钮从imageView更改图像。整个事情被实施到练习的另一个项目中。
问题是:它在第二种情况下甚至没有打开Toast“测试”。
package de.vogella.android.temperature;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity{
private EditText text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.tempbutton);
Button button2 = (Button) findViewById(R.id.imageconverter);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tempbutton:
button1Click();
break;
case R.id.imageconverter:
button2Click();
break;
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
}
public void button1Click (){
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(String.valueOf(ConverterUtil.convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(false);
fahrenheitButton.setChecked(true);
} else {
text.setText(String
.valueOf(ConverterUtil.convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
}
public void button2Click (){
ImageView frontImageView = (ImageView) findViewById(R.id.myicon);
frontImageView.setImageResource(R.drawable.assigned);
Toast.makeText(this,"done", Toast.LENGTH_LONG).show();
}
}
XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/myColor"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:inputType="numberSigned|numberDecimal"
android:ems="10" >
<requestFocus />
</EditText>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/celsius" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fahrenheit" />
</RadioGroup>
<Button
android:id="@+id/tempbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:text="@string/calc"
android:onClick="onClick" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentBottom="true"
android:text="@string/author"
android:textAppearance="?android:attr/textAppearanceSmall" />
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/button1"
android:text="DigitalClock" />
<Button
android:id="@+id/imageconverter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:onClick="onClick"
android:text="Change Image" />
<ImageView
android:id="@+id/myicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:src="@drawable/initial" />
</RelativeLayout>
logcat的:
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.os.Handler.dispatchMessage(Handler.java:102)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.os.Looper.loop(Looper.java:136)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-25 16:03:00.228: E/AndroidRuntime(1363): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 16:03:00.228: E/AndroidRuntime(1363): at java.lang.reflect.Method.invoke(Method.java:515)
02-25 16:03:00.228: E/AndroidRuntime(1363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-25 16:03:00.228: E/AndroidRuntime(1363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-25 16:03:00.228: E/AndroidRuntime(1363): at dalvik.system.NativeStart.main(Native Method)
02-25 16:03:00.228: E/AndroidRuntime(1363): Caused by: java.lang.NullPointerException
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.Activity.findViewById(Activity.java:1884)
02-25 16:03:00.228: E/AndroidRuntime(1363): at de.vogella.android.temperature.MainActivity.<init>(MainActivity.java:24)
02-25 16:03:00.228: E/AndroidRuntime(1363): at java.lang.Class.newInstanceImpl(Native Method)
02-25 16:03:00.228: E/AndroidRuntime(1363): at java.lang.Class.newInstance(Class.java:1208)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-25 16:03:00.228: E/AndroidRuntime(1363): ... 11 more
答案 0 :(得分:5)
Button button1 = (Button) findViewById(R.id.tempbutton);
Button button2 = (Button) findViewById(R.id.imageconverter);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tempbutton:
button1Click();
break;
case R.id.imageconverter:
Toast.makeText(this, "test",Toast.LENGTH_LONG).show();
button2Click();
break;
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
答案 1 :(得分:0)
首先,如果你想这样做,那么你的活动需要实现View.OnClickListener
。
接下来,您需要设置按钮的点击监听器,可能在onCreate
。例如,<yourButton>.setOnClickListener(this)
。
答案 2 :(得分:0)
除非您在布局xml中执行此操作,否则您不会设置单击侦听器。您需要实施View.OnClickListener
并在每个按钮上调用setOnClickListener
。
答案 3 :(得分:0)
我认为你的“测试”吐司是不可见的,因为在你展示它之后,你会展示另一个吐司,即“完成”吐司。最后一个可见,对吧?
哦,你的android:onClick="onClick"
按钮错过了xml中的imageconverter
。