How to stay in same activity after clicking button in android? - java

I am trying to develop simple feedback application. When user enters invalid data it should show error. After error is detected all fields should be clear and I should stay on same activity What should I do?Here's my code:
package com.example.feedback;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Feedback extends Activity {
String s;
boolean fill=true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feedback);
final Button bt1 = (Button) findViewById(R.id.bt1);
final EditText tv2 = (EditText) findViewById(R.id.tv2);
final EditText tv1 = (EditText) findViewById(R.id.tv1);
final EditText tv3 = (EditText) findViewById(R.id.tv3);
final EditText tv4 = (EditText) findViewById(R.id.tv4);
final RadioGroup rg = (RadioGroup) findViewById(R.id.rg1);
bt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
s = tv1.getText().toString();
check();
s = tv2.getText().toString();
check();
s = tv3.getText().toString();
check();
s = tv4.getText().toString();
check();
if (rg.getCheckedRadioButtonId() == -1){
Toast.makeText(Feedback.this,"Error",Toast.LENGTH_LONG).show();
}
Toast.makeText(Feedback.this,"Press again to Submit",Toast.LENGTH_LONG).show();
bt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(Feedback.this,Feedback2.class));
// TODO Auto-generated method stub
}
});
// TODO Auto-generated method stub
}
private void check() {
if(s.matches("")){
Toast.makeText(Feedback.this,"Error",Toast.LENGTH_LONG).show();
}
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.feedback, menu);
return true;
}
}

this is the line:
startActivity(new Intent(Feedback.this,Feedback2.class));
after the click, change activity.
I suggest you to check login success in onActivityResult method and if it's true than start new activity

Related

Android camera application. Two activities

I am trying to deveolp an Android application. I am learning Android so there I go.
My aplication is similar to this one: http://www.tutorialspoint.com/android/android_camera.htm ;
Except I have two activities.. One is called Report.java and the other one is called Form.java.
In Report.java, I have a button, when I click the button, I want to open the camera and take a picture. After I took the picture, I want to start the second activity (Form.java) and my image to be saved in an ImageView.
How can I do that? Thank you in advance.
Report.java
package com.example.harta;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Report extends Activity implements View.OnClickListener{
ImageView iv;
Button report;
final static int cameraData = 0;
Bitmap bmp;
Intent i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
initialize();
report.setOnClickListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
report = (Button) findViewById(R.id.bReport);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bReport:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
Intent i = new Intent(Report.this, Formular.class);
startActivity(i);
}
}
}
Form.java
package com.example.harta;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
public class Formular extends Activity{
Button button;
ImageView iv;
int cameraResults;
final static int cameraData = 0;
Bitmap bmp;
TextView tvAdress, tvAmpers, tvCommunication, tvCoordinates;
TextView tvName, tvInformations, tvDataIntro, tvPhone, tvLatitude;
TextView tvLongitude, tvInstalledPower, tvContor, tvNetwork;
EditText etAdress, etAmpers, etCommunication, etCoordinates;
EditText etName, etInformations, etDataIntro, etPhone, etLatitude;
EditText etLongitude, etInstalledPower, etContor, etNetwork;
Spinner spinNetwork;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.formular);
initialize();
}
private void initialize() {
// TODO Auto-generated method stub
tvAdress = (TextView) findViewById(R.id.tvAdresa);
tvAmpers = (TextView) findViewById(R.id.tvAmperaj);
tvCommunication = (TextView) findViewById(R.id.tvComunicatie);
tvCoordinates = (TextView) findViewById(R.id.tvCoord);
tvName = (TextView) findViewById(R.id.tvDenumire);
tvInformations = (TextView) findViewById(R.id.tvInfo);
tvDataIntro = (TextView) findViewById(R.id.tvIntroducereDate);
tvPhone = (TextView) findViewById(R.id.tvIPtelefon);
tvLatitude = (TextView) findViewById(R.id.tvLat);
tvLongitude = (TextView) findViewById(R.id.tvLong);
tvInstalledPower = (TextView) findViewById(R.id.tvPutereInstalata);
tvContor = (TextView) findViewById(R.id.tvSerieContor);
tvNetwork = (TextView) findViewById(R.id.tvTipulRetelei);
etAdress = (EditText) findViewById(R.id.etAdresa);
etAmpers = (EditText) findViewById(R.id.etAmperaj);
etCommunication = (EditText) findViewById(R.id.etComunicatie);
etName = (EditText) findViewById(R.id.etDenumire);
etPhone = (EditText) findViewById(R.id.etIPtelefon);
etInstalledPower = (EditText) findViewById(R.id.etPutereInstalata);
etContor = (EditText) findViewById(R.id.etSerieContor);
spinNetwork = (Spinner) findViewById(R.id.spinTipulRetelei);
button = (Button) findViewById(R.id.bTrimitere);
iv = (ImageView) findViewById(R.id.ivPic);
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}

