编程语言
首页 > 编程语言> > java – 无法在滑动菜单片段中解析符号’TabLayout’

java – 无法在滑动菜单片段中解析符号’TabLayout’

作者:互联网

我是android编程的新手,我正在尝试制作一个订购应用程序.我已成功创建了滑动菜单及其片段和两个不同项目中的选项卡布局.现在这就是困境,当我在片段活动中添加选项卡布局的代码时,会发生错误.它表示不使用选项卡布局导入或无法解析符号’TabLayout’.

这是我片段的布局文件:

slider_fragment2.xml

<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="#E06908"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

</RelativeLayout>

这是片段java文件:
Slider_Fragment2.java

package com.example.aldoreymedrano.slider.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.support.design.widget.TabLayout;
import com.example.aldoreymedrano.slider.R;
import com.example.aldoreymedrano.slider.menu_tab_adapter.PagerAdapter;

public class Slider_Fragment2 extends AppCompatActivity


public Slider_Fragment2() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.slider_fragment2, container, false);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 4"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new   TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()   {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });


    return rootView;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
    }
}

我试过清理和重建项目,但它不起作用.我知道有些不对劲,但我没注意到.请帮助我,我已经坚持了好几个小时了.先感谢您.

这是错误消息

错误:(137,28)错误:不兼容的类型
必需:片段
发现:Slider_Fragment2

错误:(14,37)错误:包android.support.design.widget不存在

错误:(38,9)错误:找不到符号类TabLayout

错误:(38,32)错误:找不到符号类TabLayout

错误:(43,43)错误:找不到符号变量TabLayout

错误:(49,56)错误:包TabLayout不存在

错误:(50,57)错误:包TabLayout不存在

错误:(31,5)错误:方法不会覆盖或实现超类型的方法

错误:任务’:app:compileDebugJavaWithJavac’的执行失败.

Compilation failed; see the compiler error output for details.
Information:BUILD FAILED

解决方法:

打开你的build.gradle文件,你会发现依赖性部分为你的项目添加设计库

dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile 'com.android.support:appcompat-v7:22.2.0'
        compile 'com.android.support:support-v4:22.2.0'
        compile 'com.android.support:design:22.2.0'

    }

标签:java,android,android-fragments,android-tablayout,slidingmenu
来源: https://codeday.me/bug/20190528/1168107.html