-2

Java/XML newbie using Android Studio 3.1.1, I receive an error:

error: not well-formed (invalid token).
Message{kind=ERROR, text=error: not well-formed (invalid token)., sources=[C:\...\MyWebFavourites\app\src\main\res\layout\activity_main.xml:20], original message=, tool name=Optional.of(AAPT)

From this answer, I have tried creating this code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mywebfavourites.MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Paid WP Maintenance"
        tools:layout_editor_absoluteX="25dp"
        tools:layout_editor_absoluteY="27dp" />

    <TextView
        textView =(TextView)findViewById(R.id.editText)
        textView.setClickable(true)
        textView.setMovementMethod(LinkMovementMethod.getInstance())
        String text = "<a href='https://www.example.com/'>Example</a>"
        textView.setText(Html.fromHtml(text)) />

Line 20 is textView =(TextView)findViewById(R.id.editText). I can't see what is not well-formed about this line.

Can you help please?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Steve
  • 2,066
  • 13
  • 60
  • 115

2 Answers2

1

You cannot do like that.

<TextView
    textView =(TextView)findViewById(R.id.editText)
    textView.setClickable(true)
    textView.setMovementMethod(LinkMovementMethod.getInstance())
    String text = "<a href='https://www.example.com/'>Example</a>"
    textView.setText(Html.fromHtml(text)) />

This part is totally fail. You cannot put java codes into xml layout file. You must remove this part.

Add this lines to your onCreate method in related Activity class like that:

But you need to learn basics of Java, Android and XML first. Without these, your number of questions will be increased.

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

    TextView textView =(TextView)findViewById(R.id.textView);
    textView.setClickable(true);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    String text = "<a href='http://www.google.com'> Google </a>";
    textView.setText(Html.fromHtml(text));
}
Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
  • Thanks. So I need to put that in the MainActivity.java. What do I wrap the code in? A constructor? Or inside of `onCreate` ? – Steve Apr 15 '18 at 14:10
  • Thanks Oğuzhan. I have added the code inside of `onCreate`. The word `TextView` is red, and I receive `error: cannot find symbol class TextView`. – Steve Apr 15 '18 at 14:17
  • Nevermind, I found I need to `import android.widget.TextView;`. – Steve Apr 15 '18 at 14:20
  • @Steve as I mentioned before, you need to learn basics and then you can start building an app. Read documentations, follow tutorials and google your errors, I am sure that 99% of your errors were asked and answered before. – Oğuzhan Döngül Apr 15 '18 at 14:20
0

None of these lines go inside the layout file.

They go in the Activity Java code. And you are using R.id.editText, so you need to set that as an EditText in your code to match the XML.

EditText  textView =(EditText)findViewById(R.id.editText);
textView.setClickable(true);

textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href=\"https://www.example.com/\">Example</a>";
textView.setText(Html.fromHtml(text));
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245