我正在学习如何使用Android Studio,而我正在处理一个表单应用程序,就像您在网站上注册为用户一样。您添加用户名,全名,电子邮件地址,然后使用DatePicker选择您的出生日期。然后单击“提交”按钮,然后导航到“谢谢”用户名“页面。
一切正常,直到我尝试添加“向上”导航按钮将返回到上一个表单,从而从头开始使用空白表单。现在,当我尝试提交表单时,它会突然崩溃,并出现“SimpleRegistrationForm已停止”错误。我以为我已逐步遵循了这些步骤,但是我错过了什么吗? ,但由于我的经验不足,我不确定我没有实现什么。任何协助都将受到赞赏和赞成。
PS:
(This article may be similar to: https://stackoverflow.com/questions/43403402/android-up-button-isnt-working)
(I've consulted: https://developer.android.com/training/appbar/up-action.html)
此处我做了哪些更改(以下更改以粗体或如果不是粗体,则标记为“**”以包围文本:
我的activity_display.message.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:id="@+id/activity_display_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayMessageActivity">
**<android.support.v7.widget.Toolbar
android:id="@+id/my_child_toolbar"
android:layout_width="match_parent"
android:layout_height="29dp"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />**
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="@string/user_Name"
android:textColor="@color/colorPrimary"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/thank_you"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="2dp"
android:contentDescription="@string/thank_you_for_signing_up"
android:text="@string/thank_you_for_signing_up"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_child_toolbar"
tools:text="@string/thank_you_for_signing_up" />
<!--<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="finishActivity"
android:layout_marginBottom="391dp"
android:layout_marginTop="164dp"
android:text="Back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />-->
</android.support.constraint.ConstraintLayout>
我的DisplayMessageActivty.java文件(代码的某些部分已注释掉):
package com.example.simpleregistrationform.feature;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;
import android.support.v7.app.ActionBar;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
**// my_child_toolbar is defined in the layout file
Toolbar myChildToolbar = findViewById(R.id.my_child_toolbar);
setSupportActionBar(myChildToolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);**
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
/*public void finishActivity(View v){
finish();
}*/
}
我的AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simpleregistrationform.app">
**<application>
<!-- The main/home activity (it has no parent activity) -->
<activity android:name="com.example.simpleregistrationform.feature.MainActivity">
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.simpleregistrationform.feature.DisplayMessageActivity"
android:label="@string/my_child_toolbar"
android:parentActivityName="com.example.simpleregistrationform.feature.MainActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.simpleregistrationform.feature.MainActivity" />
</activity>
</application>**
</manifest>
这是我的错误日志显示的内容:
答案 0 :(得分:0)
您已使用此行ActionBar
设置setSupportActionBar(myChildToolbar);
为什么第二次添加ActionBar ab = getSupportActionBar();
。
只需删除第二行!要添加其他属性,只需编写getSupportActionBar();
而不是ab