.setText() crashes my android app - java

I have tried many other questions on this site today but none of them have been helpful, i seem to have identical syntax that works for other people but every time i hit send (when i want my text fields to update) my app just crashes, I have tried a bunch of different syntax for the setText() method but nothing has worked.
Thanks for any help in advance
this is my main class
package com.example.myapp;
import android.app.Activity;
import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class TestprojectsActivity extends Activity {
private int index;
private int QsClicked = 0;
private ArrayList<String> categories = new ArrayList<String>();
private ArrayList<Button> buttons = new ArrayList<Button>();
Button b1;
Button b2;
Button b3;
Button b4;
Button changer;
private int Qselected;
TextView txt;
EditText edit;
private ArrayList<String> questions = new ArrayList<String>();
private int qnum = 0;
// updates all the buttons so we can see more categories
public void updateButtons(View v){
for(int i = 0; i < 4; i++){
if(index >= categories.size()){
index = 0;
}
buttons.get(i).setText(categories.get(index));
index++;
}
}
public void Q1(View v){
setContentView(R.layout.sub);
Qselected = index;
//send(null);
}
public void Q2(View v){
setContentView(R.layout.sub);
Qselected = (index+1);
//send(null);
}
public void Q3(View v){
setContentView(R.layout.sub);
Qselected = (index+2);
//send(null);
}
public void Q4(View v){
setContentView(R.layout.sub);
Qselected = (index+3);
//send(null);
}
public void send(View v){
edit.setText("WHY ISNT THIS WORKING?");
if(qnum < questions.size()){
txt.setText("i work!");
qnum++;
}
else{
qnum = 0;
System.exit(0);
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
index = 0;
b1 = (Button) findViewById(R.id.B1);
buttons.add(b1);
b2 = (Button) findViewById(R.id.B2);
buttons.add(b2);
b3 = (Button) findViewById(R.id.B3);
buttons.add(b3);
b4 = (Button) findViewById(R.id.B4);
buttons.add(b4);
txt = (TextView) findViewById(R.id.textView1);
edit = (EditText) findViewById(R.id.editText2);
categories.add("dumb questions");
categories.add("smart questions");
questions.add("hi there, what is your name?");
questions.add("what did you have for breakfast today?");
questions.add("how old are you?");
questions.add("what color is your hair?");
updateButtons(null);
}
}
this is my main xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutrowtop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/B4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B4"
android:onClick="Q4" />
<Button
android:id="#+id/B3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B3"
android:onClick="Q3" />
<Button
android:id="#+id/B2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B2"
android:onClick="Q2" />
<Button
android:id="#+id/B1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B1"
android:onClick="Q1" />
<Button
android:id="#+id/Button05"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="More Categories"
android:onClick="updateButtons" />
</LinearLayout>
this is my sub xml (i use it so i can have another layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.22"
android:text="Question"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.51"
android:ems="10"
android:inputType="text" />
<Button
android:id="#+id/send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.08"
android:text="Send"
android:onClick="send" />
</LinearLayout>

Your activity is using main.xml as its layout, and that does not have a TextView with id editText2. Just putting such a view in sub.xml doesn't create the view; the layout file needs to be inflated and set as the content view for the activity. Call findViewById after you change layouts and all should be well.

AS in when Oncreate get call main.xml was you layout xml and it has not R.id.editText2 so at that time edit would be null..........
Put this line
edit = (EditText) findViewById(R.id.editText2);
in
public void Q1(View v){
setContentView(R.layout.sub);
Qselected = index;
edit = (EditText) findViewById(R.id.editText2); <-----------------here in Q1 Q2 Q3 Q4
//send(null);
}

In the activity's onCreate method you set the content view to R.layout.main and you search for the TextView that you want to set the text. Because the TextView is not in that layout(is in the R.layout.sub and not in the R.layout.main) it will be null and you'll get a NullPointerException when you set the text, although you set the content view as the new layout that contains that TextView(you already search for it in the onCreate method so the reference is initialized with null).
The solution is to search for the TextView after you set the new content view(R.layout.sub), for example:
public void send(View v){
txt = (TextView) findViewById(R.id.textView1);
edit = (EditText) findViewById(R.id.editText2);
edit.setText("WHY ISNT THIS WORKING?");
if(qnum < questions.size()){
txt.setText("i work!");
qnum++;
}
else{
qnum = 0;
System.exit(0);
}
}
Ans the same thing goes for that Edittext.

Related

My android app stopping when I touch the button

This is my code guys when I touch the "saldir" button or another button my app
crashes and this is my app's image https://ibb.co/ce3ATm
This is my error (https://ibb.co/jBxb16):
Dergilik APP has stopped
package com.example.rg.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener
{
int sayac =0;
TextView tv;
**//kind of turkish words don't focus the variables**
TextView tvkarekterOzellikleri;
Button byemekye;
#Override
**//THIS IS MY MAIN_Activity.Java**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.Answer);
//I'm trying very basic app when I touch button this layout'll changed
tvkarekterOzellikleri = (TextView) findViewById(R.id.Question);
Button bsaldir = (Button) findViewById(R.id.saldir);
//comment/i have 3 buttons as you can see on link at top
Button buyu = (Button) findViewById(R.id.uyu);
Button byemekye = (Button) findViewById(R.id.yemekYe);
bsaldir.setOnClickListener(this);
buyu.setOnClickListener(this);
byemekye.setOnClickListener(this);*
//This part useless now because I didn't added this part later I'll add this part
//Character k = new Character();
//k.Movementnum = 10;
//k.Kilos = 10;
//k.FightPower = 10;
//}
#Override
//This part is PROBLEM WHEN I DELETE THIS PART MY CODE IS WORKING
public void onClick(View v){
if (v.getId()==byemekye.getId())
tv.setText("yemek yenildi");
else
tv.setText("Nar" + (sayac++));
}
}
When I delete if.(v.getId....) app is running normal.
This side is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rg.myapplication.MainActivity">
<Button
android:id="#+id/saldir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="saldir" />
<TextView
android:id="#+id/Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|bottom|center"
android:padding="3dp"
android:text="#string/Dilektasi"
android:textAppearance="#style/TextAppearance.AppCompat.Display2"
android:layout_above="#+id/saldir"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="75dp" />
<TextView
**android:id="#+id/Answer**"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/saldir"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="" />
//I have 3 button for my app as you can see on image
<Button
android:id="#+id/yemekYe"
**im saying twice pls dont focus on variables**
**next time i'll make this variables english**
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toLeftOf="#+id/saldir"
android:layout_toStartOf="#+id/saldir"
android:text="yemek ye" />
smth smth smth smth
<Button
android:id="#+id/uyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toEndOf="#+id/saldir"
android:layout_toRightOf="#+id/saldir"
android:text="Uyu" />
//dont focus the variable names
</RelativeLayout>
I think your problem is this:
Define Buttons:
private Button bt;
Set id of the button on xml, than on onCrate()
bt= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
code:
public class YourActivity extends AppCompatActivity {
private Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
parent_view = findViewById(android.R.id.content);
btn1= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
}
on xml
<Button
android:id="#+id/butto1" <!-- the id -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

Cannot set text to EditText from custom dialog

I had some problem with setting text to EditText. There are no error occur and I can get text from customDialog, but when I set text to edittext nothing happen.
This is a pact of xml file that use to set up edittext.
<LinearLayout
android:id="#+id/setting_layout_fname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100"
android:clickable="true">
<TextView
android:layout_height="wrap_content"
android:layout_weight="25"
android:clickable="false"
android:text="Firstname"
android:textStyle="bold"
android:layout_width="wrap_content" />
<android.support.design.widget.TextInputLayout
android:id="#+id/setting_textLayout_fname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="75"
android:clickable="false">
<android.support.design.widget.TextInputEditText
android:id="#+id/setting_edittext_fname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
And this is code to handle my alertdialog.
package com.example.yggdrasil.realapplication;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class setting extends AppCompatActivity implements View.OnClickListener{
private android.support.v7.widget.Toolbar toolbar;
private LinearLayout linear_fname, linear_lname, linear_birthdate, linear_gender, linear_tel,
linear_carefname, linear_carelname, linear_careemail, linear_caretel, linear_range,
linear_min, linear_max;
private TextInputEditText input_fname;
private List<LinearLayout> layout_list;
private AlertDialog.Builder alertdialog;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
alertdialog = new AlertDialog.Builder(this);
layout_list = new ArrayList<LinearLayout>();
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.activity_setting_toolbar);
toolbar.setCollapsible(true);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Setting");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
input_fname = (TextInputEditText) findViewById(R.id.setting_edittext_fname);
input_fname.setText("test");
layout_list.add(linear_fname = (LinearLayout) findViewById(R.id.setting_layout_fname));
/*layout_list.add(linear_lname = (LinearLayout) findViewById(R.id.setting_layout_lname));
layout_list.add(linear_birthdate = (LinearLayout) findViewById(R.id.setting_layout_birthdate));
layout_list.add(linear_gender = (LinearLayout) findViewById(R.id.setting_layout_gender));
layout_list.add(linear_tel = (LinearLayout) findViewById(R.id.setting_layout_tel));
layout_list.add(linear_carefname = (LinearLayout) findViewById(R.id.setting_layout_caretaker_fname));
layout_list.add(linear_carelname = (LinearLayout) findViewById(R.id.setting_layout_caretaker_lname));
layout_list.add(linear_careemail = (LinearLayout) findViewById(R.id.setting_layout_caretaker_email));
layout_list.add(linear_caretel = (LinearLayout) findViewById(R.id.setting_layout_caretaker_tel));
layout_list.add(linear_range = (LinearLayout) findViewById(R.id.setting_layout_glucose_range));
layout_list.add(linear_min = (LinearLayout) findViewById(R.id.setting_textLayout_minglucose));
layout_list.add(linear_max = (LinearLayout) findViewById(R.id.setting_textLayout_maxglucose));*/
for(LinearLayout linearLayout : layout_list){
linearLayout.setOnClickListener(this);
}
}
public void onClick(View v){
if(v.getId() == R.id.setting_layout_fname){
final EditText edittext = new EditText(getApplicationContext());
alertdialog.setTitle("First Name");
alertdialog.setMessage("Enter your firstname: ");
alertdialog.setView(edittext);
alertdialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
final String fname = edittext.getText().toString();
Toast.makeText(setting.this, fname, Toast.LENGTH_SHORT).show();
setting.this.input_fname.setText( fname, TextView.BufferType.EDITABLE);
}
});
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alertdialog.create().show();
}
}
don't know what happen with my code, Toast always show same value as I put it but it not show in edittext of mainActivity.
code have 2 edittext, the 1st is editText in alertdialog that created programmatically, and the 2nd is editText that create in xml file.
All I want to do is setText to editText in xml with value from editText in alertdialog. I can got the value from edittext in alertdialog but cannot setText to editText in xml file.
package com.example.yggdrasil.realapplication;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class setting extends AppCompatActivity implements View.OnClickListener{
private android.support.v7.widget.Toolbar toolbar;
private LinearLayout linear_fname, linear_lname, linear_birthdate, linear_gender, linear_tel,
linear_carefname, linear_carelname, linear_careemail, linear_caretel, linear_range,
linear_min, linear_max;
private TextInputEditText input_fname;
private List<LinearLayout> layout_list;
private AlertDialog.Builder alertdialog;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
alertdialog = new AlertDialog.Builder(this);
layout_list = new ArrayList<LinearLayout>();
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.activity_setting_toolbar);
toolbar.setCollapsible(true);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Setting");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
input_fname = (TextInputEditText) findViewById(R.id.setting_edittext_fname);
input_fname.setText("test");
layout_list.add(linear_fname = (LinearLayout) findViewById(R.id.setting_layout_fname));
for(LinearLayout linearLayout : layout_list){
linearLayout.setOnClickListener(this);
}
}
public void onClick(View v){
if(v.getId() == R.id.setting_layout_fname){
final EditText edittext = new EditText(getApplicationContext());
alertdialog.setTitle("First Name");
alertdialog.setMessage("Enter your firstname: ");
alertdialog.setView(edittext);
alertdialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
final String fname = edittext.getText().toString();
Toast.makeText(setting.this, fname, Toast.LENGTH_SHORT).show();
setting.this.input_fname.setText( fname, TextView.BufferType.EDITABLE);
}
});
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alertdialog.create().show();
}
}
layout code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/activity_setting_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:divider="?android:dividerHorizontal"
android:showDividers="middle"
android:padding="10dp">
<LinearLayout
android:id="#+id/setting_layout_fname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100"
android:clickable="true">
<TextView
android:layout_height="wrap_content"
android:layout_weight="25"
android:clickable="false"
android:text="Firstname"
android:textStyle="bold"
android:layout_width="wrap_content" />
<android.support.design.widget.TextInputLayout
android:id="#+id/setting_textLayout_fname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="75"
android:clickable="false">
<android.support.design.widget.TextInputEditText
android:id="#+id/setting_edittext_fname"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
Hmmm... I figure something. When I create new xml class withly slight same element with the old one, It perfectly work either with editText or textView. So I think I did some mistake and overlook it. So I guess that I should answer my own question.

