java - 如何在 android studio 中导入 android.support.v7.app.Appcompatactivity?
问题描述
我是编码新手。我对 mainactivity.java 使用了这个 import android.support.v7.app.Appcompatactivity 但我收到了一个红色错误:
无法解析符号 v7
我也确实使缓存重新启动无效,但这不起作用。如果我在没有该行的情况下运行此代码,我的应用程序可以在 Android 5.1.1 上运行,但不能在 Galaxy j6 上运行 那行是做什么用的?
我的 android studio 版本是 3.5.0.21,我也可以在屏幕截图中显示我的错误以获取更多详细信息:
package com.example.bmi;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.support.v7.app.Appcompatactivity
public class MainActivity extends AppCompatActivity {
private EditText height;
private EditText weight;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
height = (EditText) findViewById(R.id.height);
weight = (EditText) findViewById(R.id.weight);
result = (TextView) findViewById(R.id.result);
}
public void calculateBMI(View v) {
String heightStr = height.getText().toString();
String weightStr = weight.getText().toString();
if (heightStr != null && !"".equals(heightStr)
&& weightStr != null && !"".equals(weightStr)) {
float heightValue = Float.parseFloat(heightStr) / 100;
float weightValue = Float.parseFloat(weightStr);
float bmi = weightValue / (heightValue * heightValue);
displayBMI(bmi);
}
}
private void displayBMI(float bmi) {
String bmiLabel = "";
if (Float.compare(bmi, 15f) <= 0) {
bmiLabel = getString(R.string.very_severely_underweight);
} else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
bmiLabel = getString(R.string.severely_underweight);
} else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
bmiLabel = getString(R.string.underweight);
} else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
bmiLabel = getString(R.string.normal);
} else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
bmiLabel = getString(R.string.overweight);
} else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
bmiLabel = getString(R.string.obese_class_i);
} else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
bmiLabel = getString(R.string.obese_class_ii);
} else {
bmiLabel = getString(R.string.obese_class_iii);
}
bmiLabel = bmi + "\n\n" + bmiLabel;
result.setText(bmiLabel);
}
}
我的 build.gradle 代码是
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.bmi"
minSdkVersion 15
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'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
//noinspection GradlePath
implementation files('SdkManager\\extras\\android\\support\\v7\\appcompat\\libs and select android-support-v7-appcompat')
}
解决方案
更换
导入 android.support.v7.app.Appcompatactivity;
和
导入androidx.appcompat.app.AppCompatActivity;
将工作,因为不再支持 v7。您不能同时使用它们。
推荐阅读
- python - 发出停止长时间运行的 Huey 任务的信号
- python - 比较多个列以获取两个熊猫数据框中不同的行
- eclipse - 面向(嵌入式 C/C++)开发人员的 Eclipse IDE 2020-12:经典深色主题深黑色背景和菜单中的文本
- html - 使用小窗口浏览器拉伸背景
- ios - 在 Timer.scheduledTimer 中更新多个 UI 元素不起作用
- algorithm - 两个数字和问题的真实时间复杂度是多少?
- javascript - 页面中的 HTML 按钮
- javascript - 将值从 index.php 发送到顶点图表 .js 文件
- php - 使用按钮更新表中的值
- python - Pandas:在过滤的数据框中选择行范围和列