I am writing an app that sends an e-mail with multiple image as attachments. I followed this link : Android multiple email attachments using Intent to set up the Email intent and stuff .But the app force closes when I click on the send button and there seeems to be a Null Pointer Exception.
Here's the code for the two classes that are involved (only for the email part) :
PrevEmail.java :
public class PrevEmail extends Activity{
Context context;
TextView tv,tvEmail,tvEmailAd,tvSubject,tvContent,tvLocation;
EditText etSubject,etContent,etLocation;
String subject,content,location;
Button send;
String btnSelected;
String[] imgPath;
Bundle extras;
ArrayList<Uri> uris;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.prevemail);
intializeVar();
}
private void intializeVar() {
// Links xml to java
tv = (TextView) findViewById(R.id.tv);
tvEmail = (TextView) findViewById(R.id.tvEmail);
tvEmailAd = (TextView) findViewById(R.id.tvEmailAd);
tvSubject = (TextView) findViewById(R.id.tvSubject);
tvContent = (TextView) findViewById(R.id.tvContent);
tvLocation = (TextView) findViewById(R.id.tvLocation);
etSubject = (EditText) findViewById(R.id.Subject);
etContent = (EditText) findViewById(R.id.Content);
etLocation= (EditText) findViewById(R.id.Location);
send = (Button) findViewById(R.id.bSend);
extras = getIntent().getExtras();
if(extras!=null){
btnSelected=extras.getString("Button");
imgPath=extras.getStringArray("Path");
//Sets the E-mail field according to the value received by the Bundle.
if(btnSelected.equalsIgnoreCase("case1")){
tvEmailAd.setText("email_address");
}else if(btnSelected.equalsIgnoreCase("case2")){
tvEmailAd.setText("email_address");
}else if(btnSelected.equalsIgnoreCase("case3")){
tvEmailAd.setText("email_address");
}else if(btnSelected.equalsIgnoreCase("case4")){
tvEmailAd.setText("email_address");
}
}
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
convertEditTextToString();
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,"email_address");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,content);
uris = new ArrayList<Uri>();
for(String file : imgPath) {
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
});
}
private void convertEditTextToString(){
subject = etSubject.getText().toString();
content = etContent.getText().toString();
location= etLocation.getText().toString();
}
}
Android manifest :
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.name.StartScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.name.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.name.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.name.PrevEmail"
android:label="@string/app_name">
</activity>
</application>
And the Logcat :
12-02 21:44:48.180: W/dalvikvm(29329): threadid=1: thread exiting with uncaught exception (group=0x40bc4498)
12-02 21:44:48.180: E/test(29329): Exception
12-02 21:44:48.200: E/AndroidRuntime(29329): FATAL EXCEPTION: main
12-02 21:44:48.200: E/AndroidRuntime(29329): java.lang.NullPointerException
12-02 21:44:48.200: E/AndroidRuntime(29329): at com.example.name.PrevEmail$1.onClick(PrevEmail.java:100)
12-02 21:44:48.200: E/AndroidRuntime(29329): at android.view.View.performClick(View.java:4106)
12-02 21:44:48.200: E/AndroidRuntime(29329): at android.view.View$PerformClick.run(View.java:17150)
12-02 21:44:48.200: E/AndroidRuntime(29329): at android.os.Handler.handleCallback(Handler.java:615)
12-02 21:44:48.200: E/AndroidRuntime(29329): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 21:44:48.200: E/AndroidRuntime(29329): at android.os.Looper.loop(Looper.java:137)
12-02 21:44:48.200: E/AndroidRuntime(29329): at android.app.ActivityThread.main(ActivityThread.java:4792)
12-02 21:44:48.200: E/AndroidRuntime(29329): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 21:44:48.200: E/AndroidRuntime(29329): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 21:44:48.200: E/AndroidRuntime(29329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
12-02 21:44:48.200: E/AndroidRuntime(29329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
12-02 21:44:48.200: E/AndroidRuntime(29329): at dalvik.system.NativeStart.main(Native Method)