tags:

views:

80

answers:

3

Is there anywhere that I can find documentation on the scope of the XML files? I have an app I am currently working on and have been struggling with getting a feature to work and it seems that the problem I am having is that I am trying to access an element in an XML file that must be out of scope. To simplify the layout, my project has main.xml, sub.xml, main.java, and sub.java files in it. As you can probably guess, main.java works with main.xml and sub.java is working with the elements in sub.xml. Here's where the issue comes in, I have a TextView element that is created in main.xml that I would like to modify the text in, but the action that would trigger it will occur in sub.java. I can't figure out how to change it from sub.java, and I can't figure out how to move the element into sub.xml. The code I am using is pretty simple:

TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText(filePath);

I get a FC every time I run the app, but if I move the code into main.java, it runs flawlessly. If anyone can offer any ideas, or point me in the direction of some documentation that would explain what java files can access what elements in which xml files, that would be awesome! Sorry for the novel, but I'm just struggling to get the point across. Thanks.

A: 

Activity.findViewById(int) only works if that view is in the activity's layout. So no, you can't refer to a view in main.xml because that layout doesn't apply to sub.

Do you have any TextViews in sub.xml called myTitle?

magaio
Sub.xml is the layout for the rows in a list, and main.xml is the layout for the main UI, so I don't think I can put the textView in sub.xml. Maybe I need to rethink the entire layout! *sigh*
Bryan
A: 

Hai Bryan, U can access the the textview of main.java(main.xml) in submain.java as follows In main.java write the following code

  static TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText(filePath);

and u can access this submain.java as

   Main.titleText.setText(filePath);
Tilsan The Fighter
I tried that, but I get an error saying that I can't access the non-static function findViewById in a static context.
Bryan
@Mr.Bryan: Can u post the code what u tried now
Tilsan The Fighter
Please post full the code i would like it review it Bryan
Tilsan The Fighter
I'm a little limited on space in the forums, so I'm not sure exactly what you would want me to post. I actually tried exactly the code that you listed above in the main.xml file and the error that Eclipse gives is: "Cannot make a static reference to the non-static method findViewById(int) from the type Activity"
Bryan
Bryan: try like this: static TextView titleText;
Tilsan The Fighter
A: 
try like this Bryan in Main.xml file it works with no issue...........Declare first & then Initialize it...



  public class Main extends Activity {

        static TextView tv;
        static Button submit;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {

            tv = (TextView) findViewById(R.id.header_text1);
            }
}
Tilsan The Fighter
Ok, so it compiles and runs that way. I added a declaration for the TextView before the onCreate method, then created a public static void method that named setCustomTitle() that just called titleText.setText(sub.filePath); In sub.java I called main.setCustomTitle. Everything seemed kosher, but when I caused a screen refresh in the emulator that calls the function, it FCs. I ran it in debug, and it calls setCustomTitle() just fine, but when it tries to execute the line titleText.setText(filePath), it errors. Any ideas? Thanks so much for your help Tilsan!
Bryan
FYI, just to verify, I changed the setText call to titleText.setText("New Title") and it still gave me a FC, so that shows that it is erroring out when it tries to set the text, not necessarily when it tries to access sub.filePath from main.java.
Bryan
can u please tell what error message, u got in ur Logcat Bryan
Tilsan The Fighter
NullPointerException
Bryan
You know what? Getting the error from the Logcat helped me finish it off myself. Sorry, I'm new to this and didn't know about Logcat until you asked. Thanks so much for the help Tilsan, YOU ROCK!!!
Bryan
I got so excited to see that my problem was fixed, I forgot to try to help others who might be viewing this. So, for the sake of future visitors: What Tilsan suggested was spot on, but I caused my own pain. I decided to get rid of the tv= statement in the onCreate method and just set the text when I wanted it to show up. This was causing me to get a NullPointerException because I had declared the TextView, but never instantiated it. If you want to do this, declare the TextView as a static outside of the onCreate method, instantiate the TextView inside of it, then you can hit it from anywhere.
Bryan
Mr.Bryan: Hmmm Nice and Very happy to see, finally u achieved the war b/w u and the Bug!!! Hope in future too u will win the war against Bug. By Tilsan(Ur Friend)
Tilsan The Fighter