由于工具栏中的标题,Android应用程序崩溃了

时间:2018-03-06 15:43:58

标签: android debugging toolbar material

我的训练应用程序的主要活动中有工具栏标题的问题。我正在使用主要父活动(ChooseActivity)构建一个应用程序,我可以在其中按两个按钮将自己重新定位到其他两个子项(GroupsActivity和AddGroupActivity)。我想向每个人添加工具栏并使用它自己的标题。另外,我将UpButtons添加到每个子活动中。我在Android Manifest中使用标签param设置了标题。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hfad.singleton">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".ChooseActivity"
        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=".AddGroupActivity"
        android:label="@string/add_group"
        android:parentActivityName="com.hfad.singleton.ChooseActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.hfad.singleton.ChooseActivity"/>
    </activity>
    <activity android:name=".GroupsActivity"
        android:label="@string/groups"
        android:parentActivityName="com.hfad.singleton.ChooseActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.hfad.singleton.ChooseActivity"/>
    </activity>
</application>

它适用于我的孩子活动,但ChooseActivity不显示任何标题。当我试图通过添加像toolbar.setTitle或getSupportActionBar()。setTitle(“我的应用程序”)等方法来解决它时,我的应用程序被编译但从运行开始就崩溃了。我不知道如何处理它。下面我插入活动类的代码

ChooseActivity类:

public class ChooseActivity extends AppCompatActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose);
        //Creating of toolbar with title App Name
        Toolbar myToolbar = findViewById(R.id.toolbar);
        setSupportActionBar(myToolbar);
        //Adding butttons with listeneres
        Button addGroup = (Button) findViewById(R.id.add_group);
        Button groups = (Button) findViewById(R.id.groups);
        addGroup.setOnClickListener(this);
        groups.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_group:
                Intent intentAddGroup = new Intent(this, AddGroupActivity.class);
                startActivity(intentAddGroup);
                break;
            case R.id.groups:
                Intent intentGroups = new Intent(this, GroupsActivity.class);
                startActivity(intentGroups);
                break;
        }
    }
}

AddGroupActivity类:

public class AddGroupActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_group);
        //Creating of toolbar with title Add Group
        Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(myToolbar);
        //Enable Up Button
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
    }
}

GroupsActivity类:

public class GroupsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_groups);
        //Creating of toolbar with title Add Group
        Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(myToolbar);
        //Enable Up Button
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
    }
}

工具栏布局代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="@android:color/white"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

主要ChooseActivity的xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout       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"
android:orientation="vertical"
tools:context="com.hfad.singleton.ChooseActivity"
tools:layout_editor_absoluteY="25dp">

<include
    android:id="@+id/include"
    layout="@layout/layout_toolbar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/add_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:text="@string/add_group"
    android:textAppearance="@style/TextAppearance.AppCompat.Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/include" />

<Button
    android:id="@+id/groups"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="227dp"
    android:text="@string/groups"
    android:textAppearance="@style/TextAppearance.AppCompat.Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

0 个答案:

没有答案