教程中的示例与NullPointerException崩溃

时间:2012-10-02 15:41:07

标签: android nullpointerexception

我已完成The New Boston网站教授的电子邮件应用程序。据我所述,一切都很好。我的应用运行得很好,但就在我按"Send Email" Button时,应用程序崩溃了。我正在HTC Desire S上测试该应用。请帮我解决一下这个。

我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thenewboston.pok"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Splash"
        android:label="@string/title_activity_the_new_boston" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TheNewBostonActivity"
        android:label="Add and Sub 1" >
        <intent-filter>
            <action android:name="com.thenewboston.pok.THENEWBOSTONACTIVITY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Menu"
        android:label="Select An Application" >
        <intent-filter>
            <action android:name="com.thenewboston.pok.MENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TextPlay"
        android:label="Text Play!" >          
    </activity>
    <activity
        android:name=".Email"
        android:label="Email Application" >
        <intent-filter>
            <action android:name="com.thenewboston.pok.EMAIL" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>          
    </activity>
    <activity
        android:name=".Camera"
        android:label="Camera Application"
        android:screenOrientation="portrait" >          
    </activity>
    <activity
        android:name=".Data"
        android:label="Rate It!!"
        android:screenOrientation="portrait" >          
    </activity> 
    <activity
        android:name=".OpenedClass"
        android:label="Rate It!!"
        android:screenOrientation="portrait" >          
    </activity>            

</application>

Email活动:

public class Email extends Activity implements View.OnClickListener{

EditText personsEmail, intro, personsName, stupidThings, hatefulAction, outro;
String emailAdd, beginning, name, stupidAction, hateAct, end;
Button sendEmail;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.email);
    initializeVars();
    sendEmail.setOnClickListener(this);

}

private void initializeVars() {
    // TODO Auto-generated method stub
    personsEmail = (EditText) findViewById(R.id.etEmails);
    stupidThings = (EditText) findViewById(R.id.etThings);
    intro = (EditText) findViewById(R.id.etIntro);
    personsName= (EditText) findViewById(R.id.etName);
    hatefulAction= (EditText) findViewById(R.id.etAction);
    sendEmail= (Button) findViewById(R.id.bSentEmail);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    convertEditToString();
    String emailAddress[] = {"emailAdd"};
    String message = "Well hello "
            + name
            + " I just wanted to say "
            + beginning
            + ".  Not only that but I hate when you "
            + stupidAction
            + ", that just really makes me crazy.  I just want to make you "
            + hateAct
            + ".  Welp, thats all I wanted to chit-chatter about, oh and "
            + end
            + ".  Oh also if you get bored you should check out www.mybringback.com"
            + '\n' + "PS. I think I love you...  :-(  ";

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailAddress);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hello");
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    startActivity (emailIntent);

    }

    private void convertEditToString() {
        // TODO Auto-generated method stub
        emailAdd = personsEmail.getText().toString();
        beginning = intro.getText().toString();
        name = personsName.getText().toString();
        hateAct = hatefulAction.getText().toString();
        stupidAction = stupidThings.getText().toString();
        end = outro.getText().toString();
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}

我的 email.xml

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:weightSum="100" android:layout_height="match_parent">
<ScrollView android:layout_weight="30" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:orientation="vertical"
        android:layout_width="match_parent" android:layout_height="match_parent">
        <TextView android:text="Email address(es):"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        <EditText android:layout_height="wrap_content"
            android:layout_width="match_parent" android:id="@+id/etEmails">
        </EditText>
        <TextView android:text="Hateful Intro:"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        <EditText android:layout_height="wrap_content"
            android:layout_width="match_parent" android:id="@+id/etIntro"></EditText>
        <TextView android:text="Person's name" android:layout_width="wrap_content"
            android:layout_height="wrap_content"></TextView>
        <EditText android:layout_height="wrap_content"
            android:layout_width="match_parent" android:id="@+id/etName"></EditText>
        <TextView android:text="Stupid Things that this Person does"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        <EditText android:layout_height="wrap_content"
            android:layout_width="match_parent" android:id="@+id/etThings"></EditText>
        <TextView android:text="What you want to do to this person:"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        <EditText android:layout_height="wrap_content"
            android:layout_width="match_parent" android:id="@+id/etAction"></EditText>
        <TextView android:text="Hateful Outro" android:layout_width="wrap_content"
            android:layout_height="wrap_content"></TextView>
        <EditText android:layout_height="wrap_content"
            android:layout_width="match_parent" android:id="@+id/etOutro"></EditText>
    </LinearLayout>
</ScrollView>
<LinearLayout android:orientation="vertical"
    android:layout_width="match_parent" android:layout_weight="40"
    android:layout_height="fill_parent">
    <Button android:text="Send Email" android:id="@+id/bSentEmail"
        android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
</LinearLayout>
<LinearLayout android:orientation="vertical"
    android:layout_width="match_parent" android:layout_weight="30"
    android:layout_height="fill_parent">
    <AnalogClock android:id="@+id/analogClock1"
        android:layout_width="fill_parent" android:layout_height="fill_parent"></AnalogClock>
</LinearLayout>

LogCatNullPointerException

10-02 22:15:40.603: E/AndroidRuntime(28009): FATAL EXCEPTION: main
10-02 22:15:40.603: E/AndroidRuntime(28009): java.lang.NullPointerException
10-02 22:15:40.603: E/AndroidRuntime(28009):    at com.thenewboston.pok.Email.convertEditToString(Email.java:69)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at com.thenewboston.pok.Email.onClick(Email.java:38)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at android.view.View.performClick(View.java:3549)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at android.view.View$PerformClick.run(View.java:14393)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at android.os.Handler.handleCallback(Handler.java:605)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at android.os.Looper.loop(Looper.java:154)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at android.app.ActivityThread.main(ActivityThread.java:4945)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at java.lang.reflect.Method.invokeNative(Native Method)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at java.lang.reflect.Method.invoke(Method.java:511)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-02 22:15:40.603: E/AndroidRuntime(28009):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

NullPointerException的原因是您没有初始化标识为EditText的{​​{1}}的最后一个etOutro。只需在initializeVars方法中添加此行:

outro = (EditText) findViewById(R.id.etOutro);