I'm trying to work on an exercise, which means i need to change an image from an imageView with a button. The whole thing is implemented into another project of the exercise.
The problem is: it doesn't even open the Toast "test" in the second case.
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 File:
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" >
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" >
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1" >
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/celsius" />
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fahrenheit" />
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" />
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" />
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" />
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" />
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" />
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.(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