Android app compiled but not working

I have made an android app named Yash on AIDE. It has an edittext,textview & a button. It is designed to first take input through the edittext then when we press the button, it should display the edittext text on the textview. It starts & takes the input but as I press the button, it hangs & shows:"Unfortunately Yash has stopped."
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/oButton"/>
/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Go!"
android:onClick="bc"
android:id="#+id/goButton"></Button>
</LinearLayout>
Java:
package com.mycompany.myapp4;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Toast;
import android.widget.EditText;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void bc(View view){
EditText t = (EditText) findViewById(R.id.editText);
String s = t.getText().toString();
TextView textView = (TextView) findViewById(R.id.oButton);
textView.setText(s);
}
}
The following line
String s = t.toString();
won't work like expected, because this way 's' is the String representation of the EditText object 't'. Most likely you meant to write
String s = t.getText().toString();
Make sure you have declared your activity in AndroidManifest.xml
try on this way.
public class MainActivity extends Activity {
EditText t;
TextView textView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initilization();
}
public void initilization() {
// TODO Auto-generated method stub
t = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.oButton);
String s = t.getText().toString();
if (!TextUtils.isEmpty(s)) {
textView.setText(s);
}else {
// check log its null
}
}
}

NullPointerException when calling GetText()

First off, I'm an Android noob and I'm making a simple app that takes text from an EditText widget and shows it in a TextView when you tap on a button. But when the button is clicked, it forcecloses and I get a NullPointerException in the logcat.
My activity code is:
package com.deltablue.freeearl.ama.ama;
import android.app.Activity;
import android.os.Bundle;
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 {
private Button b1;
private EditText e1;
private TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Definiciones de los elementos
Button b1 = (Button) findViewById(R.id.bttn1);
EditText e1 = (EditText) findViewById(R.id.edit1);
TextView t1 = (TextView) findViewById(R.id.txt1);
}
public void b1click(View view) {
Toast.makeText(this, "Pressed", Toast.LENGTH_SHORT).show();
String text = e1.getText().toString();
t1.setText("Input: ");
}
}
The thing is, when debugging, the findViewById() calls return correct values. But when b1click() is executed, e1 & t1 are null. All the other similar questions I've seen have to see with findViewById() returning null or wrong widget IDs, but that's not my case, AFAIK. I know it probably is some really stupid error, but I don't see where I'm wrong.
My layout is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.deltablue.freeearl.ama.ama.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edit1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show text"
android:id="#+id/bttn1"
android:layout_gravity="center_horizontal"
android:onClick="b1click" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/txt1"
android:editable="true" />
</LinearLayout>
</RelativeLayout>
Thanks in advance for the help!
Here, in onCreate:
Button b1 = (Button) findViewById(R.id.bttn1);
EditText e1 = (EditText) findViewById(R.id.edit1);
TextView t1 = (TextView) findViewById(R.id.txt1);
... you're declaring three local variables. Those are completely separate from the fields that you declared earlier:
private Button b1;
private EditText e1;
private TextView t1;
I'm pretty sure you just wanted to change the values of the fields instead, so you just want to assign new values to the variables, rather than redeclaring them:
b1 = (Button) findViewById(R.id.bttn1);
e1 = (EditText) findViewById(R.id.edit1);
t1 = (TextView) findViewById(R.id.txt1);