Android ( Intent )

i have these peace of code taht is written by eclipse and which gives me no syntax error
but when i run it , it says intent_ has stopped working
Here is my code:
package com.example.intent_;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView t1, t2, t3;
EditText tf1;
Button b1, b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this, "Welcome to On create", Toast.LENGTH_LONG).show();
b1 = (Button) findViewById(R.id.button1);
t1 = (TextView) findViewById(R.id.textView1);
t2 = (TextView) findViewById(R.id.textView2);
t3 = (TextView) findViewById(R.id.textView3);
b1 = (Button) findViewById(R.id.button1);
tf1 = (EditText) findViewById(R.id.editText1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t1.setText("Abed-Almalik");
t2.setText("Jorda-jerash");
t3.setText("2010901092");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Please help me solve the problem
Thank you !
My guess is that b2 wasn't defined.
so b2.setOnClickListener will give you a null pointer error.
Further to Saba's answer - you have this twice
b1 = (Button) findViewById(R.id.button1);
The second one should probably be b2 = ...
You haven't any button b2 for b2.setOnClickListener
//running code is below
package com.example.intent_;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView t1, t2, t3;
EditText tf1;
Button b1, b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this, "Welcome to On create", Toast.LENGTH_LONG).show();
b1 = (Button) findViewById(R.id.button1);
t1 = (TextView) findViewById(R.id.textView1);
t2 = (TextView) findViewById(R.id.textView2);
t3 = (TextView) findViewById(R.id.textView3);
b2 = (Button) findViewById(R.id.button2); //here change it to b2 and button2
tf1 = (EditText) findViewById(R.id.editText1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t1.setText("Abed-Almalik");
t2.setText("Jorda-jerash");
t3.setText("2010901092");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

how to use content provider to access shared preferences

i have a registration app in the making where the user registers the data and on the other end the data can be shown and viewed and manipulated , i am using shared preferences method as shown below to write and access data (you can see them in DataEntry.java & DataManipulation.java shown below) but i want to use content provider method to manipulate data like quering or inserting or deleting....is there a way to do this and if yes can you please tell me how?
thank you
here are the codes below
the User end DataEntry.java:
package com.hossa.datamanipulation;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class DataEntry extends Activity {
/*
* here is the part the user will submit several data for several people
*/
Button Submit, BroadcastButton;
TextView User, Email, Mobile;
EditText UserEdit, EmailEdit, MobileEdit;
String UserValue, EmailValue, MobileValue;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dataentry);
Submit = (Button) findViewById(R.id.DataSubmit);
BroadcastButton = (Button) findViewById(R.id.Broadcast);
User = (TextView) findViewById(R.id.TVuser);
Email = (TextView) findViewById(R.id.TVemail);
Mobile = (TextView) findViewById(R.id.TVmobile);
UserEdit = (EditText) findViewById(R.id.Edituser);
EmailEdit = (EditText) findViewById(R.id.Editemail);
MobileEdit = (EditText) findViewById(R.id.Editmobile);
Submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// saving the data of the user//
SharedPreferences sp = getSharedPreferences("userdata",
MODE_PRIVATE);// name of the file and it's mode//
SharedPreferences.Editor edit = sp.edit();// to be able to edit
// the file//
edit.putString("Username", UserEdit.getText().toString());// get
// the
// input
// username//
edit.putString("Email", EmailEdit.getText().toString());// get
// the
// input
// Email//
edit.putString("Mobile", MobileEdit.getText().toString());// get
// the
// input
// Mobile//
edit.commit();
Toast.makeText(getApplicationContext(),
"Data is Saved Successfully", Toast.LENGTH_SHORT)
.show();
}
});
BroadcastButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent e = new Intent();
e.setAction("com.hossa.data" + "manipulation.custombroadcast");
sendBroadcast(e);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and the other end DataManipulaton.java:
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class DataManipulation extends Activity {
Button ImportButton, ViewButton, RetrieveButton;
EditText UserShow;
TextView EmailDisplay, UserDisplay, MobileDisplay, EmailTV, UserTV,
MobileTV;
public static final String defaultvalue = "N/A";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datamanipulation);
ContentResolver ct = getContentResolver();
String Path = "data/data/com.hossa.datamanipulation/files/userdata";
Uri uri = Uri.parse("content://" + Path);
ImportButton = (Button) findViewById(R.id.ImportButton);
ViewButton = (Button) findViewById(R.id.ViewButton);
RetrieveButton = (Button) findViewById(R.id.RetrieveButton);
EmailDisplay = (TextView) findViewById(R.id.EmailDisplay);
MobileDisplay = (TextView) findViewById(R.id.MobileDisplay);
UserDisplay = (TextView) findViewById(R.id.UsernameDisplay);
EmailTV = (TextView) findViewById(R.id.EmailTV2);
UserTV = (TextView) findViewById(R.id.UserTV2);
MobileTV = (TextView) findViewById(R.id.MobileTV2);
final String U1[] = new String[10];
final String E1[] = new String[10];
final String M1[] = new String[10];
final String[][] alldata = new String[][] { U1, M1, E1 };
RetrieveButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int i = 0;
SharedPreferences spdata = getSharedPreferences("userdata",
Context.MODE_PRIVATE);
while (spdata.getString("Username", defaultvalue) != null) {
String tempuser = spdata
.getString("Username", defaultvalue);
U1[i] = tempuser;
i++;
}
i = 0;
while (spdata.getString("Email", defaultvalue) != null) {
String tempemail = spdata.getString("Email", defaultvalue);
E1[i] = tempemail;
i++;
}
i = 0;
while (spdata.getString("Mobile", defaultvalue) != null) {
String tempmobile = spdata
.getString("Mobile", defaultvalue);
M1[i] = tempmobile;
i++;
}
i = 0;
}
});
ImportButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getApplicationContext();
// TODO Auto-generated method stub
SharedPreferences sp = getSharedPreferences("userdata",
Context.MODE_PRIVATE);
String Username = sp.getString("Username", defaultvalue);
String Email = sp.getString("Email", defaultvalue);
String Mobile = sp.getString("Mobile", defaultvalue);
// what if there is no data from the user??//
if (Username.equals(defaultvalue) || Email.equals(defaultvalue)
|| Mobile.equals(defaultvalue)) {
Toast.makeText(getApplicationContext(),
"no data has been entered", Toast.LENGTH_SHORT)
.show();
} else {
UserTV.setText(Username);
EmailTV.setText(Email);
MobileTV.setText(Mobile);
}
}
});
//THE BELOW IS THE SECTION I NEED HELP WITH...THE ABOVE IS SIMPLY SOME TRIALS AND TESTS//
Cursor c = ct.query(uri, U1, null, null, null);
// c=ct.delete(uri, where, selectionArgs);
// c=ct.update(uri, values, where, selectionArgs);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
PLEASE NOTE THAT BOTH CLASSES ARE IN THE SAME PACKAGE SO IT IS THE SAME APP NOT 2 DIFFERENT APPS

