restore savedInstanceState not match to stored - match

Why do the "savedInstanceState" I use
The following problem will happen?
When the program was first runing receive right message.
But when I select video of video gallery .
In 'savedInstanceState'
The "test" to save it
But when recovery was
Equal conditions 'test'
You can not run?
And orders in
'Else if (savedInstanceState.getString (" key ")! ="test")'
Was run , while the amount of 'savedInstanceState.getString (" key ")'
is The 'test' ?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
if (savedInstanceState.getString("key")=="test"){
TextView txtarr_1=(TextView) findViewById(R.id.test_array_1);
txtarr_1.setText("if(savedInstanceState.getString(key)==test){" + savedInstanceState.getString("key")+"}");
}
else if (savedInstanceState.getString("key")!="test") {
TextView txtarr_3=(TextView) findViewById(R.id.test_array_3);
txtarr_3.setText( savedInstanceState.getString("key"));
}
}else{
TextView txtarr_5=(TextView) findViewById(R.id.test_array_5);
txtarr_5.setText("elseif (savedInstanceState != null)");
}}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("key", "test");
super.onSaveInstanceState(savedInstanceState);
}

i change code to :: instead store a string -->store a integer
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
int val=savedInstanceState.getInt("key");
if (val==123){
TextView txtarr_1=(TextView) findViewById(R.id.test_array_1);
txtarr_1.setText("if == 123{" + savedInstanceState.getInt("key")+"}");
}
else if (val!=123) {
TextView txtarr_3=(TextView) findViewById(R.id.test_array_3);
txtarr_3.setText( savedInstanceState.getString("key"));
}
}else{
TextView txtarr_5=(TextView) findViewById(R.id.test_array_5);
txtarr_5.setText("elseif (savedInstanceState != null)");
}}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("key", putInt);
super.onSaveInstanceState(savedInstanceState);
}
In this case the problem can be solved
that's mean :
Was detected  I store 123 and orders Equal to 123  Was conducted
but if i store 123 to form of string
run orders not equal 123 execute
in call a new activity To a stored form
and in restore The stored value is different
But when I show exactly the same text??????
why?????

I found my mistake
for string in IF must use equal , not ==
if (savedInstanceState != null) {
String str =savedInstanceState.getString("key");
if ( str.equals("test")){
TextView txtarr_1=(TextView) findViewById(R.id.test_array_1);
txtarr_1.setText("if == test{" + str+"}");
}
else if (!str.equals("test")){
TextView txtarr_3=(TextView) findViewById(R.id.test_array_3);
txtarr_3.setText("if != test{" + str+"}");
}
}else{
TextView txtarr_5=(TextView) findViewById(R.id.test_array_5);
txtarr_5.setText("elseif (savedInstanceState != null)");
}

Related

Why is the beaconmanager just finding a beacon?

My problem is that the beacon manager just finds a beacon.
My code is below;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//location permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1234);
}
button = (Button) findViewById(R.id.b1);
//button onClick
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBeaconManager = BeaconManager.getInstanceForApplication(getApplicationContext());
mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT));
mBeaconManager.bind(MainActivity.this);
}
});
}
#Override
public void onBeaconServiceConnect() {
ArrayList<Identifier> identifiers = new ArrayList<>();
identifiers.add(null);
Region region = new Region("all-beacons-region", identifiers);
try {
mBeaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
mBeaconManager.addRangeNotifier(this);
}
//display beacon size
#Override
public void didRangeBeaconsInRegion(Collection beacons, Region region) { Log.i(TAG,"" + beacons.size()); }
}
The result beacon.size 1. But there are three beacons.
What should I do to find more than one beacon? I'll be happy if you can help me.

Second activity does not load in login form

