2

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

"Error"

Community
  • 1
  • 1
Rieth
  • 305
  • 1
  • 4
  • 13

3 Answers3

11

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.

AutoM8R
  • 3,020
  • 3
  • 32
  • 52
  • It didn't work... My app is running at emulator, but not rendering on "Preview" window – Rieth Dec 04 '15 at 02:15
  • this worked just fine, I had imported a project from and older android studio and this fixed it – Manny265 Mar 06 '16 at 08:36
  • This solution worked for me. Looking at the dates of the last comments, this issue has been around for a long time. I am using android studio 3.2.1 – Tom Rutchik Dec 07 '18 at 17:19
1

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.

Igor Fridman
  • 1,267
  • 1
  • 16
  • 30
0

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

Jamal S
  • 1,649
  • 1
  • 19
  • 24