单击按钮更改活动时应用崩溃

时间:2018-08-24 07:53:17

标签: java android xml crash

很抱歉,如果我不太准确,但这是我的第一个应用程序。

问题是:当我按下按钮更改活动应用崩溃时。

有人可以帮助我吗? 谢谢大家!

如果您想了解更多信息,请告诉我,我会尽力解释。


代码:

主要活动

    package com.example.principale.designazioni;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Button invia = (Button) findViewById(R.id.buttInvia);
            invia.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivityPage1();
                }
            });


        }

        public void openActivityPage1() {
            Intent intent = new Intent (getApplicationContext(), Page1.class);
            startActivity(intent);
        }
    }

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"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="73dp">

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="231dp"
        android:layout_height="229dp"
        android:layout_marginEnd="8dp"
        android:adjustViewBounds="false"
        android:contentDescription='@string tools:ignore="ContentDescription"'
        android:cropToPadding="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.146"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.04"
        app:srcCompat="@mipmap/logo_grigio" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="272dp"
        android:layout_height="51dp"
        android:fontFamily="monospace"
        android:text="Disponibilità"
        android:textAlignment="center"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.818"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView4"
        app:layout_constraintVertical_bias="0.093" />

    <Button
        android:id="@+id/buttInvia"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="monospace"
        android:text="Invia"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="36sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="0.482" />
</android.support.constraint.ConstraintLayout>

活动第1页

package com.example.principale.designazioni;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Page1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page1);

        EditText mail = (EditText) findViewById(R.id.editTextMail);
        EditText nome = (EditText) findViewById(R.id.editTextNome);
        EditText cognome = (EditText) findViewById(R.id.editTextCognome);
        EditText tessera = (EditText) findViewById(R.id.editTextTessera);

        Button avanti = (Button) findViewById(R.id.buttInvia);

        Bundle bundle = new Bundle();
        bundle.putString("mail", mail.getText().toString());
        bundle.putString("nome", nome.getText().toString());
        bundle.putString("cognome", cognome.getText().toString());
        bundle.putString("tessera", tessera.getText().toString());

        avanti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivityPage2();
            }
        });
    }

    public void openActivityPage2() {
        Intent intent = new Intent (getApplicationContext(), Page2.class);
        startActivity(intent);
    }
}

XML活动页面1

<?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"
    tools:context=".Page1">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="71dp"
        android:layout_height="27dp"
        android:fontFamily="monospace"
        android:text="E-Mail"
        android:textAlignment="textStart"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.093"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.06" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="96dp"
        android:layout_height="27dp"
        android:fontFamily="monospace"
        android:text="Cognome"
        android:textAlignment="textStart"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.102"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.44" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="71dp"
        android:layout_height="27dp"
        android:fontFamily="monospace"
        android:text="Numero di Tessera"
        android:textAlignment="textStart"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.093"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.629" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="71dp"
        android:layout_height="27dp"
        android:fontFamily="monospace"
        android:text="Nome"
        android:textAlignment="textStart"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.093"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.251" />

    <EditText
        android:id="@+id/editTextMail"
        android:layout_width="306dp"
        android:layout_height="50dp"
        android:ems="10"
        android:fontFamily="monospace"
        android:inputType="textEmailAddress"
        android:textColor="@android:color/background_light"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3"
        app:layout_constraintVertical_bias="0.0" />

    <EditText
        android:id="@+id/editTextCognome"
        android:layout_width="306dp"
        android:layout_height="50dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextNome"
        app:layout_constraintVertical_bias="0.17" />

    <EditText
        android:id="@+id/editTextNome"
        android:layout_width="306dp"
        android:layout_height="50dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/textView4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView5"
        app:layout_constraintVertical_bias="0.0" />

    <EditText
        android:id="@+id/editTextTessera"
        android:layout_width="306dp"
        android:layout_height="50dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView6"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/button"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="monospace"
        android:text="Avanti"
        android:textAlignment="center"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="36sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTessera"
        app:layout_constraintVertical_bias="0.487" />
</android.support.constraint.ConstraintLayout>

活动第2页

package com.example.principale.designazioni;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Page2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);

        final TextView nome = (TextView) findViewById(R.id.textViewPage2);
        Bundle bundle = this.getIntent().getExtras();
        nome.setText(bundle.getString("nome"));
    }
}

XML活动页面2

<?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"
    tools:context=".Page2">

    <TextView
        android:id="@+id/textViewPage2"
        android:layout_width="180dp"
        android:layout_height="47dp"
        android:fontFamily="monospace"
        android:text="TextView"
        android:textAlignment="center"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Android清单

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

    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Page1" />
        <activity android:name=".Page2"></activity>
    </application>

</manifest>

堆栈跟踪

08-24 10:21:07.370 6819-6819/com.example.principale.designazioni E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.principale.designazioni, PID: 6819
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.principale.designazioni/com.example.principale.designazioni.Page1}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7425)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.principale.designazioni.Page1.onCreate(Page1.java:30)
        at android.app.Activity.performCreate(Activity.java:7372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302) 
        at android.app.ActivityThread.-wrap12(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 
        at android.os.Handler.dispatchMessage(Handler.java:108) 
        at android.os.Looper.loop(Looper.java:166) 
        at android.app.ActivityThread.main(ActivityThread.java:7425) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 

1 个答案:

答案 0 :(得分:2)

Page1.java

中更改此行 来自

Button avanti = (Button) findViewById(R.id.buttInvia);

Button avanti = (Button) findViewById(R.id.button);