当我运行并按下布局上的按钮时,Android Studio Emulator会一直崩溃?

时间:2016-09-12 13:53:29

标签: java android android-layout android-studio android-optionsmenu

当我运行任何布局时,默认情况下应首先运行启动活动布局,让我登录,然后转到我点击的页面/布局。登录或注册后,模拟器崩溃说"不幸的是,app_name已停止工作。"

这是布局之一

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bignerdranch.android.assignment2.EmailReceiptActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" "
    android:id="@+id/textView3"/>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="27dp"
    android:text="@string/fragment_login_message"
    android:layout_gravity="left|top"
    android:id="@+id/fragment_login_message"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/username"
    android:layout_gravity="center_horizontal|top"
    android:layout_alignTop="@+id/fragment_login_message"
    android:layout_toRightOf="@+id/fragment_login_message"
    android:layout_toEndOf="@+id/fragment_login_message"
    android:id="@+id/textView2"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Send Receipt To:"
    android:id="@+id/send_receipt_to"
    android:layout_marginTop="40dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/email_send_receipt"
    android:layout_below="@+id/send_receipt_to"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="47dp"
    android:text="Email Address:"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send"
    android:id="@+id/send_receipt_button"
    android:layout_below="@+id/email_send_receipt"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="35dp"/>
</RelativeLayout>

这是该布局的Java类

package com.bignerdranch.android.assignment2;

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

public class EmailReceiptActivity extends AppCompatActivity {


Button btn_send;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_options_menu, menu);
    return  true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.logoff_menu_item) {
        super.onOptionsItemSelected(item);
        Toast.makeText(this, "Logging You Off...", Toast.LENGTH_LONG).show();
        startActivity(new Intent(EmailReceiptActivity.this,     LaunchActivity.class));
    }

    return true;
}

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

    btn_send = (Button)findViewById(R.id.send_receipt_button);
    btn_send.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            //Use the name of this class, and the name class where you want to     be taken when the button is clicked.
            Intent intent = new Intent(EmailReceiptActivity.this, SuccessActivity.class);
            startActivity(intent);
        }
    });

}
}

选项菜单     

<menu
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/logoff_menu_item"
        android:title="@string/logoff_menu_item">

    </item>
</menu>

启动活动布局

<?xml version="1.0" encoding="utf-8"?>

<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:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bignerdranch.android.assignment2.LaunchActivity">


<!-- Login progress -->
<ProgressBar
    android:id="@+id/login_progress"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:visibility="gone"/>

<ScrollView
    android:id="@+id/login_form"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/email_login_form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <AutoCompleteTextView
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/prompt_email"
            android:inputType="textEmailAddress"
            android:maxLines="1"
            android:singleLine="true"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/prompt_password"
            android:imeActionId="@+id/login"
            android:imeActionLabel="@string/action_sign_in_short"
            android:imeOptions="actionUnspecified"
            android:inputType="textPassword"
            android:maxLines="1"
            android:singleLine="true"/>

        </android.support.design.widget.TextInputLayout>

        <Button
            android:id="@+id/email_sign_in_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Sign In"
            android:textStyle="bold"/>

        <Button
            android:id="@+id/email_register_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Register"

            android:textStyle="bold"/>

    </LinearLayout>
</ScrollView>
</LinearLayout>

LogCat错误:

09-12 07:23:57.680 21430-21430/com.bignerdranch.android.assignment2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.bignerdranch.android.assignment2, PID: 21430
                                                                                      java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setIntent(android.content.Intent)' on a null object reference
                                                                                          at com.bignerdranch.android.assignment2.UserAccountActivity.onCreateOptionsMenu(UserAccountActivity.java:22)
                                                                                          at android.app.Activity.onCreatePanelMenu(Activity.java:2823)
                                                                                          at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340)
                                                                                          at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                                          at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1320)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1600)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:92)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:131)
                                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                          at android.os.Looper.loop(Looper.java:135)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-12 07:24:15.778 21645-21645/com.bignerdranch.android.assignment2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.bignerdranch.android.assignment2, PID: 21645
                                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.assignment2/com.bignerdranch.android.assignment2.NewSessionActivity}: 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:2325)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:135)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                                       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.bignerdranch.android.assignment2.NewSessionActivity.onCreate(NewSessionActivity.java:40)
                                                                                          at android.app.Activity.performCreate(Activity.java:5990)
                                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                          at android.os.Looper.loop(Looper.java:135) 
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                                          at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
09-12 07:30:02.938 24027-24027/com.bignerdranch.android.assignment2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.bignerdranch.android.assignment2, PID: 24027
                                                                                      java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setIntent(android.content.Intent)' on a null object reference
                                                                                          at com.bignerdranch.android.assignment2.UserAccountActivity.onCreateOptionsMenu(UserAccountActivity.java:22)
                                                                                          at android.app.Activity.onCreatePanelMenu(Activity.java:2823)
                                                                                          at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340)
                                                                                          at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                                          at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1320)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1600)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:92)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:131)
                                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                          at android.os.Looper.loop(Looper.java:135)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

0 个答案:

没有答案