I was wanting to be able to have a button which would launch the Google Maps app, however I cannot really seem to be able to get it to work, could I have some help? Really sorry if this seems easy, i'm still a newbie with android, below is my oncreate method with the relavent Intents
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setIcon(R.drawable.ic_action_headphones);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
Button mapsbtn = (Button) findViewById(R.id.mapbtn);
mapsbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String uri = String.format(Locale.ENGLISH, "geo:%f,%f");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
});
}
My app keeps crashing when the button is pressed, and the error i am receiving is
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: juddsafc.actionbarexample, PID: 2980
java.util.MissingFormatArgumentException: Format specifier: f
Thanks in advance!
When you put %f,%f you need to provide values afterwards... So change this:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f");
to this:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 35.6833, -139.7667); // numbers are just some random coordinates
String uri = String.format(Locale.ENGLISH, "geo:%f,%f");
The %f is a formatter. Your program expects a value to replace %f, but there's not enough arguments for your function to accomplish this.
You could do the following:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 100, 200);
And instead of using 100 and 200, you pass along the values you want to display.
Related
I am learning the android room with a view. I've looked through some sample projects and tutorials and there is one thing in this example that I am hung up on and that I do not understand. Here is the code (underneath the code I point out the few lines I'm confused about):
public class NewWordActivity extends AppCompatActivity {
public static final String EXTRA_REPLY = "com.example.android.wordlistsql.REPLY";
private EditText mEditWordView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_word);
mEditWordView = findViewById(R.id.edit_word);
final Button button = findViewById(R.id.button_save);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(mEditWordView.getText())) {
setResult(RESULT_CANCELED, replyIntent);
} else {
String word = mEditWordView.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, word);
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
}
}
The parts that I am confused about are the second line, the EXTRA_REPLY, and then you can see it used toward the bottom in reply.Intent.putExtra. What is the EXTRA_REPLY pointing to exactly? How would you find it in your own project?
Here is the source of the sample if you need more context: https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#12
This is just a simple key/value pair. Like a HashMap, or a Dictionary.
replyIntent.putExtra(EXTRA_REPLY, word);
This will set the EXTRA_REPLY to word within your Intent so you can read it in whatever Activity is handling your result.
You can also do it with a Bundle, for example when you launch a new Activity.
The key, com.example.android.wordlistsql.REPLY, does not really matter, just make sure that everyone is using the same key.
When you make an intent you can add extra data to it in key-value form. In this example they use the string constant EXTRA_REPLY as the key, and the variable word as the value.
The recipient of the intent would access the data by using intent.getStringExtra(NewWordActivity.EXTRA_REPLY).
It's good practice to define the keys for your intents as constants so that you're less likely to make mistakes when referencing it in other classes.
I am really new at programming. I am trying to run a simple average calculator and getting a force close, this is what the logcat is showing. I am running android studio version 2.3.3
FATAL EXCEPTION: main
Process: com.vu.gradingapp, PID: 6312
java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:620)
at java.lang.Integer.valueOf(Integer.java:794)
at com.vu.gradingapp.AverageActivity$1.onClick(AverageActivity.java:37)
at android.view.View.performClick(View.java:6219)
at android.view.View$PerformClick.run(View.java:24482)
at android.os.Handler.handleCallback(Handler.java:769)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
The code i am using is as follow
public class AverageActivity extends AppCompatActivity {
EditText editmanner, editinstances, editshortstance, editstrikes, editboxingskills, editknocks, editkicks, editResults;
Button btnResults;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.average_page);
editmanner =(EditText)findViewById(R.id.editText8);
editinstances = (EditText)findViewById(R.id.editText9);
editshortstance = (EditText)findViewById(R.id.editText10);
editstrikes = (EditText)findViewById(R.id.editText11);
editboxingskills = (EditText)findViewById(R.id.editText12);
editknocks = (EditText)findViewById(R.id.editText13);
editkicks = (EditText)findViewById(R.id.editText14);
editResults = (EditText)findViewById(R.id.editText15);
btnResults = (Button) findViewById(R.id.button10);
btnResults.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int first, second, third, fourth, fifth, sixth, seventh, results;
first=Integer.valueOf(editmanner.getText().toString());
second=Integer.valueOf(editinstances.getText().toString());
third=Integer.valueOf(editshortstance.getText().toString());
fourth=Integer.valueOf(editstrikes.getText().toString());
fifth=Integer.valueOf(editboxingskills.getText().toString());
sixth=Integer.valueOf(editknocks.getText().toString());
seventh=Integer.valueOf(editkicks.getText().toString());
results=(first+second+third+fourth+fifth+sixth+seventh)/7;
editResults.setText(String.valueOf(results));
}
});
}
public void knowtheresults (View view) {
String button_text;
button_text = ((Button) view).getText().toString();
if (button_text.equals("Summary")) {
Intent intent = new Intent(this, ResultActivity.class);
startActivity(intent);
} else if (button_text.equals("Back")) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
WHat am I doing wrong :D
Thanks Guys
Error says you cannot convert "" value to number Its a java.lang.NumberFormatException. So use 0 instead of number is blank first check if edittext is blank than set its value to 0
int first;
if(editmanner.getText().toString().equals("")){
first = 0
}else{
first = Integer.valueOf(editmanner.getText().toString());
}
do this for your all int variables.
Update your code with such verification for each int variable:
if (editmanner().toString().isEmpty())
first = 0;
else
first = Integer.parseInt(editmanner().toString());
Since you have int variables (not Integer) it is preferable to use parseInt instead valueOf
Thanks guys, I just filled in the textedits with (0) and now is working. Please apologize for my silly questions.
Is there any way to hide this zeros on the textview so it shows blanks.
java.lang.NumberFormatException
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
The logcat already stated what is the cause of the crash. One or all of your EditText has empty string. To convert string to Integer, the string must be numeric(0,1,2 etc). You need to set EditText with numeric value (eg: 0).
I'm trying to get the user information typed in the edit text. I want it saved into the Intent result variable. Trying to sending it back to the main activity afterwards. Keep getting the cannot resolve method. I'm thinking it must be that I'm missing a parameter in the putExtra() method
public class EnterDataActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enterdata);
Button doneButton = (Button) findViewById(R.id.button_done);
final EditText getData = (EditText) findViewById(R.id.enter_data_here);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra(getData);
setResult(RESULT_OK, result);
finish(); // Ends sub-activity
}//ends onClick
});
}//ends onCreate void button
}
Well if you are trying to send the EditText that's not possible.
If you are trying to send the text that are on that EditText that's possible.
How to do it?
Declare a String to save the data (You can avoid this step, but that's more clear)
String mText = getData.getText().toString();
Then you'll use the getExtra() method to send the String to the new Activity
Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_from_editText", mText);
startActivity(i);
Then the last step (You don't ask for it, but you'll need it), get the text.
//onCreate() of the second Activity
Intent i = getIntent();
String mText = i.getStringExtra("text_from_editText");
Replace result.putExtra(getData); with result.putExtra(getData.getText().toString()); to get the text from the EditText. Right now you're trying to put the entire EditText object into the intent as an extra instead of just the text from the EditText.
I need start a new android activity using a string,
The main src name is: AlphabetListDemo.java
The activit name class that i want to run is: videoviewted.java
This is the src from AlphabetListDemo.java:
http://pastebin.com/3hrz8Yms
I've tried with this but it gives me error:
http://pastebin.com/pBxZFUTV
Thanks by the help.
You have to put the string you need in the Intent
intent.putExtra("myString",<some string>);
startActivity(intent);
And you can retreive it like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String myString = getIntent().getStringExtra("myString");
...
}
i have a problem to pass extras between an Activity and a FragmentActivity.
This next code create the Intent to start the new Activity:
Intent fichaSerie = new Intent(getActivity(),ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getID());
getActivity().startActivity(fichaSerie);
When I tried to get the Extras in the Fragment Activity, i get a NullPointerException:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
String extra = getIntent().getExtras().getString("serie") //NullPointerException
}
Anyone know what mistake(s) i'm made in my code?
Thank you very much to all.
Thanks all your help guys, i solved my problem using this call:
getIntent().getStringExtra("serie")
I mark this question as solved.
Thank you again guys.
First of all Java is case sensitive so .getID() is not the same as .getId().
If you are trying to get an ID of an objet, use .getId(). That usually returns an Integer value
Pay attention on the type you are putting to extra as Michal said (String or Integer or anything else)
Intent fichaSerie = new Intent(getApplicationContext(), ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getId()); //presuming this id is an Integer or long int ... You could change this to string if it is still usable then (serie.getId().toString())... but you have to get an intent with myIntent.getStringExtra(...), but I think you will not be doing this.
startActivityForResult(fichaSerie, 0);
Getting an extra from ActividadSerie.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
Intent myIntent = getIntent();
String extra = myIntent.getStringExtra("serie"); //try diferent gets... getIntExtra(), getStringExtra()..
}
You are put an Integer value and get it into the String value so that it gets a NullPointerException.