我正在学习本教程:使用标签的Android Material Design
相关信息:
Android API等级21
依赖性被添加到graddle文件编译'com.android.support:appcompat-v7:23.0.1'并编译'com.android.support:design:23.0.1'}
无法解析方法"findViewById".我试着按照这个答案无法在Fragment中解析方法'findViewById(int)',但是这个AppCompatActivity使用OnCreateBundle代替OnCreateView,那么我无法获取View对象...
这段代码有什么问题?我错过了一些进口?
package info.androidhive.materialtabs.activity; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import java.util.ArrayList; import java.util.List; import info.androidhive.materialtabs.R; import info.androidhive.materialtabs.fragments.OneFragment; import info.androidhive.materialtabs.fragments.ThreeFragment; import info.androidhive.materialtabs.fragments.TwoFragment; public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private TabLayout tabLayout; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); viewPager = (ViewPager) findViewById(R.id.viewpager); setupViewPager(viewPager); tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); } private void setupViewPager(ViewPager viewPager) { ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); adapter.addFragment(new OneFragment(), "ONE"); adapter.addFragment(new TwoFragment(), "TWO"); adapter.addFragment(new ThreeFragment(), "THREE"); viewPager.setAdapter(adapter); } class ViewPagerAdapter extends FragmentPagerAdapter { private final ListmFragmentList = new ArrayList<>(); private final List mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Override public CharSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } }
这是Android Studio显示的"错误",但我在logevent窗口中获得了0个错误
您似乎遇到了Android Studio中的损坏缓存错误.
在使用自定义命名空间添加自定义视图时,这种情况发生了很多.如果您添加了任何自定义视图,则可能是您的问题.
要修复:文件>使高速缓存/重新启动无效
这应该重新索引所有内容并允许自动完成工作,同样,findViewById()应该工作.我查看了代码,没有看到任何错误.