Error: The constructor Object(EditText) is undefined - android

I'm getting an error in this method: "The constructor Object(EditText) is undefined"
private void setupAlert()
{
Log.d(TAG, "setupAlert()");
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.emaildialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText)promptsView.findViewById(R.id.editTxtEmailAddress);
alertDialogBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
updatePref(userInput.getText().toString());
Log.d(MainActivity.TAG, "setupAlert: getPreference()=" + getPreference());
takeSnapNow();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.colour_selector);
Log.d(TAG, "onCreate: getPreference()=" + getPreference());
if (getPreference().equals("")) {
setupAlert();
takeSnapNow();
} else {
takeSnapNow();
}
}
private void takeSnapNow()
{
String fileName = "TempImage.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "Captured by XYZ app");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
Please suggest the solution to this error.
Edit: Corrected the above error by removing UserInput as suggested by others. However the alertDialog is still not shown. The logic is to check the sharedpref initially in onCreate, and if not found, call setupAlert() method to get email ID from user.
Any clues? Changed code updated in the above snippet.

DialogInterface.OnClickListener doesn't take an EditText as a constructor parameter and you don't need to do that ! you can use userInput without passing it to DialogInterface.OnClickListener, just use it as follow
alertDialogBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {

Why are you using "new DialogInterface.OnClickListener(userInput)"?
Just use new DialogInterface.OnClickListener()

Related

Why OnClickListner is not called on clicking?

This is my code for the BASE class, i am inheriting this class to another child class. And from that class i am calling this function ActiveSMSPackage(). My code is perfect which has not kind of any error but yet the method is not called. Can you guys tell me where i am doing wrong?
public class PrepaidSMSBase extends Activity {
private String smsNumber = "";
private String smsPackageName;
private String smsPrice;
private String smsTitle;
private String smsText;
public PrepaidSMSBase(){}
public void setSmsPackageInformation(String smsTitle, String smsPackageName, String smsPrice, String smsNumber, String smsText)
{
this.smsTitle = smsTitle;
this.smsPackageName = smsPackageName;
this.smsPrice = smsPrice;
this.smsNumber = smsNumber;
this.smsText = smsText;
}
public void activeSMSPackage()
{
try
{AlertDialog.Builder builder = new AlertDialog.Builder(PrepaidSMSBase.this);
builder.setTitle(smsTitle);
builder.setMessage("Are you sure you want to active" + PrepaidSMSBase.this.smsPackageName + "in RS: " +
PrepaidSMSBase.this.smsPrice);
builder.setPositiveButton("Activate", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", smsText);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Please click send button to activate desire Package", Toast.LENGTH_LONG).show();
}
});
}catch (ActivityNotFoundException e){
e.printStackTrace();
}
}
Code of Child class is:
public class SMSCheckClass extends PrepaidSMSBase implements View.OnClickListener{
Button checkButton;
public SMSCheckClass(){
setSmsPackageInformation("Test 1","Some Thing","50","660","Sub");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checksms);
checkButton = (Button)findViewById(R.id.chkBtn);
checkButton.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
activeSMSPackage();
}
Use this code
{AlertDialog.Builder builder = new AlertDialog.Builder(PrepaidSMSBase.this);
builder.setTitle(smsTitle);
builder.setMessage("Are you sure you want to active" + PrepaidSMSBase.this.smsPackageName + "in RS: " +
PrepaidSMSBase.this.smsPrice);
builder.setPositiveButton("Activate", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", smsText);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Please click send button to activate " + PrepaidSMSBase.this.smsPackageName, Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
you just need to add builder.show() in the end of AlertDialog because your function is written inside of AlertDialog and that show() method is used to show AlertDialog
At first, you should show you dialog in activeSMSPackage, please add this code in that method:
builder.show()

Password Prompt Comes Only first time

I want when user click on Uninstall Button,there prompt a password dialog. This Dialog is coming only one time.I'm using this code:
public void run() {
Looper.prepare();
while (!exit) {
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(MAX_PRIORITY);
String activityName = taskInfo.get(0).topActivity.getClassName();
Log.d("topActivity", "CURRENT Activity ::" + activityName);
if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
//Toast.makeText(context, "Uninstall Clicked", Toast.LENGTH_LONG).show();
Intent startIntent = new Intent(this.context, Alert_Dialog.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.context.startActivity(startIntent);
exit = true;
} else if (activityName.equals("com.android.settings.ManageApplications")) {
Toast.makeText(this.context, "Back", Toast.LENGTH_LONG).show();
exit = true;
}
}
Looper.loop();
}//Run
I want whenever user click on Unistall Prompt should come , below is the code in onClick,
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView.findViewById(R.id.edit_text);
alertDialogBuilder
.setCancelable(false)
.setNegativeButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
/** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
String user_text = (userInput.getText()).toString();
/** CHECK FOR USER'S INPUT **/
if (user_text.equals("abc"))
{
Log.d(user_text, "HELLO THIS IS THE MESSAGE CAUGHT :)");
Toast.makeText(myContext,"PAssword Correct",Toast.LENGTH_LONG).show();
Alert_Dialog.this.finish();
//Search_Tips(user_text);
}
else{
Log.d(user_text,"string is empty");
String message = "The password you have entered is incorrect." + " \n \n" + "Please try again!";
AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
builder.setTitle("Error");
builder.setMessage(message);
builder.setPositiveButton("Cancel", null);
builder.create().show();
Alert_Dialog.this.finish();
}
}
});
// .setPositiveButton("Cancel",
// new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog,int id) {
// Alert_Dialog.this.finish();
//
// }
//
// }
// );
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Once the dialog calls dismiss(), the view you set for the dialog also would be destroyed.
In your case, this line set the view,
alertDialogBuilder.setView(promptsView);
But when the dialog is closed, the view promptsView is destroyed,
The promptsView should be re-created once again you use it.
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
promptsView = new PromptsView();//new or inflate the view
//....
alertDialogBuilder.setView(promptsView);

