基本上我有一个带有两个屏幕的应用程序。屏幕一和屏幕二。在第一个屏幕中,我的意思是在 MainActivity viewBinding 工作正常。但是在特定按钮上单击它将重定向到第二个屏幕。现在有问题了。错误按摩是 -
error: cannot find symbol import com.example.demoapp.databinding.DragAndDropBinding;
但在第一个屏幕上它工作正常。
MainActivity[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;
}
}
});
}
}
拖放.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[应用程序]
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'
}
Android Studio 版本 3.6.1 和 gradle 插件classpath 'com.android.tools.build:gradle:3.6.1'