"Unfortunately, <App Name> has stopped." Common unexplained Error

I am having a problem when debugging apps that respond to user input. An ordinary app that just displays text works fine, but when I give a button an event handler I get a generic message "Unfortunately, 'App Name' has stopped.". I have tried many solutions, but to no avail. I am using IntelliJ IDEA 12.0.4 (Community Edition).
Sorry for asking a very common question but it seems the solution is different for everyone.
Edit - new working code
Source Code:
package com.example.Android_Test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import static com.example.Android_Test.R.*;
public class MyActivity extends Activity {
EditText editText;
TextView textView;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Declare all widget controls.
editText = (EditText) findViewById(R.id.txtEdit);
Button button = (Button) findViewById(R.id.btnChange);
textView = (TextView) findViewById(R.id.lblText);
button.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(editText.getText());
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtEdit" android:layout_gravity="left|center_vertical"
android:layout_alignParentLeft="true" android:layout_marginLeft="0dp" android:layout_alignParentTop="true"
android:layout_marginTop="0dp" android:layout_alignParentRight="true" android:hint="Type here change text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Text"
android:id="#+id/btnChange"
android:layout_alignLeft="#+id/txtEdit" android:layout_below="#+id/txtEdit"
android:layout_alignRight="#+id/txtEdit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change this text"
android:id="#+id/lblText"
android:layout_alignLeft="#+id/txtEdit" android:layout_below="#+id/btnChange"
android:layout_alignRight="#+id/txtEdit" android:textSize="18dp"/>
</RelativeLayout>
Logcat is very long so I pasted it here: http://pastebin.com/2qaY8ZJj.
Everyone's Logcat is so much shorter i don't know why
Move this code
// Declare all widget controls.
EditText editText = (EditText) findViewById(R.id.txtEdit);
Button button = (Button) findViewById(R.id.btnChange);
TextView textView = (TextView) findViewById(R.id.lblText);
inside onCreate() and use R.id... .
Edit
I edited youre code
public class MyActivity extends Activity {
EditText editText;
TextView textView;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Declare all widget controls.
editText = (EditText) findViewById(R.id.txtEdit);
Button button = (Button) findViewById(R.id.btnChange);
textView = (TextView) findViewById(R.id.lblText);
button.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(editText.getText().toString);
}
};
}

Categories