我是一个学习中的android工作室,正在尝试使用片段制作一个简单的应用程序,但出现以下错误。
error- Cannot resolve method 'setupWithViewPager(androidx.viewPager.widget.ViewPager)'
我的代码:
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.widget.TableLayout;
public class MainActivity extends AppCompatActivity {
private TableLayout tableLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tableLayout= (TableLayout) findViewById(R.id.tab_layout);
viewPager=(ViewPager) findViewById(R.id.pager_id);
adapter=new ViewPagerAdapter(getSupportFragmentManager());
adapter.AddFragement(new Fragment_call(),"Call");
adapter.AddFragement(new FragmentContact(),"Contact");
adapter.AddFragement(new FragmentFav(),"Favourite");
viewPager.setAdapter(adapter);
tableLayout.setupWithViewPager(viewPager);
}
}
我的gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.ass"
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'
//compile 'com.android.support:design:25.3.1'
}
任何帮助将不胜感激。
答案 0 :(得分:1)
tableLayout= (TableLayout) findViewById(R.id.tab_layout);
tableLayout.setupWithViewPager(viewPager);
android.widget.TableLayout
没有方法setupWithViewPager
。
也许您正在寻找com.google.android.material.tabs.TabLayout
。
它需要材料成分库作为依赖项:
implementation 'com.google.android.material:material:1.1.0-beta02'
答案 1 :(得分:1)
您不是说TabLayout而是TableLayout吗?
如果是这样,请将tableLayout = (TableLayout) findViewById(R.id.tab_layout);
更改为TabLayout tableLayout = findViewById(R.id.tab_layout);
答案 2 :(得分:0)
它是TabLayout,而不是TableLayout。这是两件事。