i checked this web site on how to convert from string to integer type in java(android).... one of suggestion was to use (integer.parseint) i used it and when i run my application it says my app has stopped working my code below .
public void clickable (View view){
EditText mytext = (EditText) findViewById(R.id.Creat);
int number = Integer.parseInt(mytext.getText().toString());
Toast.makeText(getApplicationContext(), number ,Toast.LENGTH_LONG).show();
}
i cant figure out what is the problem with the code ?!
Declare EditText mytext variable as a global variable and then initialize it in Oncreate() method of your Activity. Then your clickable method looks like this:
public void clickable (View view){
int number = Integer.parseInt(mytext.getText().toString());
Toast.makeText(getApplicationContext(), mytext.getText().toString(), Toast.LENGTH_LONG).show();
}
Obeserve Toast.makeText() method's second argument is the resource id of the string resource to use or it can be formatted text. In your code you have passed an integer as a resource id which does not exist. So you get ResourcesNotFoundException.
What string are you passing into the Integer.parseInt()? If it's not an integer, your program will experience a NumberFormatException. I'm not sure if that's the issue here, but I'm not sure what you're passing into the Integer.parseInt().
There are many implementations of Toast.makeText. As you are passing an int as the second argument, the following implementation will execute:
Toast makeText(Context context, #StringRes int resId, #Duration int duration)
This implementation will throw a ResourcesNotFoundException if it cannot find a resource with the id of resId.
To output number as a String you need to convert it:
Toast.makeText(getApplicationContext(), String.valueOf(number), Toast.LENGTH_LONG).show();
Related
I'm getting the error "Variable 'hour' might not have been initialized", and I'm getting that same error for hour,tenMin, min, and ampm. I used intent to get these variables from another class, and I'm not sure what the issue is. Thank you in advance.
I've tried making the variables on the first class final, but that didn't do anything.
This is where I'm getting the errors:
Intent intent=getIntent();
String hour=intent.getStringExtra(hour);
String tenMin=intent.getStringExtra(tenMin);
String min=intent.getStringExtra(min);
String ampm=intent.getStringExtra(ampm);
This is where I'm getting the variables from:
EditText editText=findViewById(R.id.editText);
EditText editText2=findViewById(R.id.editText2);
EditText editText3=findViewById(R.id.editText3);
EditText editText4=findViewById(R.id.editText4);
String hour=editText.getText().toString();
String tenMin=editText2.getText().toString();
String min=editText3.getText().toString();
String ampm=editText4.getText().toString();
Intent intent=new Intent(NewAlarm.this,MainActivity.class);
intent.putExtra(hour,hour);
intent.putExtra(tenMin,tenMin);
intent.putExtra(min,min);
intent.putExtra(ampm,ampm);
I guess the error is happening because you are using String objects as key. However, as your code is now, those objects may be null/not initialized.
I think you should change your code as follows:
Intent intent=getIntent();
String hour=intent.getStringExtra("hour");
String tenMin=intent.getStringExtra("tenMin");
String min=intent.getStringExtra("min");
String ampm=intent.getStringExtra("ampm");
And
intent.putExtra("hour",hour);
intent.putExtra("tenMin",tenMin);
intent.putExtra("min",min);
intent.putExtra("ampm",ampm);
This way, you are going to use constant Strings as key
I have 2 activities in my assignment: MainActivity and Country_Activity.
I'm trying to pass 2 inputs the user puts in MainActivity:
int counter
String Country
But the app always crashes here: (this is Country_Activity)
private void Update(){
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intCounter", 0);
String country = mIntent.getStringExtra("country");
counterTextView.setText(intValue);
countryTextView.setText(country);
if (country.equals("canada")){
flagView.setImageResource(R.drawable.canada);
}
else if (country.equals("us")){
flagView.setImageResource(R.drawable.us);
}
}
Specifically on the lines "setText" for each variable.
Everything else works. I can't figure out why they wouldn't.
Thanks!
Usually the extras that are passed from one activity to another are read inside on onCreate() where all the needed initialization of variables and views is made.
In your case I see that you get the extras inside another method (maybe it's called inside onCreate()?).
So you forgot to initialize the textviews:
TextView counterTextView = findViewById(R.id.countersomething);
TextView countryTextView = findViewById(R.id.countrysomething);
also another error that you will encounter later is this:
counterTextView.setText(intValue);
change it to:
counterTextView.setText(String.ValueOf(intValue));
Don't pass an integer value inside setText() because it will be treated as the id of a resource.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView ballDisplay = (ImageView)findViewById(R.id.image_eightBall);
final int[] ballArray = {R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5};//*******What integer is that???????????
Button myButon = (Button)findViewById(R.id.askButton);
myButon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random randomNumberGenerator = new Random();
int number = randomNumberGenerator.nextInt(5);
ballDisplay.setImageResource(ballArray[number]);
}
});
}
}
What is the value stored in that intger?
Your Drawables data type isn't an int; your reference to it is an int, which you can see in R.java.
final int[] ballArray =
{R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5};
so here your are only storing reference.
You probably don't really need to know the actual value of R.drawable.ball5 as it is determined during the build.... but I won't second guess your question-- the actual value of R.drawable.ball5 can be found in the file R.java in the build folder that's created during the build.
To find it in Android Studio, after you've done a build you can right-click on R.drawable.ball5 in the editor, then Go To -> Implementation(s) and you'll see the generated R.Java file as well as the number that is assigned to this resource for this build. It looks like:
public static final int ball5=0x7f080093;
Programmatically, you can just log Integer.toString(R.drawable.ball5).
That's the answer to the question you asked. But I think you'd be better off looking at R.id.ball5 for example to explicitly name the resource or reference the drawable by the name "ball5" and refer to it with that variable name. You shouldn't really care about what value R.drawable.ball5 actually is as it will likely change as you add or remove other resources.
Something like this might work better insofar as I can guess what you're trying to do:
public void onClick(View v) {
Random randomNumberGenerator = new Random();
int number = randomNumberGenerator.nextInt(5);
ballDisplay.setImageDrawable(this,
getDrawable(ballArray[number]));
}
If you want to get the drawable using a method that supports older APIs, try:
ballDisplay.setImageDrawable(
this.getResources()
.getDrawable(ballArray[number]);
R.drawable.image stores a resource id which will return an int.
There is no need to know what value it returns because you already know how to access it, and, you cannot know it initially because the value is determined during the build time, all you can know at the start is that it is a number in hexa decimal
Still if you want to know after the build completes go to R.java file there you will find it.
R.drawable.image means the drawable resource, in this case image. Drawable means graphic which can be drawn. Every drawable is stored in res/drawable folder. Your ballArray is the array of drawables.
I am developing an android app in which the data is taken from JSON. And I am generating the listview through Baseadapter. When I click the number (TextView), I need to make a phone call to that number. I have tried it as follows
//initialization
public long abc;
in getView(int position, View convertView, ViewGroup parent)
{
abc = m.getNumber();
number.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent out = new Intent(Intent.ACTION_CALL);
out.setData(Uri.parse("tel:" + Uri.encode(abc)));
context.startActivity(out);
}
});
return convertView;
}
Error I am getting is 'Encode(java.lang.string)' in android.net.Uri cannot be applied to long.
Actually what does it mean? How to solve the problem?
Your abc variable is long. convert it into String as error saying "Encode(java.lang.string)' in android.net.Uri cannot be applied to long"
Intent out = new Intent(Intent.ACTION_CALL);
out.setData(Uri.parse("tel:" + Uri.encode(String.valueOf(abc))));
context.startActivity(out);
My solution for fixed this issue:
out.setData(Uri.parse("tel:" + Uri.encode(Stirng.valueOf(abc))));
Or
out.setData(Uri.parse("tel:" + Uri.encode(""+abc)));
Covert value of abc variable to String. Or, use
out.setData(Uri.parse("tel:" +String.valueOf(abc)));
You may want to use ACTION_DIAL instead of ACTION_CALL. ACTION_DIAL gives user the opportunity to check number before calling, while ACTION_CALL will directly make a call to the given number.
The variable abc is long. Uri.encode() requires a String parameter. So convert abc to a String to remove the error.
The following are the options:
Uri.parse("tel:" + Uri.encode(String.valueOf(abc)))
or
Uri.parse("tel:" + Uri.encode("" + abc))
For example I have Hello123456 string and I want to show it as just Hello in a TextView.
I need those numbers in my code and I can't erase them.
EDIT:
All answer are providing a way that I erase the numbers and then set it to textview, But I want them to be there.
I get the "hello" part from user and I add the number part to it and this will be the name of a listview Item. but I want to show just the hello part in listview and also if I checked if that listview clicked item ends with "123456" it returns true.
I want to show it as just "Hello" in a TextView.
Then just put Hello in your TextView without cutting your string or create an temporary string to hold the "Hello" String.
If your string is like this "HELLOHI12345" then you need a regex to eliminate all the number string within it.
sample:
textview.setText(s.replaceAll("[0-9]+", ""));
Also take note that string are immutable so the original String wont get replaced after executing replaceAll
Use this dude ! :) You can use contextDescription to access the actual text
textView.setText(yourText.replaceAll("[0-9]",""));
textView.setContentDescription(yourText);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String str = ((TextView) v.getContentDescription()).toString();
}
});
A TextView can have a tag attached to it i.e. some object that is stored as extra data to the view itself. Check out the documentation here:
View.setTag(Object)
In your case, you can assign the "Hello" to the displayed text (using the method below), and set the tag to the full text.
textView.setText(getDisplayText(helloString));
textView.setTag("TAG", helloString);
When the user clicks on your view, you can get the tag from the view and do what you need with it.
This way, there is more data stored than what you see.
(original answer before the edit)
How about a method that does what you need:
private String getDisplayText(String input)
{
return input.substring(0, 5);
}
And then just use that method when you set the value of your TextView.
textView.setText(getDisplayText(helloString));
use \\w+ regex with Pattern and Matcher classes. This will also work if you don't know the String length. The String can be hello123 or olo123
Its not possible to hide the characters in a string. You have to solve your problem in a different way. My suggestion is to implement a separate class which contains the string:
I would write an class which contains the string. Then add two methods to this class which returns the string for display and for internal use. E.g.
class DataObject {
private String data;
public String forTextView() {
//code from one of the answers above, may be the regex version
}
public Stringg getStringWithNumbers() {
return data;
}
}
This allows you to show the string without the numbers and still have them ready when you use them.