我想为我的聊天编程片段,但我总是收到相同的错误消息。 在互联网上,我读到它应该与"导入"和"导入android.support.v4.app.Fragment"有关,但这对我不起作用。
在build.gradle(应用程序(中添加了数据包不起作用..
我的Android Studio版本是3.3 Canary 5。
我希望你能解决我的问题,非常感谢!
选项卡寻呼适配器
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import android.support.v4.app.Fragment;
class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
RequestsFragment requestsFragment = new RequestsFragment();
return requestsFragment;
case 1:
ChatsFragment chatsFragment = new ChatsFragment();
return chatsFragment;
case 2:
FriendsFragment friendsFragment = new FriendsFragment();
return friendsFragment;
default:
return null;
}
}
@Override
public int getCount()
{
return 3;
}
public CharSequence getPageTitle(int position)
{
switch (position)
{
case 0:
return "Requests";
case 1:
return "Chats";
case 2:
return "Friends";
default:
return null;
}
}
}
build.gradle(app(
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "28.0.2"
defaultConfig {
applicationId "com.nexsis.entwicklung.swapv12"
minSdkVersion 15
targetSdkVersion 28
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 'com.android.support:support-v4:28'
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
implementation 'com.android.support:recyclerview-v7:28.0.0-alpha1'
implementation 'com.google.firebase:firebase-auth:16.0.2'
implementation 'com.google.firebase:firebase-core:16.0.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28'
implementation 'com.android.support:design:28'
implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha04'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
apply plugin: 'com.google.gms.google-services'
您的导入似乎发生了冲突...
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
// this one is the old version of androidx.fragment.app.Fragment
// import android.support.v4.app.Fragment;
正如其他答案所表明的那样,降级似乎不是解决方案。 请参阅工件映射,其中解释了如何偶然build.gradle
;例如,com.android.support:support-fragment
需要用androidx.fragment:fragment
替换,以便知道导入。
尝试对Fragment、FragmentManager、FragmentPagerAdapter 使用相同的版本
即
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.Fragment;
使用 getFragmentMangager(( 时,请确保 Fragment 类扩展 android.app.Fragment 类。 如果您使用的是android.support.v4.app.Fragment(请参阅您的导入(,那么您需要使用getSupportFragmentManager((
在 build.gradle 中添加此内容
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.0'
并且您的导入应该是
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
而不是
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;