基本上我有一个带有两个屏幕的应用程序。屏幕一和屏幕二。在第一个屏幕中,我的意思是在MainActivity视图中绑定运行良好。但点击一个特定的按钮,它会重定向到第二个屏幕。现在问题来了。错误消息是-
error: cannot find symbol import com.example.demoapp.databinding.DragAndDropBinding;
但在第一个屏幕上运行良好。
主活动[FirstScreen]
//Working Fine
package com.example.demoapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.example.demoapp.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.dragOnly.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), DragActivity.class);
v.getContext().startActivity(intent);
}
});
}
}
ActivityMain.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:layout_centerVertical="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dragAndDrop"
android:text="Drag and Drop"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drag"
android:id="@+id/dragOnly"/>
</LinearLayout>
</RelativeLayout>
和DragAndDropActivity[第二屏幕]
//Showing error for import com.example.demoapp.databinding.DragAndDropBinding;
package com.example.demoapp;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.demoapp.databinding.ActivityMainBinding;
import com.example.demoapp.databinding.DragAndDropBinding; //Error
public class DragActivity extends AppCompatActivity {
TextView textView;
DragAndDropBinding binding;
float xPosition, yPosition, xPrevious, yPrevious;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drag_and_drop);
textView = findViewById(R.id.dragButton);
textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int point = event.getActionMasked();
switch (point) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
xPrevious = event.getX();
yPrevious = event.getY();
return true;
case MotionEvent.ACTION_MOVE:
float dx = event.getX() - xPrevious;
float dy = event.getY() - yPrevious;
xPosition += dx;
yPosition += dy;
v.setX(xPosition - v.getWidth());
v.setY(yPosition - v.getHeight());
return true;
default:
return false;
}
}
});
}
}
DragAndDrop.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drag Me any Where"
android:textSize="25sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/dragButton"
android:background="#056"
android:padding="8dp"
android:textColor="#FFF"/>
</RelativeLayout>
build.gradle[App]
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.demoapp"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
viewBinding {
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
安卓工作室3.6.1版和渐变插件classpath 'com.android.tools.build:gradle:3.6.1'
我可以看到您在第二个活动中实际上没有使用ViewBinding。您只是在导入它生成的类Proguard在运行时混淆了未使用的类,它认为您没有使用生成的视图绑定类(没错,您只是在导入它。(所以请像在第一个活动中使用的那样使用它。它会起作用的。