Why am I getting this error after I use intent? - java

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

Related

Android Runtime error on passing info between activities

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.

EditText to integer in android

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();

is it possible to retrieve google links from strings?

i currently have a menu item that looks like the following
case R.id.action_settings_rate_app:
Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent3.setData(Uri.parse("market://details?id=com.raptools.app.raptools"));
startActivity(intent3);
bRet=true;
break;
where it says .setData(Uri.parse i would like to call a string and place the link market://details?id=com.raptools.app.raptools in the string.... is this possible to do?
the reason i want to do this is because i have over 25 java files that all call the menu and have the same links in them..... if i could call the string i would only need to change the one string value instead of having to change all the menu items one by one
thanks in advance
It's totally possible, for that you need handle the links, see this link.
So, in your Activity you can get the parameters.
eg.:
MyActivity1
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("my-scheme://my-domain.com/params1"));
startActivity(intent);
MyActivity2
#Override
protected void onCreate(Bundle savedInstanceState) {
...
Uri data = getIntent().getData();
String p = data.getLastPathSegment(); // Variable p is params1.
}
Look into using a Java Properties file to store this value (and possibly others) for your application. If you do this, you can just change the values in the .properties file and your code can pick up the changes.
Hi as per my understanding with your
You want to place link into string.so that it will be easy for you to change at one place
Let me know if I misunderstand your question
You can add link into String/ String builder
For example
Initialize string variable with link
Like
String urlLink="market://details?id=com.raptools.app.raptools";
case R.id.action_settings_rate_app:
Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent3.setData(Uri.parse(urlLink));
startActivity(intent3);
break;
You can add string variable into separate class and declared String variable as public static into it
So that using class name you will get the urlLink wherever you want in the application

getStringExtra() always throws NullPointerException

Main activity:
Intent intent = new Intent(Main.this, Secondary.class);
intent.putExtra("name",value);
startActivity(intent);
Secondary activity:
String value = getIntent().getStringExtra("name")
What's wrong here? I've searched a lot without success...
Thanks
Try this:
In the MainActivity:
//Make sure Secondary is an Activity name. Secondary.class.
Intent intent = new Intent(MainActivity.this, Secondary.class);
intent.putExtra("name",value);
startActivity(intent);
In the Secondary Activity:
String value = getIntent().getExtras().getString("name");
You need to get the bundle first and then extract the string from it.
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
bundle.getString("name");
}
Both should work. The second one is to check if the bundle is null.
When you call putExtra(...), make sure the value object is a String. If you're passing any other object, make sure to explicitly call value.toString(), especially if dealing with GUI components.
See here for more information: Android Intent.getStringExtra() returns null
I have used this method times. Just make sure value has a value or initialized.You can use Log or System.out.println(value); after .putExtra to see(in console tab) if value is null. and in second activity too.
Change this line
String value = getIntent().getStringExtra("name");
TO this line
String value = getIntent().getString("name");
I have discovered I have an issue in my code it was my fault. It wasn't an Intent problem. Thank you all.
You can try this -->
intent.putExtra("name", textView.getText().toString());
If the error still occurs, check-in the Second Activity that you are using the right id path...:-
value = findViewById(R.id.);

How to add a string in string.xml using Java code, in Android?

I have created two EditText objects in the Java code of My android application, as
final EditText et1=(EditText)findViewById(R.id.editText1);
final EditText et2=(EditText)findViewById(R.id.editText2);
then on the onClick() event of a button, called a method with parameters as-
addStringToXmlFile(et1.getText(),et2.getText());
now in the definition of this method below, i have written-
private void addStringToXmlFile(Editable editable1,Editable editable2){
String s1=new String();
s1=editable1.toString();
String s2=new String();
s2=editable2.toString();
}
The problem is that, now i want to use these two String objects s1,s2, to make two entries in the res/values/Strings.xml file of the database, & I don't know how to do it.
please guide me further.
This is not possible - an app's apk (including it's resources) can not be changed at runtime. I'm not entirely sure of all of the reasons why, but one obvious thing I can think of is that R.java needs to contain a reference to your String in order for you to access it, and this file is generated by the compiler when you create the APK.
If you need to persist a String across sessions, you should look into using one of several data storage mechanisms that Android provides, such as SharedPreferences.
Take a look into using SharedPreferences to store your string values instead.
//Saving your strings
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("s1", s1);
editor.putString("s2", s2);
editor.commit();
//retrieving your strings from preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
String s1 = prefs.getString("s1", ""); //empty string is the default value
String s2 = prefs.getString("s2", ""); //empty string is the default value

Categories