我编写了一个代码来实现带有小部件的可滚动选项卡但不幸的是,当我启动应用程序时我遇到了问题: Android工作室指向这一行:
setSupportActionBar(栏);
如果有人能帮助我的话。
这是我的活动代码:
import android.content.Intent;
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 android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class HomePage extends AppCompatActivity {
Intent intent;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
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);
Bundle extras =getIntent().getExtras();
if(extras!=null)
Toast.makeText(getApplicationContext(), extras.getString("username").toString(), Toast.LENGTH_LONG).show();;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
adapter.addFrag(new FourFragment(), "FOUR");
adapter.addFrag(new FiveFragment(), "FIVE");
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 addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
这是布局xml文件:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
这是Logcat:
3-26 07:01:40.136 5424-5424/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4d49b20)
03-26 07:01:40.136 5424-5424/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.subhi.hccc, PID: 5424
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.subhi.hccc/com.subhi.hccc.HomePage}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
at com.subhi.hccc.HomePage.onCreate(HomePage.java:32)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
03-26 07:01:40.160 523-816/? W/ActivityManager: Force finishing activity com.subhi.hccc/.HomePage
03-26 07:01:40.552 523-816/? D/dalvikvm: GC_FOR_ALLOC freed 1011K, 27% free 8922K/12140K, paused 8ms, total 8ms
03-26 07:01:40.576 523-816/? D/dalvikvm: GC_FOR_ALLOC freed 392K, 27% free 8902K/12140K, paused 12ms, total 13ms
03-26 07:01:40.576 523-816/? W/ActivityManager: Force finishing activity info.androidhive.materialtabs/.activity.ScrollableTabsActivity
03-26 07:01:40.616 523-538/? D/dalvikvm: GC_FOR_ALLOC freed 12K, 23% free 9384K/12140K, paused 7ms, total 7ms
03-26 07:01:40.616 523-538/? I/dalvikvm-heap: Grow heap (frag case) to 10.295MB for 1127532-byte allocation
03-26 07:01:40.636 523-538/? D/dalvikvm: GC_FOR_ALLOC freed <1K, 21% free 10484K/13244K, paused 7ms, total 7ms
03-26 07:01:40.652 523-567/? D/MobileDataStateTracker: default: setPolicyDataEnable(enabled=true)
03-26 07:01:40.900 150-150/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
03-26 07:01:41.076 523-538/? W/ActivityManager: Activity pause timeout for ActivityRecord{52934b38 u0 com.subhi.hccc/.HomePage t62 f}
03-26 07:01:41.176 4763-4763/? W/EGL_genymotion: eglSurfaceAttrib not implemented
03-26 07:01:41.936 523-567/? D/MobileDataStateTracker: default: setPolicyDataEnable(enabled=true)
03-26 07:01:42.072 523-567/? D/MobileDataStateTracker: default: setPolicyDataEnable(enabled=true)
03-26 07:01:43.448 5424-5424/? I/Process: Sending signal. PID: 5424 SIG: 9
03-26 07:01:43.452 523-567/? D/MobileDataStateTracker: default: setPolicyDataEnable(enabled=true)
03-26 07:01:43.452 153-503/? W/genymotion_audio: out_write() limiting sleep time 46802 to 39909
03-26 07:01:43.480 523-753/? I/ActivityManager: Process com.subhi.hccc (pid 5424) has died.
答案 0 :(得分:0)
确保清单上的主要主题样式不会扩展其中一个DarkActionBar样式。 请确保它从Theme.AppCompat.Light扩展。 NoActionBar (例如)。否则你在已经有工具栏的应用程序上添加一个工具栏。
并且会引发您在logcat上看到的错误。
快乐的编码:)
更新
这就是你的清单上的样子
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/**AppTheme**">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.**NoActionBar**">
</activity>
</application>