Basic android debugging [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I seem to get at force close when i start the app on my Nexsus 4. Big possibility its just me that fools around but i can't find anything wrong with the code.
Android Studio gives no erros. The reset method that is commented out i supposed to reset the checkbox and the TextView.
Why can't i define items before onCreate ? And when i define them after i cannot use them in the "reset" method..
Could someone plese have a look ?
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
final TextView txtTest = (TextView) findViewById(R.id.txtOut);
final CheckBox chkTest = (CheckBox) findViewById(R.id.chkTest);
final Button btnTest = (Button) findViewById(R.id.btnTest);
final EditText etTest = (EditText) findViewById(R.id.etTest);
String text = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chkTest.setChecked(true);
chkTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (chkTest.isChecked()) {
txtTest.setText("Checked :)");
} else txtTest.setText("Not Checked");
}
});
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//etTest.getText().toString(text);
text = etTest.toString();
// Reset(text);
}
});
/*void Reset(String text) {
chkTest.setChecked(false);
txtTest.setText(text);
}*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is the logcat error:
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{[...].MyActivity}: java.lang.NullPointerException
You can not initialize the item in this way! The layout is not inflated yet when is executed the code final TextView txtTest = (TextView) findViewById(R.id.txtOut); so you have a null pointer exception. Also, define the reset() method outside the onCreate().
See the codes below:
TextView txtTest = null;
CheckBox chkTest = null;
Button btnTest = null;
EditText etTest = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtTest = (TextView) findViewById(R.id.txtOut);
chkTest = (CheckBox) findViewById(R.id.chkTest);
btnTest = (Button) findViewById(R.id.btnTest);
etTest = (EditText) findViewById(R.id.etTest);
....
}
void reset(String text) {
chkTest.setChecked(false);
txtTest.setText(text);
}
You can't find a view when is not created yet, try this:
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txtTest = null;
CheckBox chkTest = null;
Button btnTest = null;
EditText etTest = null;
String text = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtTest = (TextView) findViewById(R.id.txtOut);
chkTest = (CheckBox) findViewById(R.id.chkTest);
btnTest = (Button) findViewById(R.id.btnTest);
etTest = (EditText) findViewById(R.id.etTest);
chkTest.setChecked(true);
chkTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (chkTest.isChecked()) {
txtTest.setText("Checked :)");
} else txtTest.setText("Not Checked");
}
});
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//etTest.getText().toString(text);
text = etTest.toString();
// Reset(text);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void Reset(String text) {
chkTest.setChecked(false);
txtTest.setText(text);
}
}
You cant cause the items your are initializing is on the activity_main.xml and you set the content view after the initialization. Do the initialization in the onCreate().
Firstly you must define the Reset() function outside the onCreate(). Thats not the correct way.
Secondly, I observe you have just started out this so I would suggest to define the views in the xml and then act upon them. You can set the checkbox true initially in the xml and the later set to false in the code.

Generate random sentence on button press, code looks right but isnt working

I'm not sure what's wrong here, all i want to do is randomly grab an item from my array. Which is just like a random sentence. Then generate another once the button is pressed. All my code looks good to me but it's causing a crash when i hit the button. any ideas?
package com.my.package;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View.OnClickListener;
public class Randomsentence extends Activity implements OnClickListener{
private String[] myString;
private static final Random rgenerator = new Random();
private TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);
View nextButton = findViewById(R.id.next_button);
nextButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.next_button:
tv.setText(myString[rgenerator.nextInt(myString.length)]);
break;
}
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu3, menu);
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case R.id.menu:
startActivity(new Intent(this, Main.class));
return true;
case R.id.startnhie:
startActivity(new Intent(this, startnhie.class));
return true;
}
return false;
}
}
In your onCreate(), change
TextView tv = (TextView) findViewById(R.id.text1);
to
tv = (TextView) findViewById(R.id.text1);
since you already declared TextView tv as an instance variable.

Categories

Resources