Question

Hi im new to programming. I have a button that when clicked should change the image of an imageView to a different image. But when the button is clicked the app crashes. Im sure its something small im missing. Iv looked up the errors and cant seem to find anything can help sort out this problem. Im thankful for any help I can get.

public class MonthSelect extends Activity {

    //declaring variables
    ImageView image;
    Button jan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_select);

        addListenerOnButton();
    }

    private void addListenerOnButton() {
        image = (ImageView)findViewById(R.id.monthDisplay);

        jan = (Button)findViewById(R.id.januaryButton);
        jan.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                image.setImageResource(R.drawable.a_10);

            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.month_select, menu);
        return true;
    }

}

the xml with imageview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".MonthSelect" >

    <ImageView
        android:id="@+id/monthDisplay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/month_quote_display"
        android:src="@drawable/a_1" />

</RelativeLayout>

the errors

05-07 08:22:10.464: E/AndroidRuntime(7415): FATAL EXCEPTION: main
05-07 08:22:10.464: E/AndroidRuntime(7415): java.lang.NullPointerException
05-07 08:22:10.464: E/AndroidRuntime(7415):     at com.learning.MonthSelect$1.onClick(MonthSelect.java:54)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at android.view.View.performClick(View.java:4091)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at android.view.View$PerformClick.run(View.java:17072)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at android.os.Handler.handleCallback(Handler.java:615)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at android.os.Looper.loop(Looper.java:153)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at android.app.ActivityThread.main(ActivityThread.java:4987)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
05-07 08:22:10.464: E/AndroidRuntime(7415):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution 2

according to your you need to have R.id.januaryButton in activity_month_select layout

if you nead to display image in new window just create another activity with your image layout as contentview, then call the activity with image from the activity with button in the onclick function of button.

OTHER TIPS

There is no Button jan in your activity_month_select.xml file and you still create jan

jan = (Button)findViewById(R.id.januaryButton);// Cause Null Pointer Exception

You need to add Button with id januaryButton in your layout file.

It seems the layout you posted is not R.layout.activity_month_select, but a separate one that contains the ImageView you use as well. From what I can see from the logs, it seems R.layout.activity_month_select actually contains the button with the ID R.id.januaryButton, but not the ImageView R.id.monthDisplay.

Make sure those (the Button and the ImageView) are both in R.layout.activity_month_select.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top