首页 > 解决方案 > 找不到符号类 ImplementAndroidArchitecture

问题描述

我已经实现了这些代码。

activity_quotes.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

        <variable
            name="quote"
            type="e.com.learningprojects.ImplementAndroidArchitecture.QuotesModel"/

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ImplementAndroidArchitecture.QuotesActivity">

        <TextView
            android:id="@+id/quotes"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="137dp"
            android:layout_marginEnd="16dp"
            android:text="@{quote.quotes"
            android:textColor="@color/black"
            android:textSize="25sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="24dp"
            android:text="@{quote.author}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/quotes" />

        <Button
            android:id="@+id/next_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="32dp"
            android:text="@string/next"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/author" />

        <Button
            android:id="@+id/previous_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:text="@string/previous"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/next_btn" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

QuoteActivity.java

package e.com.learningprojects.ImplementAndroidArchitecture;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import e.com.learningprojects.R;
import e.com.learningprojects.databinding.ActivityQuotesBinding;

public class QuotesActivity extends AppCompatActivity {

    MainViewModel mainViewModel;
    TextView text, author;
    Button nextBtn, previousBtn;
    ActivityQuotesBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_quotes);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_quotes);
        mainViewModel = new ViewModelProvider(this, new ViewModelFactory(this)).get(MainViewModel.class);

        text = findViewById(R.id.quotes);
        author = findViewById(R.id.author);
        nextBtn = findViewById(R.id.next_btn);
        previousBtn = findViewById(R.id.previous_btn);
        mainViewModel.fetchQuotes();
        // setQuotes(mainViewModel.fetchQuotes());
        nextBtn.setOnClickListener(v -> setQuotes(mainViewModel.nextQuotes()));
        previousBtn.setOnClickListener(v -> setQuotes(mainViewModel.previousQuotes()));

    }

    public void setQuotes(QuotesModel quotes) {
//        text.setText(quotes.quotes);
//        author.setText(quotes.author);

//       binding.quotes.setText(quotes.quotes);
//       binding.author.setText(quotes.author);
        
        binding.setQuote(quotes);

    }
}

QuoteModel.java

package e.com.learningprojects.ImplementAndroidArchitecture;

public class QuotesModel {
    public  String quotes, author;


    public QuotesModel(String quotes, String author) {
        this.quotes = quotes;
        this.author = author;
    }

    public String getQuotes() {
        return quotes;
    }

    public String getAuthor() {
        return author;
    }
}

我正在实现数据绑定。我已经实现了数据绑定的所有工作,并从布局初始化了所有视图,但这仍然给出了这个错误: cannot find symbol class ImplementAndroidArchitecture

错误

cannot find symbol class ImplementAndroidArchitecture

import e.com.learningprojects.ImplementAndroidArchitecture;
                             ^
  symbol:   class ImplementAndroidArchitecture
  location: package e.com.learningpro`enter code here`jects

package ImplementAndroidArchitecture does not exist

 protected ImplementAndroidArchitecture.QuotesModel mQuote;
                                 

标签: javaandroidpackageandroid-databinding

解决方案


我可以指出您的代码的两个问题。

  1. 您需要用“}”完成下面的赋值表达式

    android:text="@{quote.quotes"

  2. 将包名称“ImplementAndroidArchitecture”更改为小写。看这里https://stackoverflow.com/a/12534322/5980764以获得进一步的解释。


推荐阅读