How to debug an Android app force close error? - java

public class MainActivity extends Activity {
EditText oETextN;
EditText oETextPh;
Button oButtonSave;
public String givenName;
public String givenPhNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewsById();
oButtonSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
givenName = oETextN.getText().toString();
givenPhNum = oETextPh.getText().toString();
OrgiOperations.Insert2Contacts(getApplicationContext(),
givenName, givenPhNum);
if (OrgiOperations.isTheNumberExistsinContacts(
getApplicationContext(), givenPhNum)) {
Log.i(OrgiOperations.TAG, "Exists");
} else {
Log.i(OrgiOperations.TAG, "Not Exists");
}
}
});
setContentView(R.layout.activity_main);
}
private void findViewsById(){
oButtonSave = (Button) findViewById(R.id.SaveBtn);
oETextN = (EditText) findViewById(R.id.FLName);
oETextPh = (EditText) findViewById(R.id.Ph);
//oEText = (EditText) findViewById(R.id.Name);
}
#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;
}
}
I've been trying to make a simple Android app that saves the entered details (Name and Phone Number) directly to your device phonebook. This is the MAIN activity, the other class includes the functions to do this, which I'm sure are implemented correctly.
The problem rises when I run the application on my phone, and unluckily after hours of cold coding I get the FORCE CLOSE error!

Change to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
Then
private void initializeViews(){
oButtonSave = (Button) findViewById(R.id.SaveBtn);
oETextN = (EditText) findViewById(R.id.FLName);
oETextPh = (EditText) findViewById(R.id.Ph);
//oEText = (EditText) findViewById(R.id.Name);
}
You need to set the content of the layout first to the activity then initialize views.
Also rename findViewsById(); to initializeViews() or something more appropriate coz you initialize your views with findViewById as oButtonSave = (Button) findViewById(R.id.SaveBtn);
Edit:
Have the click listener after initialization of view.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oButtonSave = (Button) findViewById(R.id.SaveBtn);
oETextN = (EditText) findViewById(R.id.FLName);
oETextPh = (EditText) findViewById(R.id.Ph);
oButtonSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
givenName = oETextN.getText().toString();
givenPhNum = oETextPh.getText().toString();
OrgiOperations.Insert2Contacts(getApplicationContext(),
givenName, givenPhNum);
if (OrgiOperations.isTheNumberExistsinContacts(
getApplicationContext(), givenPhNum)) {
Log.i(OrgiOperations.TAG, "Exists");
} else {
Log.i(OrgiOperations.TAG, "Not Exists");
}
}
});
}

Related

Opening next Activity

I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.

Want to display stored name/value data in intent to new activity screen

I am semi-ok with the understanding of how you store and retrieve data through intents, what I am not sure about however is the code implementation. What I want to accomplish is to pass stored data (name/value pairs and a bundle object) to my ThirdActivity class from MainActivity class. Here is a snippet what i have attempted thus far (which when the user clicks on the a3button it just displays a blank page with no string):
MainActivity from which I want to store data to send to ThirdActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a2_button = (Button) findViewById(R.id.a2_button);
a2_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a2intent = new Intent("com.tester.lab.x");
startActivityForResult(a2intent, 1);
}
});
Button a3_button = (Button) findViewById(R.id.a3_button);
a3_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a3intent = new Intent(v.getContext(), ThirdActivity.class);
a3intent.putExtra("greeting","my Name");
startActivity(a3intent);
}
});
}
}
ThirdActivity:
public class ThirdActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
getIntent().getStringExtra("greeting");
}
}
How would I display the string "my Name" once the user clicks ThirdActivity button? Any help is wonderful, thanks!
Everything is good, just add String type for value that you are getting and use this String as you want (to show in TextView else..):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
Or by using Button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
Button three = (Button) findViewById(R.id.your_button_id);
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
});
}

equals() is not working

