0

I am having a problem with the code below

setContentView(R.layout.activity_main);
    TextView myLocation = (TextView)findViewById(R.id.txtAddress);
    myLocation.setText("123 Main St New York, NY");
    Linkify.addLinks(myLocation , Linkify.MAP_ADDRESSES);
    mainLayout.addView(myLocation);    

My problem is with the part that says

mainLayout.addView(myLocation);

it is complaining that "mainLayout cannot be resolved" that is the exact error that it is saying what i have put into the quotes.

does anyone have a suggestion on how to fix this?

Toxicvip1
  • 3
  • 2
  • Can you edit the question and paste EXACTLY what the error message says? – DreadHeadedDeveloper Oct 26 '14 at 19:01
  • the exact error is _mainLayout cannot be resolved_ – Toxicvip1 Oct 26 '14 at 23:15
  • You need to findViewById your layout "mainLayout" from the id attribute in XML, after setContentView, before mainLayout.addView(myLocation); so mainLayout can be resolved - just like you did for TextView myLocation - but should be done for layout - either RelativeLayout or LinearLayout - whichever you had set up in XML – Wildroid Oct 26 '14 at 23:22
  • can you please give an example? – Toxicvip1 Oct 26 '14 at 23:32
  • if it helps i have a RelativeLayout – Toxicvip1 Oct 26 '14 at 23:46
  • Not sure why you are adding the textview to the layout again. You already updated the textview with address. If you remove the addView line, Error would go away. But I provided my answer below in case you need to add another view. – Wildroid Oct 27 '14 at 01:44

1 Answers1

0

Not sure why you are adding the textview to the layout again. You already updated the textview with address. If you remove the addView line, Error would go away. Anyhow, this is how to eliminate error "mainLayout cannot be resolved": Find the view of the layout "mainLayout" from the id attribute in XML that was processed in onCreate - see added line:

setContentView(R.layout.activity_main);
    // add
    RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.layoutnameinXML);

    TextView myLocation = (TextView)findViewById(R.id.txtAddress);
    myLocation.setText("123 Main St New York, NY");
    Linkify.addLinks(myLocation , Linkify.MAP_ADDRESSES);
    mainLayout.addView(myLocation); 

In the XML you should give the RelativeLayout an id attribute, like:

<RelativeLayout        
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layoutnameinXML"  >
Wildroid
  • 864
  • 7
  • 9
  • thank you it got rid of that error but now it is not giving an error and on startup it crashes i was able to narrow it down to **mainLayout.addView(myLocation)** when i comment that part of the code out the program starts up but the address is not clickable meaning that it is just text not linkified but when that part of the code is not commented out it crashes the program on startup. – Toxicvip1 Oct 27 '14 at 04:28
  • Can you post the logcat of the crash that you get after commenting out the addView line. – Wildroid Oct 27 '14 at 11:21
  • The address should have a comma before City and before State. For example: 123 Main Street, New York, NY - or try Linkify.addLinks(myLocation , Linkify.ALL); for auto detect. – Wildroid Oct 27 '14 at 11:29
  • i am not able to post the logical sadly because it goes over the characters that are allowed. – Toxicvip1 Oct 27 '14 at 13:02
  • Did you try correcting the address as per my last comment above or use linkify.all - you need to learn more on how to use linkify - please don't forget to mark my answer accepted as it addressed the initial question. – Wildroid Oct 27 '14 at 13:10
  • yes and still giving me the same problem – Toxicvip1 Oct 27 '14 at 13:11
  • You need to learn more how to use linkify - [link](http://www.aviyehuda.com/blog/2011/01/27/android-creating-links-using-linkfy/) – Wildroid Oct 27 '14 at 13:17
  • I just tested the code and it worked fine - I used: myLocation.setText("123 Main Street, New York, NY"); Linkify.addLinks(myLocation , Linkify.MAP_ADDRESSES); – Wildroid Oct 27 '14 at 15:26
  • i figured out why it was not working it is because the emulator does not by default have google maps where a real device does as soon as i loaded it onto a real device it worked fine as long as the mainLayout.add(myLocation); is taken out or commented out it works. – Toxicvip1 Oct 27 '14 at 17:01