Ok, thanks to below dude biggest issue is fixed. But whatever I print out, none is printed and I cannot print out the message what is typed in class 2 (nimekysija) as well :(. I really need that it stores name and in future it will write down name every time! Thanks for your help!
Problem must be in 2nd class tho. When I update editor.putString("nimi2", nimiS); nimiS into "plapla", then plapla actually shows up :/. So I have really no idea, what is problem!
(updated below classes too to the newest)
Class 1:
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class MainStuff extends Activity {
TextView tere;
String nimi;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
tere = (TextView) findViewById(R.id.tvTere);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
if (nimiOlemas == false){
startActivity(new Intent("viimane.voimalus.NIMEKYSIJA"));
finish();
}
if (nimiOlemas == true){
nimi = preferences.getString("nimi2", "");
System.out.print("töötab!");
tere.setText("Tere " + nimi);
}
System.out.print("töötab2!");
}
}
CLASS 2
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class nimekysija extends Activity {
EditText nimi;
SharedPreferences preferences;
String nimiS;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.nimekysija);
preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
nimi = (EditText) findViewById(R.id.etNimekysija);
nimiS = nimi.getText().toString();
Button kysOk = (Button) findViewById(R.id.bNimekysija);
kysOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences.Editor editor = preferences.edit();
editor.putString("nimi2", nimiS); // nime kirjutamine
editor.putBoolean("nimionolemas", true); // nimi on kirjutatud!
editor.commit();
startActivity(new Intent("viimane.voimalus.MAINSTUFF"));
finish();
}
});
}
}
Ok I'm guessing you may be new to Java, forgive me if I'm incorrect. You never READ from nimiOlemas.
boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
nimiOlemas = false;
startActivity(new Intent("viimane.voimalus.NIMEKYSIJA"));
finish();
nimiOlemas = true;
I think what you are trying to do is initialize nimiOlemas and then, if it is false, start an activity, call finish, then set nimiOlemas to true, but this is not what you are doing. Is this what you want?
boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
if (nimiOlemas == false)
{
startActivity(new Intent("viimane.voimalus.NIMEKYSIJA"));
finish();
nimiOlemas = true;
}
= is an assignment, == is a boolean comparison. You say in your question that you check the value of your boolean, but you never do, you only assign to it.
Assuming that nimiOlemas is inherited from activity and not used in the activity class, or other supper class, then yes it is not used in nimekysija (Class 2). It IS used in class 1. But this should just be a warning... You will get this warning for every class that extends activity and doesn't use nimiOlemas.
Related
Can I ask if someone knows how to pass the data entered from one activity to another activity? The string to be passed to that activity will be compared to the images in the database of the Android phone..
Here is the code sirs:
AlphabetConversion.java
package com.mobilebasedsignlanguage;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class AlphabetConversionMenu extends Activity {
EditText et_conv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphaconv);
Button alphaConv = (Button)findViewById(R.id.btn_alphaconv);
et_conv = (EditText)findViewById(R.id.et_convtxt);
alphaConv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String checkWord = et_conv.getText().toString();
Bundle word = new Bundle();
word.putString("key", checkWord);
Intent a = new Intent(AlphabetConversionMenu.this, AlphabetCompareClass.class);
a.putExtras(word);
startActivity(a);
}
});
}
}
Here is the java for the second activity
package com.mobilebasedsignlanguage;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AlphabetCompareClass extends Activity {
String get;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphabetcompare);
Bundle gotWord = getIntent().getExtras();
get = gotWord.getString("key");
TextView Word = (TextView)findViewById(R.id.textView1);
Word.setText(get);
for(int x = 0; x > gotWord.getString("key").length(); x++){
//if(gotWord.getString("key").charAt(0) == "SELECT * from " + TABLE_CONTACTS)
// Get Image from the Database
// Set it on Image View
// set timer 2 secs
}
};
}
The comments in the second activity I think I don't know how to code that. Really need someones help. Thank you very much.
Did you try to save information on the shared preferences?
Allows you you to save "key-value".
http://developer.android.com/reference/android/content/SharedPreferences.html
try this on AlphabetConversion.java
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String checkWord = et_conv.getText().toString();
Intent a = new Intent(AlphabetConversionMenu.this, AlphabetCompareClass.class);
a.putExtra("key",checkWord);
a.putExtras(word);
startActivity(a);
}
and in the onCreate of second activity
Bundle b;
b = this.getIntent().getExtras();
mssg = b.getString("key");
Select Statement will return the resultset object. If you want to compare it to the specific column then fetch the column name only. For comparing the string objects you can use String.compare method or simple == will be sufficient.
First, you should put it into extras in the first activity:
Intent intent = new Intent(this, AlphabetCompareClass.class);
intent.putExtra("STRING_KEY", yourString);
In the second activity you can take it from extras:
String yourString = getIntent().getExtras().getString("STRING_KEY");
Or you can use shared preferences to store information. It's more convenient if you should save info for more than one activity change.
In your first activity call like this :
Intent a = new Intent(AlphabetConversionMenu.this, AlphabetCompareClass.class);
a.putExtras("key", checkWord);
startActivity(a);
And in second activity get value like this
get = getIntent().getStringExtra("key",""); // here if no extra found then default "" return.
I'm working on an app and would like to save the state of obe of my checkbox options, but when I try using the SharedPreferences class (first time using it) I get a nullpointer exception. I have been troubleshooting for a couple hours and cant find a solution. Could anyone look at this and tell me what's wrong? The full code is alot longer but this is the park giving me the null pointer exception. I know for a fact it has to do with SharedPreferences.
package us.mattmccoy.meanfind;
//import java.util.Arrays;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
//saved data stuff needed
SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
Editor edit = preferences.edit();
//private data using saved data
String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR";
boolean autoclear = preferences.getBoolean(autoclearKEY, false);
//normal private data
final Context con1 = this;
int dividend;String dataFixed;boolean tb;
private CheckBox myCBox, acBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerBoxes();
if(autoclear == true){
acBox.setChecked(true);
}else{
acBox.setChecked(false);
}
}
//check box listeners
public void addListenerBoxes(){
//instantiate boxes
acBox = (CheckBox) findViewById(R.id.chex2);
acBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//is acBox checked?
if (((CheckBox) v).isChecked()) {
edit.putBoolean(autoclearKEY, true).commit();
}else{
edit.putBoolean(autoclearKEY, false).commit();
}
}
});
myCBox = (CheckBox) findViewById(R.id.chex);
}
error message:
http://pastebin.com/5P2Mfwik
Change this line
SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
to
SharedPreferences preferences; = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
and put
preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
in onCreate()
You are trying to use Context before establishing it with onCreate() so you are getting NPE on context. You will also want to move these lines
Editor edit = preferences.edit();
boolean autoclear = preferences.getBoolean(autoclearKEY, false);
into onCreate() after you initialize preferences or you will get another NPE since preferences will be null until you initialize it. So it should be something like
public class MainActivity extends Activity {
//saved data stuff needed
SharedPreferences preferences; //declare them
Editor edit;
//private data using saved data
String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR";
boolean autoclear;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences; = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
Editor edit = preferences.edit(); //initialize them
boolean autoclear = preferences.getBoolean(autoclearKEY, false);
addListenerBoxes();
if(autoclear == true){
acBox.setChecked(true);
}else{
acBox.setChecked(false);
}
If this doesn't solve your problem then please post full logcat
I want to make password protected android app, but when in this simple program system is not matching two strings.
package com.pokmgr;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainPM extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pm_layout);
final EditText pin = (EditText) findViewById(R.id.pinET);
final String pass = pin.getText().toString();
final String code = "ajaj";
Button enter = (Button) findViewById(R.id.enterBtn);
enter.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (pass.equals(code)) {
Intent in = new Intent(MainPM.this, Menu.class);
startActivity(in);
}
else {
Intent in = new Intent(MainPM.this, Menu2.class);
startActivity(in);
}
}
});
}
/*#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.pm_layout, menu);
return true;
}*/
}
I have made Menu and Menu2 class, but every time Menu2 class is accessed. Even if I enter same pass that is "ajaj" [in this code to test]
i have defined both activities in manifest file.
Can't understand why pass.eqals(code) is not working
The problem is that you are setting pass to the contents of the EditText when the activity gets created. Instead you have to retrieve the contents of your EditText inside the OnClickListener.
Like this:
public void onClick(View v) {
final String pass = pin.getText().toString();
if (pass.equals(code)) {
// do something
} else {
// do something different
}
}
Put pin.getText().toString(); inside onClick of button. You are setting variable pass before the user actually entered something in pinEt EditText.
My question is if it is possible to write code before setContentView() in the onCreate() method of the main Activity. In the code below I want to call the setVariables() before setContentView() but this causes my application to crash. If I call setVariables() after setContentView(), it works fine. Why is this?
package com.oxinos.android.moc;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
public class mocActivity extends Activity {
/** Called when the activity is first created. */
public static String prefsFile = "mocPrefs";
SharedPreferences mocPrefs;
public Resources res;
public CheckBox cafesCB, barsRestCB, clothingCB, groceriesCB, miscCB;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVariables();
setContentView(R.layout.main);
mocPrefs = getSharedPreferences(prefsFile,0);
}
private void setVariables(){
res = getResources();
cafesCB = (CheckBox) findViewById(R.id.cafesCheckBox);
barsRestCB = (CheckBox) findViewById(R.id.barsRestCheckBox);
clothingCB = (CheckBox) findViewById(R.id.clothingCheckBox);
groceriesCB = (CheckBox) findViewById(R.id.groceriesCheckBox);
miscCB = (CheckBox) findViewById(R.id.miscCheckBox);
}
public void submitHandler(View view){
switch (view.getId()) {
case R.id.submitButton:
boolean cafes = cafesCB.isChecked();
boolean barsRest = barsRestCB.isChecked();
boolean clothing = clothingCB.isChecked();
boolean groceries = groceriesCB.isChecked();
boolean misc = miscCB.isChecked();
SharedPreferences.Editor editor = mocPrefs.edit();
editor.putBoolean(res.getString(R.string.cafesBool), cafes);
editor.putBoolean(res.getString(R.string.barsRestBool), barsRest);
editor.putBoolean(res.getString(R.string.clothingBool), clothing);
editor.putBoolean(res.getString(R.string.groceriesBool), groceries);
editor.putBoolean(res.getString(R.string.miscBool), misc);
editor.commit();
startActivity(new Intent(this, mocActivity2.class));
break;
}
}
}
You can execute any code you want before the setContentView() method as long as it doesn't refer to (parts of) the View, which isn't set yet.
Since your setVariables() method refers to the contents of the View, it can't be executed.
The setContentView() method sets the content of your XML file as the View, which is shown by the Activity.
You're calling setVariables() before you've specified any View to be shown.
That's why the error raises. The compiler doesn't know where that View belongs to. If you want to use a ResourceView, you have to set it first.
What I want is that when Activity starts first time, it first checks if nimiolemas is true. Since it just starts, then it can't be true. So, it will automatically starts new activity and also asks to get me info. In activity 2 person can type their name and when they press Ok, info will be sent back to activity one. Now, I don't know really how to change there Boolean to true and send that as well, so for now I told to change nimiolemas true before launching activity 2.
After pressing ok, it sends back to activity one and does the check again. Since it should be now true and also able to retrieve information about persons name, then it will go to true condition and print that name on screen in first activity. Now whenever program is launched, it will skip asking name and will straight show the person name :).
But it doesn't work exactly as I want. Before i put boolean, it actually went to second activity, but I couldn't get data so well. I have been working on solution for too long and I really appreciate help. If I find mistakes I can study from that more then searching on solution all over internet for next 10 hours :(.
I might have made some things very wrong, so please let me know and teach me! I really want to get better in this! So far I have done:
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainStuff extends Activity {
String tyybinimi;
TextView tere;
Boolean nimiolemas;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
tere = (TextView) findViewById(R.id.TERE);
Intent i = new Intent(this, nimekysija.class);
tyybinimi = i.getStringExtra("nimi");
if (nimiolemas = true) {
System.out.print(tyybinimi);
} else {
startActivity(i);
nimiolemas = true;
finish();
}
}
}
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class nimekysija extends Activity {
Intent resultIntent;
EditText nimi;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.nimekysija);
Button kysOk = (Button) findViewById(R.id.bNimekysija);
nimi = (EditText) findViewById(R.id.etNimekysija);
kysOk.setOnClickListener(new View.OnClickListener() {
String nimiS = nimi.getText().toString();
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.putExtra("nimi", nimiS);
startActivity(new Intent("viimane.voimalus.MAIN"));
finish();
}
});
}
}
Instead of using a boolean, you should be using Shared Preferences.