我尝试将此支持库导入到我的项目中但无法使其工作。
首先,我找不到它。当我点击Dependencies选项卡上的+按钮并搜索它时。我找到了android.support.v4,但我也需要v7。我在某处读到了这个库可能已被弃用。
所以在我的代码中它无法导入android.support.v7.widget.ShareActionProvider v7是Red,因为它无法解析。我按照了如何添加库的方向。但仍然无法解决问题。我做了正常的步骤,如干净,同步gradle文件,无效链接等...
以下是mainactivity的一些代码
import android.content.Intent
import android.os.Bundle
import android.support.v4.view.MenuItemCompat
import android.view.Menu
import android.view.MenuItem
//import androidx.appcompat.widget.ShareActionProvider
import androidx.appcompat.widget.Toolbar
//import androidx.core.view.MenuItemCompat
//import android.support.v4.view.MenuItemCompat
//import androidx.core.view.MenuItemCompat.getActionProvider
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.ShareActionProvider
//import android.widget.ShareActionProvider
class MainActivity : AppCompatActivity() {
private var shareActionProvider : ShareActionProvider? = null
import android.content.Intent
import android.os.Bundle
import android.support.v4.view.MenuItemCompat
import android.view.Menu
import android.view.MenuItem
//import androidx.appcompat.widget.ShareActionProvider
import androidx.appcompat.widget.Toolbar
//import androidx.core.view.MenuItemCompat
//import android.support.v4.view.MenuItemCompat
//import androidx.core.view.MenuItemCompat.getActionProvider
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.ShareActionProvider
//import android.widget.ShareActionProvider
class MainActivity : AppCompatActivity() {
private var shareActionProvider : ShareActionProvider? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
}
override fun onCreateOptionsMenu(menu : Menu): Boolean {
getMenuInflater().inflate(R.menu.menu_main, menu)
val menuItem = menu.findItem(R.id.action_share) as MenuItem
shareActionProvider = MenuItemCompat.getActionProvider(menuItem) as ShareActionProvider
setShareActionIntent("Want to join me for pizza?")
return super.onCreateOptionsMenu(menu)
}
private fun setShareActionIntent(text : String) {
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT, text)
shareActionProvider!!.setShareIntent(intent)
}
override fun onOptionsItemSelected(item : MenuItem) : Boolean {
when(item.itemId) {
R.id.action_create_order -> {
val intent = Intent(this, OrderActivity::class.java)
startActivity(intent)
return true
}
else -> return super.onOptionsItemSelected(item)
}
}
}
这是我的gradle.build文件
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.71'
repositories {
google()
jcenter()
maven { url '../pluginrepo' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-beta05'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这是我的app(build.gradle)文件
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.gandalf.bitsandpizza"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
// implementation 'com.android.support:support-core-utils:27.0.0'
implementation 'com.google.android:support-v4:r7'
implementation 'com.android.support:appcompat-v7:28.0.+'
// implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-core-utils:28.0.+'
}
这是基于答案中建议的重构。
问题现已解决。这是应用程序中更新的gradle.build文件
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.some.name"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
我正在使用这些依赖项
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
有进口
import android.support.v4.view.MenuItemCompat
import android.support.v7.widget.ShareActionProvider
而且没有红色。
尝试清理代码
正如@Mohsen所说,你也可以尝试这样做
所以在我的代码中它无法导入android.support.v7.widget.ShareActionProvider v7是Red,因为它无法解析
我希望这是您的代码中唯一的问题。你添加了:
implementation 'com.google.android:support-v4:r7'
implementation 'com.android.support:appcompat-v7:28.0.+' // Use specific version like 28.0.0 in future for this
这是指qazxsw poi和qazxsw poi qazxsw poi但你的qazxsw poi和测试+ v4
正在使用v7
依赖。这对我来说实际上很奇怪而且令人困惑!
要使用AppCompat
提供的Toolbar
,您需要确保您的应用程序首先迁移到ConstraintLayout
!
我相信这种情况正在发生,因为您的项目似乎已迁移到AndroidX
,这让IDE混淆使用ShareActionProvider
或AppCompat
。
简单地说,如果您正在尝试使用V7依赖项,请访问AndroidX
,选择AndroidX
。
经过很多头痛。我发现问题是我正在使用的Android Studio版本。我使用的是Beta版本,出于某种原因,这些库已经搞砸了。当我切换到稳定版本时,一切都开始正常工作。
1)只需按ALT + ENT并选择将App迁移到AppCompat。
2)ImageView重命名为AppCompatImageView。
3)按ALT + ENT最后导入班级。问题解决了 !
或者导入androidx.appcompat.widget.AppCompatImageView;
我在gradle.properties中使用它们
AppCompat