我正在学习如何使用 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>
这是我的错误日志显示的内容: