Android应用程序在setSupportActionBar崩溃

时间:2015-09-07 20:21:19

标签: java android android-studio android-appcompat

我正在尝试按照以下教程创建标签视图布局:http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html

不幸的是,应用程序在出现任何问题之前都会崩溃。将调试日志插入我的代码后,我发现问题存在于MainActivity中的 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if indexPath.section == 3 { return 90 } else if indexPath.section == 1 { return 185 } else if indexPath.section == 2 { return 155 } else if indexPath.section == 4 { return 100 } return UITableViewAutomaticDimension } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { ... } else if indexPath.section == 3 { let cell = tableView.dequeueReusableCellWithIdentifier("weeklyCell", forIndexPath: indexPath) as! WxTableViewCell cell.selectionStyle = UITableViewCellSelectionStyle.None cell.forecastCollectionView.delegate = self cell.forecastCollectionView.dataSource = self cell.forecastCollectionView.reloadData() cell.forecastCollectionView.tag = indexPath.row cell.forecastCollectionView.showsHorizontalScrollIndicator = false return cell } else if indexPath.section == 4 { let cell = tableView.dequeueReusableCellWithIdentifier("dailyInformation", forIndexPath: indexPath) as! WxTableViewCell cell.hidden = true cell.contentView.hidden = true return cell } ... } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let index = NSIndexPath(forRow: 0, inSection: 3) let myindex = NSIndexPath(forRow: 0, inSection: 4) var myTable = self.tableView var lastCell = myTable.cellForRowAtIndexPath(index) var dailyCell = myTable.cellForRowAtIndexPath(myindex) var myView = UIView(frame: CGRectMake(20, 0, lastCell!.frame.width, lastCell!.frame.height)) dailyCell.frame.size = myView.frame.size } 行。我不知道为什么会这样。我在下面粘贴了我的整个MainActivity文件。

setSupportActionBar(toolbar)

这是activity_main.xml:

package com.example.rkhaj.tabstest;

import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;


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);

        Log.d("icecream", "position 1");


        // Creating The Toolbar and setting it as the Toolbar for the activity

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        Log.d("icecream", "position 1.5");
        setSupportActionBar(toolbar);

        Log.d("icecream", "position 2");


        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

        Log.d("icecream", "position 3");

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        Log.d("icecream", "position 4");

        // Assinging 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

        Log.d("icecream", "position 5");

        // 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);
            }
        });

        Log.d("icecream", "position 6");

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);

        Log.d("icecream", "position 7");



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是logcat:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />

    <com.example.rkhaj.tabstest.SlidingTabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="2dp"
        android:background="@color/ColorPrimary"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1"
        >
    </android.support.v4.view.ViewPager>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您需要为活动使用AppCompat主题。像这样,Theme.AppCompat.Light.NoActionBar

答案 1 :(得分:0)

在将代码从一个新项目复制到新项目时,此注释对我有帮助:

不要忘记将android:theme="@style/AppTheme.NoActionBar"添加到 您在AndroidManifest.xml中的活动声明。我知道这个 需要在那里但忘了添加它,我不知道为什么 我的应用在我的活动中的setupActionBar()处崩溃-此问题已修复 它。 –危险爪子

应该是一个独立的答案:-)