Saved File not present in internal storage

i want to save a view, when i run the app everything is working fine, but i am unable to find saved file/s in the internal storage of the phone. Any help would be appreciated as i am new and unable to sort out what is going wrong . here is my code
public void saveMe(View v) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompt.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialog);
// set dialog message
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final String fileName = userInput.getText().toString();
final View view1=findViewById(R.id.relativeLayout); // The view that you want to save as an image
Bitmap bitmap = Bitmap.createBitmap(view1.getWidth(), view1.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view1.draw(c);
if(fileName.length() == 0)
Toast.makeText(EidCardFinal.this,"Please Enter File Name",Toast.LENGTH_SHORT).show();
else{
File file = new File(context.getFilesDir(), fileName);
if (file.exists())
Toast.makeText(EidCardFinal.this,"File Already Exists",Toast.LENGTH_SHORT).show();
else{
try{
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, out);
Toast.makeText(EidCardFinal.this,"File Saved",Toast.LENGTH_SHORT).show();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}

making phone call from DialogBox in Android

I have one DialogBox with 2 buttons. If we click the +ve button, it attempts to open the Dialing Pad; else if we click the -ve button it closes the dialog. When I click the +ve button it shows the null exception. If the same code executes without the DialogBox, it is fine.
Here is my code:
callDialog.setPositiveButton("Call Now", new android.DialogInterface.
OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
}
I've given permissions in the manifest file as <uses-permission android:name="android.permission.CALL_PHONE"/>
Try this..
Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel:phonenumber");
startActivity(call);
Remove the #Overrride line.
Like this:
callDialog.setPositiveButton("Call Now", new android.DialogInterface.
OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
}
----UPDATE-----
Since you havent posted any logcat, its hard to know where it crashes. Try this block:
AlertDialog.Builder callDialog = new AlertDialog.Builder(this);
callDialog.setTitle("My title");
callDialog.setMessage("My message");
callDialog.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
});
callDialog.setNegativeButton("Cancel", null);
callDialog.show();
Try the following---
Intent dial = new Intent(Intent.ACTION_DIAL);
//dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
startActivity(dial);
Use
Intent callIntent = new Intent(Intent.ACTION_CALL);
instead of this
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setAction("android.intent.action.DIAL"); // but this line is an optional one
callIntent.setData(Uri.parse("tel:044 6000 6000"));
startActivity(callIntent);
use this code......
String number = "tel:" + phonenumber;
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse(number));
startActivity(intent);
give permission in manifest: <uses-permission android:name="android.permission.CALL_PHONE" />
updated.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.menu_icon_pager).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit")
.setMessage(
"Texting from dialog")
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// Exit the activity
String number = "tel:"+phonenumber ;
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse(number));
startActivity(intent);
}
})
.show();
}
});
}
Actually I called the AlertDialog from another Activity,so I need the context of called
Activity's Context reference.So I passed the Context as a parameter to Called Activity
Here is Called Activity's code
if(position == 1)new CalledByActivity().dailingDialogBox(param1,
param2,context);//Here I'm passing the context as param
Here CalledBy activity's implemented function
public void dailingDialogBox(final String param1,String param2,final Context context){
Here I implemented in +ve button click
Intent dail = new Intent(Intent.ACTION_DIAL);
dail.setData(Uri.parse("tel:"+station_Number));
context.startActivity(dail);//Here the "context" is reference of called Activity's context

Posting something to status on Facebook

I am using this tutorial to learn how to connect to Facebook via Android app. I am particularly interested in the section where he shows how to post something on the wall / status of the user.
The tutorial is fairly straightforward however my requirement is that I need to let the app append things along with what the user posts.
How can that be achieved ?
You can do something like this:
private void publishToFacebook(String message)
{
long songId = MusicUtils.getCurrentAudioId();
long albumId = MusicUtils.getCurrentAlbumId();
String albumartUrl = MusicUtils.getArtworkUrlFromExtrasCache(getApplicationContext(),albumId);
Bitmap bm = MusicUtils.getArtworkFromExtrasCache(getApplicationContext(),albumId,false,false);
if(bm == null)
bm = MusicUtils.getDefaultArtwork(getApplicationContext());
shareFacebookConnector = new ShareConnector(MediaPlaybackActivity.this, getApplicationContext());
shareFacebookConnector.setCurrentAlbum(MusicUtils.getCurrentAlbumName());
shareFacebookConnector.setCurrentArtist(MusicUtils.getCurrentArtistName());
shareFacebookConnector.setCurrentTrack(MusicUtils.getCurrentTrackName());
shareFacebookConnector.setCurrentCoverArt(bm);
if(albumartUrl != null)
{
shareFacebookConnector.setCurrentCoverUrl(albumartUrl);
}
LayoutInflater inflater = this.getLayoutInflater();
final View dialoglayout = inflater.inflate(R.layout.facebook_share, (ViewGroup) findViewById(R.id.facebook_share_root));
final ImageView image = (ImageView) dialoglayout.findViewById(R.id.facebook_cover_view);
input = (EditText) dialoglayout.findViewById(R.id.facebook_share_content);
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AlertDialogCustom);
AlertDialog.Builder alert = new AlertDialog.Builder(ctw);
if(message != null)
{
shareFacebookConnector.publishToFacebook(alert, input, message);
}
else
{
shareFacebookConnector.publishToFacebook(alert, input, null);
}
alert.setView(dialoglayout);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
bAlertInProgress = false;
String messageToPost = input.getText().toString();
shareFacebookConnector.postMessageToWall(messageToPost, false);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
bAlertInProgress = false;
Toast.makeText(MediaPlaybackActivity.this, "Wall post cancelled !", Toast.LENGTH_SHORT).show();
//finish();
}
});
bAlertInProgress = true;
mShareKey = "********";
alertDialog = alert.create();
alertDialog.show();
}
You have to open the session as session.openForPublish(); and you required permission "publish_action"

Categories

Resources