2012-10-02 55 views
0

我已完成The New Boston站點教導的電子郵件應用程序。據我回顧,一切都很好。我的應用運行得很好,但是當我按下"Send Email"Button時,應用崩潰了。我正在測試我的HTC Desire S上的應用程序。請幫我解決一下這個。來自教程的示例使用NullPointerException異常

我的清單文件:

<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) 
+0

第一課顯示LogCat錯誤或沒有人會幫助。 – JustinMorris

回答

0

其原因NullPointerException是,你沒有初始化的最後EditText的一個id爲etOutro。只需在initializeVars方法中添加此行:

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

非常感謝Luksprog ...:D – renupok92

相關問題