我正在为Android中的滑动标签创建演示。
添加所有Requried java和Xml文件后,App正在崩溃。
使用上方链接我创建了演示。
MainActivity.java
public class MainActivity extends AppCompatActivity {
// Declaring Your View and Variables
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Home","Events"};
int Numboftabs =2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
Logcat错误
07-08 13:49:32.710: E/AndroidRuntime(7625): FATAL EXCEPTION: main
07-08 13:49:32.710: E/AndroidRuntime(7625): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slidingtab/com.example.slidingtab.MainActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class com.android4devs.slidingtab.SlidingTabLayout
07-08 13:49:32.710: E/AndroidRuntime(7625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-08 13:49:32.710: E/AndroidRuntime(7625): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
07-08 13:49:32.710: E/AndroidRuntime(7625): at android.app.ActivityThread.access$600(ActivityThread.java:149)
07-08 13:49:32.710: E/AndroidRuntime(7625): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
帮我解决这个错误。
答案 0 :(得分:1)
对于第二个错误,请将其添加到style.xml
<style >
<item name="android:windowActionBar">false</item>
</style>
答案 1 :(得分:0)
我找到了问题的答案
在Styles.xml中添加此行
Styles.xml
<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
在清单文件中将主题样式更改为NoActionBarTheme
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.slidingtab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/NoActionBarTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Tab1"
android:label="@string/title_activity_tab1" >
</activity>
<activity
android:name=".Tab2"
android:label="@string/title_activity_tab2" >
</activity>
</application>
</manifest>