I'm following this tutorial: Android Material Design Using Tabs
Infos:
- Android API Level 21
- Dependecies are added to graddle file compile 'com.android.support:appcompat-v7:23.0.1' and compile 'com.android.support:design:23.0.1' }
The method "findViewById" cannot be resolved. I tried to follow this answer Cannot resolve method 'findViewById(int)' in Fragment, but this AppCompatActivity use OnCreateBundle instead OnCreateView, then i can't get the View object...
What is wrong with this code ? I'm missing some import?
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 List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> 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);
}
}
}
This is the "Error" that Android Studio show but i got 0 erros in logevent window
You appear to be running into the corrupt cache bug in Android Studio.
This happens to me a LOT when adding custom views with a custom namespace. If you added any custom views, this could be your issue.
To fix: File > Invalidate Caches / Restart
That should reindex everything and allow for auto-completion to work, as well, your findViewById() should work. I reviewed the code and do not see any errors.
AppCompatActivity extends FragmentActivity which extends Activity which has findViewById() method.
So it shouldn't be a problem to get the method.
I believe it's IDE bug. Try to close your IDE and reopen the project. Worked for me, perfectly.
Update your build tools version to the latest 27.0.3, after trying all solutions this is what worked for me.
in app -> build.gradle
buildToolsVersion '27.0.3'
rebuild and hope it works for you too
来源:https://stackoverflow.com/questions/34078404/cannot-resolve-findviewbyid-in-appcompatactivity