I have added an image and a button in the xml as follows
<Button
android:id="@+id/btnChangeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/buttonplayicon" />
onclick of the button I want to change the image. Below is the main_activity onclick function of the button
public void addListenerOnButton() {
image = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.btnChangeImage);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
image.setImageResource(R.drawable.player_pause);
}
});
}
If I add
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
image.setImageResource(R.drawable.player_pause);
}
});
in the function, I get the message "Unfortunately app has stopped"
has any one idea regarding this?
EDIT 1
To view logcat if I follow ( in eclipse : Window > show view > other > android > Logcat ) steps I get Logcat(deprecated)
EDIT 2 logcat :
06-12 13:37:43.006: E/AndroidRuntime(3159): Caused by: java.lang.NullPointerException
06-12 13:37:43.006: E/AndroidRuntime(3159): at com.example.play_audio_app.MainActivity.addListenerOnButton(MainActivity.java:38)
06-12 13:37:43.006: E/AndroidRuntime(3159): at com.example.play_audio_app.MainActivity.onCreate(MainActivity.java:30)
06-12 13:37:43.006: E/AndroidRuntime(3159): at android.app.Activity.performCreate(Activity.java:5231)
06-12 13:37:43.006: E/AndroidRuntime(3159): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-12 13:37:43.006: E/AndroidRuntime(3159): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-12 13:37:43.006: E/AndroidRuntime(3159): ... 11 more
06-12 13:38:41.816: D/AndroidRuntime(3304): Shutting down VM
06-12 13:38:41.816: W/dalvikvm(3304): threadid=1: thread exiting with uncaught exception (group=0x4164eba8)
06-12 13:38:41.816: E/AndroidRuntime(3304): FATAL EXCEPTION: main
06-12 13:38:41.816: E/AndroidRuntime(3304): Process: com.example.play_audio_app, PID: 3304
06-12 13:38:41.816: E/AndroidRuntime(3304): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.play_audio_app/com.example.play_audio_app.MainActivity}: java.lang.NullPointerException
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.os.Handler.dispatchMessage(Handler.java:102)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.os.Looper.loop(Looper.java:136)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-12 13:38:41.816: E/AndroidRuntime(3304): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 13:38:41.816: E/AndroidRuntime(3304): at java.lang.reflect.Method.invoke(Method.java:515)
06-12 13:38:41.816: E/AndroidRuntime(3304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-12 13:38:41.816: E/AndroidRuntime(3304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-12 13:38:41.816: E/AndroidRuntime(3304): at dalvik.system.NativeStart.main(Native Method)
06-12 13:38:41.816: E/AndroidRuntime(3304): Caused by: java.lang.NullPointerException
06-12 13:38:41.816: E/AndroidRuntime(3304): at com.example.play_audio_app.MainActivity.addListenerOnButton(MainActivity.java:38)
06-12 13:38:41.816: E/AndroidRuntime(3304): at com.example.play_audio_app.MainActivity.onCreate(MainActivity.java:30)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.Activity.performCreate(Activity.java:5231)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-12 13:38:41.816: E/AndroidRuntime(3304): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-12 13:38:41.816: E/AndroidRuntime(3304): ... 11 more
EDIT 3
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
addListenerOnButton();
}