I created a simple app of login form but it did not go to second activity after login. There is no error in the code. Will you please help me, here is the code:
public class MainActivity extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter=5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = (EditText)findViewById(R.id.editText);
Password = (EditText) findViewById(R.id.editText2);
Info = (TextView)findViewById(R.id.textView);
Login = (Button) findViewById(R.id.btn);
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validate(Name.getText().toString(), Password.getText().toString());
}
});
}
private void validate(String userName, String userPasswor) {
if ((userName == "admin") && (userPasswor == "1234")) {
Intent intent= new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
} else {
counter--;
Info.setText("No of Attempts Remaining: " + String.valueOf(counter));
if (counter == 0) {
Login.setEnabled(false);
}
}
}
}
You write your code
if((userName =="admin") && (userPasswor=="1234"))
{
Intent intent= new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
change this code as
if((userName.equals("admin")) && (userPasswor.equals("1234")))
{
Intent intent= new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
also enter Second activity in Android.mainfeast file.

issues Spotting the error

The error that i'm getting is "Multiple markers at this line
- Syntax error, insert "}" to complete
ClassBody
- Syntax error, insert ";" to complete
Statement
- Syntax error, insert ")" to complete
Expression"
still on the same line i've tried placing different ")" and"}" and they just make it worse even tried playing around with removing or moving them around still same issue ..cannot run cause of the error
My SignUp class,
public class SignUp extends Activity {
private EditText reg_fullname, reg_username, reg_email, reg_password, reg_confirmpassword;
private Button btnRegister;
private LoginDataBaseAdapter dataBaseAdapter;
private TextView loginScreen;
protected DataBaseHelper DB = new DataBaseHelper(SignUp.this);
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set View to register.xml
setContentView(R.layout.signup);
reg_fullname = (EditText) findViewById(R.id.reg_fullname);
reg_username = (EditText) findViewById(R.id.reg_username);
reg_email = (EditText) findViewById(R.id.reg_email);
reg_password = (EditText) findViewById(R.id.reg_password);
reg_confirmpassword = (EditText) findViewById(R.id.reg_confirmpassword);
loginScreen = (TextView) this.findViewById(R.id.link_to_login);
//Listening to Login Screen Link
btnRegister = (Button) findViewById(R.id.btnRegister);
loginScreen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.link_to_login:
startActivity(new Intent(SignUp.this, LoginScreen.class));
finish();
break;
case R.id.btnRegister:
// I think they have to be final to be used in the onClickListener
final String fullname = reg_fullname.getText().toString();
final String username = reg_username.getText().toString();
final String password = reg_password.getText().toString();
final String email = reg_email.getText().toString();
boolean invalid = false;
if(fullname.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Enter your Firstname", Toast.LENGTH_SHORT).show();
}
else
if(username.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Username", Toast.LENGTH_SHORT).show();
}
else
if(password.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Password", Toast.LENGTH_SHORT).show();
}
else
if(email.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Email ID", Toast.LENGTH_SHORT).show();
}
else
if(invalid == false)
{
addEntry(fullname, username, password, email);
Intent i_register = new Intent(SignUp.this, LoginScreen.class);
startActivity(i_register);
finish();
}
}
break;
} ****error appears here ***
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
try{
super.onDestroy();
dataBaseAdapter.close();
}catch(Exception e){
Log.e("onDestroy SignUp- Error", e.getMessage());
}}
private void addEntry(String fullname, String username, String password, String email)
{
SQLiteDatabase db = DB.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("fullname", fullname);
values.put("username", username);
values.put("password", password);
values.put("email", email);
try
{
db.insert(DataBaseHelper.DATABASE_TABLE_NAME, null, values);
Toast.makeText(getApplicationContext(), "your details submitted Successfully...", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}}
You need the following symbols after your break;:
...
break;
}
});
}
#Override
protected void onDestroy() {

File Upload in Android Webview - No File Chosen

I'm using the solution from Android WebView File Upload to have File Upload in Android Webview:
private WebView webView;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient(){
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Main.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);
}
public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
Main.this.startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Main.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), Main.FILECHOOSER_RESULTCODE );
}
});
webView.loadUrl( WEBSITE_URL );
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE){
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
The filechooser display but after selecting photo, the input file still display No File Chosen. What I am missing on my code?
Thanks.

Refresh ListView when Device receives GCM IntentService Message

My app is able to receive messages from GCM and saves the messages to the SQLlite database on the phone. The messages are viewable in a activity that has a listview.
In the code below, the onPause() function refreshes the listView. This is not a good implementation because it only works if the activity is not displayed at the time of the update. If the activity is displayed at the time of an update, the list is static and does not update.
Questions:
How to I update the listview when the activity is being displayed? Or is there a way to use a background service to update the adapter, so that whenever the activity is displayed, it always shows the newest data.
is this kind of functionality currently not possible with android and I'll need to implement something else like 'pull-to-refresh'?
refreshing listview in OnResume() crashes the application, and shows a null pointer exception.
Activity:
public class NotesView extends Activity implements OnItemClickListener {
ListView listView;
NoteAdapter objAdapter;
NotificationsDatabase db = new NotificationsDatabase(this);
List<Notes> listAlerts;
String note;
String time;
TextView noteView;
TextView timeView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
listView = (ListView) findViewById(R.id.notelist);
listView.setOnItemClickListener(this);
noteView = (TextView) findViewById(R.id.noteDisplay);
timeView = (TextView) findViewById(R.id.notetimeStampDisplay);
new MyTask().execute();
}
// My AsyncTask start...
class MyTask extends AsyncTask<Void, Void, List<Notes>> {
ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NotesView.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
if (isCancelled()) {
this.cancel(true);
}
}
#Override
protected List<Notes> doInBackground(Void... params) {
db.open();
listAlerts = db.getData();
if (isCancelled()) {
this.cancel(true);
}
return null;
}
protected void onPostExecute(List<Notes> alerts) {
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
db.close();
setAdapterToListview();
}
}// end myTask
public void setAdapterToListview() {
objAdapter = new NoteAdapter(NotesView.this, R.layout.row_notes, listAlerts);
objAdapter.sortByNoteDesc();
objAdapter.notifyDataSetChanged();
listView.setAdapter(objAdapter);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent intent = new Intent(
NotesView.this.getApplicationContext(),
TabBarExample.class);
intent.putExtra("goToTab", "Alerts");
startActivity(intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onItemClick(AdapterView<?> parent, View viewDel, int position,
long id) {
for (int i = 0; i < 1; i++) {
Notes item = listAlerts.get(position);
int ids = item.getId();
note = item.getNote();
time = item.getTimeStamp();
}
System.out.println(note + " " + time);
//
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
setContentView(R.layout.note);
listView = (ListView) findViewById(R.id.notelist);
listView.setAdapter(null);
listView.setOnItemClickListener(this);
noteView = (TextView) findViewById(R.id.noteDisplay);
timeView = (TextView) findViewById(R.id.notetimeStampDisplay);
new MyTask().execute();
}
#Override
protected void onDestroy() {
}
}
Code snippets From GCMIntentService
#Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
//String message = getString(R.string.gcm_message);
System.out.println("onMessage: ");
Bundle extras = intent.getExtras();
String message = extras.getString("message");
String event_id_from_server = extras.getString("server_id");
// displayMessage(context, message);
generateNotification(context, message);
saveMsg(message);
System.out.println("server id is " + event_id_from_server);
if (event_id_from_server != null) {
updateLocalDatabase(event_id_from_server);
}
}
public void saveMsg(String msg) {
boolean worked = true;
try {
NotificationsDatabase entry = new NotificationsDatabase(GCMIntentService.this);
entry.open();
java.util.Date date = new java.util.Date();
Timestamp x = new Timestamp(date.getTime());
String timeStamp = x.toLocaleString();
entry.createEntry(msg, timeStamp);
entry.close();
//update adapter service
} catch (Exception e) {
worked = false;
String error = e.toString();
System.out.println(error);
} finally {
if (worked) {
}
}
}
I cleaned up your code a little bit. Basically all the view assignments should be done once in onCreate, while the loading of the data should be done in onResume(). See if this helps:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
listView = (ListView) findViewById(R.id.notelist);
listView.setAdapter(null);
listView.setOnItemClickListener(this);
noteView = (TextView) findViewById(R.id.noteDisplay);
timeView = (TextView) findViewById(R.id.notetimeStampDisplay);
}
#Override
protected void onResume() {
super.onResume();
new MyTask().execute();
}
#Override
protected void onPause() {
super.onPause();
}