package com.example.life.bluebell;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123"))
{
System.out.println("asdfg");
}
}
i am getting error at
if(et.equals("ab123"))
'equals' is shown as error with cannot resolve symbol type
i have tried comparing some other predeclared string instead of 'et' but still it is generating same error
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
init();
}
private void init() {
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123")) {
System.out.println("asdfg");
}
}
}
Write the following inside your OnCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("sss"))
{
System.out.println("asdfg");
}
}
Use the comparison code inside the method scope of onCreate(). In java, it is not allowed to do operations outside a method or an instance block or a static block inside a class.
Modify your own code like:
package com.example.life.bluebell;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123"))
{
Log.d("Myactivity","asdfg");
//system out will not be of much use and will only print on console. Use log instead to see output in Logcat
}
}
}
Try this , I am sure this will work
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123")) {
System.out.println("asdfg");
}
}
}
try to put this code in some method like
public void getData(){
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123"))
{
System.out.println("asdfg");
}
}
and dont call this getData() in onCreate(),
call it in some Button click action like follows
cButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getData();
}
});
your complete code would be like
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button cButton = (Button) findViewById(R.id.custom_button);
cButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getData();
}
});
}
public void getData() {
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123")) {
System.out.println("asdfg");
}
}
}
First u have to check the reference editText is correct or not, then after just follow the below codes,
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
checkValue();
}
public void checkValue()
{
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString().trim();
if(et.contentEquals("ab123")) // if(et.equalsIgnoreCase("ab123"))
{
System.out.println("asdfg");
}
}
}
It will help u.

unable to match edittext with image name

Learning android by making a simple guessing game, I have set the images in an Arraylist and applied random operation on it, but now I'm confused how to match edit text value with image name so that I can set score.
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Integer> list = new ArrayList<Integer>();
ImageView random_image = (ImageView) findViewById(R.id.imageView);
final int[] images = {R.drawable.kung_fu_panda, R.drawable.mr_nobody, R.drawable.toy_story};
list.add(R.drawable.kung_fu_panda);
list.add(R.drawable.mr_nobody);
list.add(R.drawable.toy_story);
int position = new Random().nextInt(list.size());
random_image.setImageResource((Integer) list.get(position));
list.remove(position);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validateInput();
}
});
}
private void validateInput() {
String input = editText.getText().toString();
}
You can try definition name's array of images, and get the name by random position, and use currentName save the name.
String currentName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Integer> list = new ArrayList<Integer>();
ImageView random_image = (ImageView) findViewById(R.id.imageView);
final String imageNames = {"kung fu panda", "mr nobody", "toy story"};
list.add(R.drawable.kung_fu_panda);
list.add(R.drawable.mr_nobody);
list.add(R.drawable.toy_story);
int position = new Random().nextInt(list.size());
random_image.setImageResource((Integer) list.get(position));
currentName = imageNames[position];
list.remove(position);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validateInput();
}
});
}
Then, in your validateInput() method, you can use currentName to compare, notice case, use String.toLowerCase() to format input string.
private boolean validateInput() {
String input = editText.getText().toString();
if(!TextUtils.isEmpty(input)){
input = input..toLowerCase();
if(input.equeals(currentName)){
return true;
}
}
return false;
}

Transfer data from EditText to TextView of another activity using setText and onClickListener?

I am a newbie.. I am trying to get text on another activity which I have created, from an activity created apriori. I could do this using onClickListener, but I am unable to get it done using onClick and setText(). The code runs well until the second activity, but when I type and send button, the code crashes. Help me!
Here's the XML file:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#+id/tv1"
android:text="#string/hello_world" />
Here's the Java file:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
DisplayMessageActivity.class);
EditText et = (EditText) findViewById(R.id.et1);
String message = et.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
#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 code can help you
MainActivity.java
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et=(EditText) findViewById(R.id.editText2);
Button bt=(Button) findViewById(R.id.button2);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(Main.this,DisplayMessageActivity.class);
intent.putExtra("theText", et.getText().toString());
startActivity(intent);
}
});
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.theme.R.layout.DisplayActivity);
TextView tv=(TextView) findViewById(com.example.theme.R.id.textView2);
tv.setText(getIntent().getExtras().getString("theText"));
}
Replace
android:layout="#+id/tv1"
with
android:id="#+id/tv1"

Categories

Resources