Re: [android-developers] ImageView change image

No. that image is definitely there. The word "button" is getting underlined. It says that it cannot find it. I think it is because button is declared in the other method. How do I fix this?

On Sun, Apr 3, 2011 at 12:17 AM, Mark Murphy <mmurphy@commonsware.com> wrote:
You do not have a drawable resource named m1s, most likely.

On Sat, Apr 2, 2011 at 2:44 PM, Raghav Sood <raghavsood@gmail.com> wrote:
> Okay I fixed that but now the line in red cannot find the button imageview.
>
> public class class extends Activity {
>
>
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>         AdView adView = (AdView)findViewById(R.id.ad);
>         adView.requestFreshAd();
>         ImageView button = (ImageView)findViewById(R.id.l1m1);
>         // Register the onClick listener with the implementation above
>         button.setOnClickListener(mCorkyListener);
>
>     }
>     public OnClickListener mCorkyListener = new OnClickListener() {
>         public void onClick(View v) {
>           button.setImageResource (R.drawable.m1s);
>         }
>     };
> }
>
> I know its because the button variable is part of the other method but how
> do I fix it?
>
> On Sun, Apr 3, 2011 at 12:09 AM, Raghav Sood <raghavsood@gmail.com> wrote:
>>
>> Sorry my fault. forgot to remove the imageview declaration from top.
>>
>> On Sun, Apr 3, 2011 at 12:07 AM, Raghav Sood <raghavsood@gmail.com> wrote:
>>>
>>> Okay I fixed that part now but it still force closes. My code now is:
>>> public class class extends Activity {
>>>     ImageView button = (ImageView)findViewById(R.id.l1m1);
>>>     public OnClickListener mCorkyListener = new OnClickListener() {
>>>         public void onClick(View v) {
>>>           button.setImageResource (R.drawable.m1s);
>>>         }
>>>     };
>>>
>>>     /** Called when the activity is first created. */
>>>     @Override
>>>     public void onCreate(Bundle savedInstanceState) {
>>>         super.onCreate(savedInstanceState);
>>>         setContentView(R.layout.main);
>>>         AdView adView = (AdView)findViewById(R.id.ad);
>>>         adView.requestFreshAd();
>>>         ImageView button = (ImageView)findViewById(R.id.l1m1);
>>>         // Register the onClick listener with the implementation above
>>>         button.setOnClickListener(mCorkyListener);
>>>
>>>     }
>>> }
>>>
>>> and the log reads:
>>>
>>> 04-03 00:06:53.383 I/ActivityManager( 1368): Start proc com.raghavsood.mp
>>> for activity com.raghavsood.mp/.matchpuzzler: pid=26022 uid=10113
>>> gids={3003}
>>> 04-03 00:06:53.553 W/dalvikvm(26022): threadid=1: thread exiting with
>>> uncaught exception (group=0x40020ac0)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): FATAL EXCEPTION: main
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): java.lang.RuntimeException:
>>> Unable to instantiate activity
>>> ComponentInfo{com.raghavsood.mp/com.raghavsood.mp.matchpuzzler}:
>>> java.lang.NullPointerException
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> android.app.ActivityThread.
>>>
>>> performLaunchActivity(ActivityThread.java:2585)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> android.app.ActivityThread.access$2300(ActivityThread.java:125)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> android.os.Handler.dispatchMessage(Handler.java:99)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> android.os.Looper.loop(Looper.java:123)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> android.app.ActivityThread.main(ActivityThread.java:4627)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> java.lang.reflect.Method.invokeNative(Native Method)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> java.lang.reflect.Method.invoke(Method.java:521)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> dalvik.system.NativeStart.main(Native Method)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): Caused by:
>>> java.lang.NullPointerException
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> android.app.Activity.findViewById(Activity.java:1637)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> com.raghavsood.mp.matchpuzzler.<init>(matchpuzzler.java:14)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> java.lang.Class.newInstanceImpl(Native Method)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> java.lang.Class.newInstance(Class.java:1429)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> android.app.Instrumentation.newActivity(Instrumentation.java:1021)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): at
>>> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
>>> 04-03 00:06:53.573 E/AndroidRuntime(26022): ... 11 more
>>> 04-03 00:06:53.583 W/ActivityManager( 1368):   Force finishing activity
>>> com.raghavsood.mp/.matchpuzzler
>>>
>>> Now I have no idea as to what is wrong though it still seems to be my
>>> findviewbyid
>>>
>>> Please help
>>>
>>> --
>>> Raghav Sood
>>> http://www.raghavsood.com/
>>> http://www.androidappcheck.com/
>>> http://www.telstop.tel/
>>
>>
>>
>> --
>> Raghav Sood
>> http://www.raghavsood.com/
>> http://www.androidappcheck.com/
>> http://www.telstop.tel/
>
>
>
> --
> Raghav Sood
> http://www.raghavsood.com/
> http://www.androidappcheck.com/
> http://www.telstop.tel/
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en



--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to *Advanced* Android Development_ Version
1.9.2 Available!

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
Raghav Sood
http://www.raghavsood.com/
http://www.androidappcheck.com/

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en