Solved

What's wrong on this little fragment of Android code

Posted on 2014-02-07
19
547 Views
Last Modified: 2014-02-10
Hi.

I have a little Android app which has a tab control on top to show a relative layout with a background image.

I would like that when user makes a click on the background image, this image change, so I added this code inside the onCreate event:

		findViewById(R.drawable.background1).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				nextImageEspera();
			}
		});

Open in new window


With this code, when starting app, it crashes. If I comment this code then all works fine (except the image don't change).

I know the problem is not on nextImageEspera() function because app crashes even if I comment this call.

What is wrong?
0
Comment
Question by:gplana
  • 8
  • 7
  • 3
  • +1
19 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39842979
Have you tried debugging it? What is the error message?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39843093
Doesn't this look odd to you :

findViewById(R.drawable.background1)

shouldn't it be more like :

findViewById(R.id.background1);

?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39843142
0
 
LVL 15

Author Comment

by:gplana
ID: 39845453
Thank you all for your comments.

This is the logcat just after the app crashed. I have commented the line with the call, so my code is this:

		findViewById(R.drawable.background1).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				nextImageEspera();
			}
		});

Open in new window


Any help?

Thanks
logErrors.txt
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39845562
logErrors.txt 0 bytes
The log file is empty
0
 
LVL 15

Author Comment

by:gplana
ID: 39845613
Sorry, it was my fault. Now here are the logs.
logErrors.txt
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39845887
Please post line 114 of FragmentTabs.java
0
 
LVL 15

Author Comment

by:gplana
ID: 39845907
Is the first line inside function  nextImageEspera:

113      public void nextImageEspera(){
114            RelativeLayout v =(RelativeLayout)findViewById(R.id.root);
115            v.setBackgroundResource(R.drawable.detall_espera);
      }
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39845918
Could be you are casting to the wrong type somewhere.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 39845920
That's your problem: it's not a RelativeLayout, so cannot be cast to it
0
 
LVL 15

Author Comment

by:gplana
ID: 39845936
I think it is:

<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="@drawable/background1"
    android:id="@+id/root"
    tools:context=".MainActivity" >

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39845942
Nope. You can find out what it is by

System.out.println(findViewById(R.id.root).getClass());

Open in new window

0
 
LVL 15

Author Comment

by:gplana
ID: 39845986
Thank you. You were right. It seems to be a LinearLayout.

After changing this the app doesn't crash anymore but the image still don't change. Why?

This is the code on the function now:

	public void nextImageEspera(){
		System.out.println(findViewById(R.id.root).getClass());
		LinearLayout v =(LinearLayout)findViewById(R.id.root);
		v.setBackgroundResource(R.drawable.detall_espera);
		v.invalidate();
	}

Open in new window


And I attach logs
log4.txt
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39846018
Well now i'm at a disadvantage - i don't do Android ;) Try calling repaint?
0
 
LVL 15

Author Comment

by:gplana
ID: 39846525
I have read that invalidate() is the method to do a repaint. Is that not true?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39846783
Ha - I don't do Android either, but from sniffing around, one reason could be that your thread is asleep, similar to the effect of running a long-winded command - or calling sleep() - on the Event Dispatch Thread, EDT, in mainstream Java. Maybe check you haven't called sleep() like that.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39846790
I would guess the main purpose of that is to layout the gui again. I know that sometimes with Swing at least, revalidate is insufficient to cause the display to update properly and repaint has to be called as well. So just guessing here
0
 
LVL 15

Author Comment

by:gplana
ID: 39846887
You were right. After some investigation, I have found another way to refresh. This is my current code:

      public void nextImageEspera(){
            System.out.println(findViewById(R.id.root).getClass());
            LinearLayout v =(LinearLayout)findViewById(R.id.root);
            v.setBackgroundResource(R.drawable.detall_espera);
            v.invalidate();
           v.removeAllViews();
             v.refreshDrawableState();
     }

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39846929
Good :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
starter POM and spring-boot-starter,  spring-boot-web 2 43
Java Timer (static) 9 36
reverse digits of a number using for loop 5 36
iPhone v Android phone 7 30
Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now