Home > DeveloperSection > Forums > Sending Message Error
DefineMac Mac

Total Post:6

Points:42
Posted on    June-21-2015 10:47 PM

 Android Android 
Ratings:


 1 Reply(s)
 644  View(s)
Rate this:
help. when i click i click my 1st button (b1) i get an error on asynctask i dont know how to use async

package com.example.myfirstproject;





import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class Kupon extends Activity {
ImageButton b1,b2,b3,b4,b5,b6,b7,b8,btn;
int[] checker = {11,11,11,11,11,11,11,11,11};
GMailSender sender= new GMailSender("argenteedmarkr@gmail.com", "argente234");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kupon);
b1 = (ImageButton) findViewById(R.id.imageButton1);
b2 = (ImageButton) findViewById(R.id.imageButton2);
b3= (ImageButton) findViewById(R.id.imageButton3);
b4= (ImageButton) findViewById(R.id.imageButton4);
b5= (ImageButton) findViewById(R.id.imageButton5);
b6= (ImageButton) findViewById(R.id.imageButton6);
b7= (ImageButton) findViewById(R.id.imageButton7);
b8= (ImageButton) findViewById(R.id.imageButton8);
btn= (ImageButton) findViewById(R.id.btnUser);
b1(); b2(); b3(); b4(); b5(); b6(); b7(); b8();
btn.setOnClickListener(new OnClickListener(){
public void onClick(View vx){
lipat();
}
});
}
public void lipat(){
// lipat2();
Intent intent = new Intent(this, Profile.class);
intent.putExtra("xny", checker);
//  intent.putExtra("SUCFAI", res);
  startActivity(intent);  
}
public void lipat2(){
//Toast.makeText(getApplicationContext(), "cmula"  , Toast.LENGTH_SHORT).show();
//Bundle b=new Bundle();
// b.putStringArray("xny", checker);
//Intent i=new Intent(this, Profile.class);
// Toast.makeText(getApplicationContext(), "lagpas"  , Toast.LENGTH_SHORT).show();
//i.putExtras(b);
// startActivity(i);
}
public void b1(){
b1.setOnClickListener(new OnClickListener(){
ViewGroup layout = (ViewGroup) b1.getParent();
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(Kupon.this);
        builder.setMessage("1-pc. Chickenjoy & 16 oz. Pepsi");
        builder.setTitle("Thursday 3$");
        builder.setNegativeButton("Avail", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id){
            checker[1]=1;
            layout.removeView(b1);
           // sendMail("Thursday 3%","1-pc. Chickenjoy & 16 oz. Pepsi");
           try {
              new MyAsyncClass().execute();
        } catch (Exception ex) {
        Toast.makeText(getApplicationContext(), ex.toString(), 100).show();
        }
            // layout.addView(b1);
            //Toast.makeText(getApplicationContext(), "Availed"  , Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
       
        }
        }
        );
        builder.create();
        builder.show(); 
      
}
});
}
public void b2(){
b2.setOnClickListener(new OnClickListener(){
ViewGroup layout = (ViewGroup) b2.getParent();
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(Kupon.this);
        builder.setMessage("1pc. Chicken with Spaghetty and 16 oz. Pepsi");
        builder.setTitle("Wednesday 5$");
        builder.setNegativeButton("Avail", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
            checker[2]=2;
            layout.removeView(b2);
            sendMail("Wednesday 5$","1pc. Chicken with Spaghetty and 16 oz. Pepsi");
            Toast.makeText(getApplicationContext(), "Availed"  , Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
        }
        }
        );
        builder.create();
        builder.show(); 
}
});
}
public void b3(){
b3.setOnClickListener(new OnClickListener(){
ViewGroup layout = (ViewGroup) b3.getParent();
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(Kupon.this);
        builder.setMessage("Tuesday 3$");
        builder.setTitle("Spaghetti");
        builder.setNegativeButton("Avail", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
            checker[3]=3;
            layout.removeView(b3);
            sendMail("Tuesday 3$","Spaghetty");
            Toast.makeText(getApplicationContext(), "Availed" , Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
        }
        }
        );
        builder.create();
        builder.show(); 
}
});
}
public void b4(){
b4.setOnClickListener(new OnClickListener(){
ViewGroup layout = (ViewGroup) b4.getParent();
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(Kupon.this);
        builder.setMessage("Thursday 3$");
        builder.setTitle("1pc. Chickenjoy & 16 oz Pepsi");
        builder.setNegativeButton("Avail", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
            checker[4]=4;
            layout.removeView(b4);
            sendMail("Thursday 3$","1pc. Chickenjoy & 16 oz Pepsi");
           
            Toast.makeText(getApplicationContext(), "Availed" , Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
        }
        }
        );
        builder.create();
        builder.show(); 
}
});
}
public void b5(){
b5.setOnClickListener(new OnClickListener(){
ViewGroup layout = (ViewGroup) b5.getParent();
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(Kupon.this);
        builder.setMessage("Thursday 3%");
        builder.setTitle("1pc. Chickenjoy & 16 oz Pepsi");
        builder.setNegativeButton("Avail", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
            checker[5]=5;
            layout.removeView(b5);
            sendMail("Thursday 3$","1pc. Chickenjoy & 16 oz Pepsi");
            Toast.makeText(getApplicationContext(), "Availed" , Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
        }
        }
        );
        builder.create();
        builder.show(); 
}
});
}
public void b6(){
b6.setOnClickListener(new OnClickListener(){
ViewGroup layout = (ViewGroup) b6.getParent();
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(Kupon.this);
        builder.setMessage("Wednesday 5$");
        builder.setTitle("1pc. Chickenjoy with Spaghetty & 16 oz Pepsi");
        builder.setNegativeButton("Avail", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
            checker[6]=6;
            layout.removeView(b6);
            sendMail("Wednesday 5$","1pc. Chickenjoy with Spaghetty & 16 oz Pepsi");
            Toast.makeText(getApplicationContext(), "Availed" , Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
        }
        }
        );
        builder.create();
        builder.show(); 
}
});
}
public void b7(){
b7.setOnClickListener(new OnClickListener(){
ViewGroup layout = (ViewGroup) b7.getParent();
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(Kupon.this);
        builder.setMessage("Wednesday 5$");
        builder.setTitle("1pc. Chickenjoy with Spaghetty & 16 oz Pepsi");
        builder.setNegativeButton("Avail", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
            checker[7]=7;
            layout.removeView(b7);
            sendMail("Wednesday 5$","1pc. Chickenjoy with Spaghetty & 16 oz Pepsi");
            Toast.makeText(getApplicationContext(), "Availed" , Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
        }
        }
        );
        builder.create();
        builder.show(); 
}
});
}
public void b8(){
b8.setOnClickListener(new OnClickListener(){
ViewGroup layout = (ViewGroup) b8.getParent();
public void onClick(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(Kupon.this);
        builder.setMessage("Wednesday 5$");
        builder.setTitle("1pc. Chickenjoy with Spaghetty & 16 oz Pepsi");
        builder.setNegativeButton("Avail", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
            // layout.addView(b1);
            checker[8]=8;
            layout.removeView(b8);
            sendMail("Wednesday 5$","1pc. Chickenjoy with Spaghetty & 16 oz Pepsi");
            //Toast.makeText(getApplicationContext(), "Availed" , Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
        }
        }
        );
        builder.create();
        builder.show(); 
}
});
}
private void sendMail(String x1, String x2){
  
//    try {
//            new MyAsyncClass().execute();
//      } catch (Exception ex) {
//     Toast.makeText(getApplicationContext(), ex.toString(), 100).show();
//      }
  
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"Tiramizu28@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, x1);
i.putExtra(Intent.EXTRA_TEXT   , x2);
try {
   startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
   Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
/*Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","Tiramizu28@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, x2);
startActivity(Intent.createChooser(emailIntent, "awewew"));*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.kupon, menu);
return true;
}
class MyAsyncClass extends AsyncTask<Void, Void, Void> {
        ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
             super.onPreExecute();
             pDialog = new ProgressDialog(Kupon.this);
             pDialog.setMessage("Please wait...");
             pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... mApi) {
             try {
    // Add subject, Body, your mail Id, and receiver mail Id.
       sender.sendMail("Math", " sexybody", "argenteedmarkr@gmail.com", "clamornhel@gmail.com");
        Toast.makeText(getApplicationContext(), "Email send", Toast.LENGTH_LONG).show();
             }
             catch (Exception ex) {
            Toast.makeText(getApplicationContext(), "not send", Toast.LENGTH_LONG).show();
             }
             return null;
        }

        @Override
        protected void onPostExecute(Void result) {
             super.onPostExecute(result);
             pDialog.cancel();
        }
  }
}



Manoj Pandey

Total Post:26

Points:188
Posted on    June-22-2015 4:15 AM

 
Remove toast from Void doInBackground(Void... mApi) 

You cannot show toast on doInBackground() method here 
  because there is dialog already running, you have need to remove toast from here and add on onPostExecute( ). 

  @Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.cancel();
Toast.makeText(MainActivity.this, "Email send", Toast.LENGTH_LONG)
.show();
}

Don't want to miss updates? Please click the below button!

Follow MindStick