【问题标题】:Error: not well-formed (invalid token)错误:格式不正确(令牌无效)
【发布时间】:2018-09-25 08:03:21
【问题描述】:

Java/XML 新手使用 Android Studio 3.1.1,我收到错误:

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)

来自this answer,我已尝试创建此代码:

<?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)) />

第 20 行是textView =(TextView)findViewById(R.id.editText)。我看不出这条线有什么不好的地方。

你能帮忙吗?

【问题讨论】:

  • 为什么xml里面有个findViewById??

标签: android xml


【解决方案1】:

你不能那样做。

<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)) />

这部分完全失败。您不能将 java 代码放入 xml 布局文件中。 您必须删除这部分。

将此行添加到相关 Activity 类中的 onCreate 方法中,如下所示:

但您需要先学习 Java、Android 和 XML 的基础知识。没有这些,您的问题数量将会增加。

@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));
}

【讨论】:

  • 谢谢。所以我需要把它放在 MainActivity.java 中。我用什么包装代码?构造函数?还是在onCreate 内部?
  • 感谢奥古占。我在onCreate 中添加了代码。 TextView这个词是红色的,我收到error: cannot find symbol class TextView
  • 没关系,我发现我需要import android.widget.TextView;
  • @Steve 正如我之前提到的,您需要学习基础知识,然后才能开始构建应用程序。阅读文档、遵循教程并在 Google 上搜索您的错误,我相信您的错误 99% 之前都已被询问和回答。
【解决方案2】:

这些行都不在布局文件中。

它们进入 Activity Java 代码。而且您使用的是R.id.editText,因此您需要在代码中将其设置为 EditText 以匹配 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));

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 2016-05-04
  • 2018-12-05
  • 1970-01-01
  • 2021-05-08
相关资源
最近更新 更多