I have this error in eclipse.My code is this.
public class MainActivity extends ActionBarActivity {
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView) findViewById(R.id.textView1);
t1.setText("Hello");
}
}
XML code
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="46dp"
android:layout_marginTop="37dp"
android:text="TextView" />
Help me to give the solution.
Related
Can anyone guide me please?
When I run this, it stops before running.
I dunno what's wrong in this.
Can anyone guide me please?
When I run this, it stops before running.
I dunno what's wrong in this.
MainActivity.java-
package gangster.cookies;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
TextView textView = (TextView) findViewById(R.id.textView);
ImageView imageView = (ImageView) findViewById(R.id.bc);
public void eat(View view) {
textView.setText(R.string.changed_shit);
imageView.setImageResource(R.drawable.after_cookie);
}
public void uneat(View view) {
textView.setText(R.string.changed_again);
imageView.setImageResource(R.drawable.before_cookie);
}
}
Strings.xml-
<resources>
<string name="app_name">Cookies</string>
<string name="changed_shit">I am full bitch</string>
<string name="changed_again">Feed me again Motherfucker</string>
</resources>
activity_main.xml-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#BC8F8F"
android:orientation="vertical"
tools:context="gangster.cookies.MainActivity">
<ImageView
android:id="#+id/bc"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop"
android:src="#drawable/before_cookie" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="I am Hungry Bitch"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp"
android:textColor="#fff"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eat Cookie"
android:id="#+id/button"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:onClick="eat"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Uneat"
android:id="#+id/button2"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="100dp"
android:onClick="uneat"/>
</LinearLayout>
</LinearLayout>
Change your onCreate View like This:
TextView textView;
ImageView imageView;
Button eat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
imageView = (ImageView) findViewById(R.id.bc);
eat = (Button) findViewById(R.id.button);
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
eat();
}
});
// Calling member function
uneat();
}
public void eat() {
textView.setText(R.string.changed_shit);
imageView.setImageResource(R.drawable.after_cookie);
}
public void uneat() {
textView.setText(R.string.changed_again);
imageView.setImageResource(R.drawable.before_cookie);
}
you have initialized the TextView and ImageView outside of any method.. put that code inside onCreate(). method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView);
ImageView imageView = (ImageView) findViewById(R.id.bc);
}
Replace your MainActivity.java with following code.
package gangster.cookies;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
imageView = (ImageView) findViewById(R.id.bc);
}
public void eat(View view) {
textView.setText(R.string.changed_shit);
imageView.setImageResource(R.drawable.after_cookie);
}
public void uneat(View view) {
textView.setText(R.string.changed_again);
imageView.setImageResource(R.drawable.before_cookie);
}
}
I need a button in android button.I need to show that when button was click it show the character 1 in the TextView field textView.
So here is my java code.
package com.example.kartikeya;
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 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonOnClick(){
Button button = (Button) findViewById(R.id.button);
final TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textView.setText(textView.getText() + "" + 1);
}
});
}
This is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<Button
android:text="#string/button"
android:id="#+id/button"
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:clickable="true"
android:contentDescription="#string/click1"
android:contextClickable="true" />
<Button
You forget to call your method. buttonOnClick();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOnClick();
}
Try this way
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
final TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textView.setText(textView.getText() + "" + 1);
}
});
}
You have missed the text view in your activity.xml file.
you never called this fuction... buttonOnClick()
In MainActivity write following code.
public class MainActivity extends AppCompatActivity {
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.clickBtn);
TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(textView.getText() + "" + 1);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/textView"
android:textSize="30sp"/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/clickBtn"
android:layout_centerInParent="true"
android:text = "click me"/>
</RelativeLayout>
I have developed a simple form within an activity.
My requirement is that when I click a send button, the form data is displayed in same the form layout
But this program is not displaying any field data.
No error is thrown.
Main_Activity.java:
public class MainActivity extends ActionBarActivity {
// private static final String EXTRA_MESSAGE = "com.example.";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
EditText editText1 =(EditText) findViewById(R.id.name);
String message1 =editText1.getText().toString();
Log.v("ThisApp", message1);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message1);
}
}
activity_main.xml:-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="#string/name"
android:background="#f3f3f3"
android:layout_marginTop="30dp"
android:hint="Name"/>
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Am I doing something wrong?
Put TextView in same layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="#string/name"
android:background="#f3f3f3"
android:layout_marginTop="30dp"
android:hint="Name"/>
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"
android:layout_gravity="center_horizontal" />
<TextView
android:id="#+id/showText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="..."
android:background="#f3f3f3"
android:layout_marginTop="30dp"
android:hint="..."/>
</LinearLayout>
Then reference it from MainActivity:
public class MainActivity extends ActionBarActivity {
// private static final String EXTRA_MESSAGE = "com.example.";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton=(Button)findViewById(R.id.sendButton);
EditText editText1 =(EditText) findViewById(R.id.name);
TextView textView = (TextView) findViewById(R.id.showText);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
String message1 =editText1.getText().toString();
textView.setText(message1);
}
});
}
}
![error ][1]
package com.welcome.testingnew;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.R;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText username;
final EditText password;
username=(EditText) findViewById(R.id.EditText1);
password=(EditText) findViewById(R.id.EditText2);
final EditText result=(EditText) findViewById(R.id.EditText3);
Button login=(Button) findViewById(R.id.button1);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(username.toString().equals("thamarai") && password.toString().equals("selva")){
EditText3.setText("Success");
}
else
EditText3.setText("failed");
}
});
}
}
Tried: if i run projetc with below codes it works fine
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
}
and if i insert anycode after the
setContentView(R.layout.activity_main);
like this
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText username;
final EditText password;
username=(EditText) findViewById(R.id.EditText1);
password=(EditText) findViewById(R.id.EditText2);
final EditText result=(EditText) findViewById(R.id.EditText3);
Button login=(Button) findViewById(R.id.button1); }
}
it makes Unfortunately app has stopped problem please answer me as soon as possible
I guess you're trying to achieve this. Here's the working code :
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:text="Username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="30dp"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginLeft="28dp"
android:layout_toRightOf="#+id/textView1"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/editText1"
android:layout_alignRight="#+id/editText1"
android:inputType="textPassword" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="Login" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="Status"
android:textAppearance="?android:attr/textAppearanceLarge" />
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText username;
EditText password;
TextView status;
Button log;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.editText1);
password = (EditText) findViewById(R.id.editText2);
status = (TextView) findViewById(R.id.textView3);
log = (Button) findViewById(R.id.button1);
log.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String uname = username.getText().toString();
String pass = password.getText().toString();
if(uname.equals("admin") && pass.equals("admin"))
{
status.setText("Success");
}
else
{
status.setText("failed");
}
}
});
}
}
When app starts,
When validation fails,
When validation success,
Hope it helps!
I am trying to fetch the EditText value on click of a button.
String ETValue = ((EditText)findViewById(R.id.ETID)).getText().toString().trim();
Every things works fine on other android versions, but on 1.6 I am getting ""(Empty) String.
Whats going wrong on Android 1.6, how this is happening?
Thanks
Screen Shots:
Code Reference :
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_marginTop="10dip"
android:layout_marginLeft="5dip"
android:text="Type you text here"
android:id="#+id/TextView02"
android:layout_height="wrap_content"
android:textColor="#333333"
android:layout_width="fill_parent"
android:textSize="16dip">
</TextView>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID1">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="250dip"/>
<Button android:id="#+id/btnID"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="#drawable/icon"
android:gravity="bottom"
android:paddingBottom="9dip"
android:layout_marginLeft="3dip"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID2"
android:visibility="gone">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginRight="5dip" />
</LinearLayout>
<Button android:text="Click" android:id="#+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
Etext.Java
public class EText extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = ((EditText) findViewById(R.id.keyword)).getText().toString().trim();
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
Even this way doesn't works
public class EText extends Activity {
/** Called when the activity is first created. */
EditText ETextt1 = null;
EditText ETextt2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(Integer.parseInt(Build.VERSION.SDK) < 7){
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
ETextt1 = ((EditText) findViewById(R.id.keyword1));
}else{
ETextt2 = ((EditText) findViewById(R.id.keyword2));
}
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = null;
if(null == ETextt1){
ETValue = ETextt2.getText().toString().trim();
}else if(null == ETextt2){
ETValue = ETextt1.getText().toString().trim();
}
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
This Works Perfectly Fine :-)
package com.test.et;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class EText extends Activity {
/** Called when the activity is first created. */
EditText ETextt1 = null;
EditText ETextt2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(Integer.parseInt(Build.VERSION.SDK) < 7){
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
}
//ETextt = ((EditText) findViewById(R.id.keyword1));
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = null;
if(Integer.parseInt(Build.VERSION.SDK) < 7){
ETValue = ((EditText) findViewById(R.id.keyword2)).getText().toString().trim();
}else{
ETValue = ((EditText) findViewById(R.id.keyword1)).getText().toString().trim();
}
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
I have found the problems source. You declareted two EditTexts with the same id. To resolve problem just rename your EditText as keyword1 and keyword2. Then, get text from second EditText.
public class EdText extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = ((EditText) findViewById(R.id.keyword2)).getText().toString().trim();
Toast.makeText(EdText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
layout:
`
<TextView
android:layout_marginTop="10dip"
android:layout_marginLeft="5dip"
android:text="Type you text here"
android:id="#+id/TextView02"
android:layout_height="wrap_content"
android:textColor="#333333"
android:layout_width="fill_parent"
android:textSize="16dip">
</TextView>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID1">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword1"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="250dip"/>
<Button android:id="#+id/btnID"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="#drawable/icon"
android:gravity="bottom"
android:paddingBottom="9dip"
android:layout_marginLeft="3dip"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID2"
android:visibility="gone">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword2"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginRight="5dip" />
</LinearLayout>
`
I have just finished struggling with EditText returning an empty string ("").
The reason was that setContentView(R.layout.main); was invoked twice:
private EditText editText1;
private EditText editText2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /// !!!
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
// some really long and untrivial initialization stuff
setContentView(R.layout.main); /// !!!
}
It looks like editText1 and editText2 pointed into the parts of the previous incarnation of R.layout.main...
Here is my test example for your case:
public class EdText extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// string initialised here will always have initial value and never changes
final String ETValue = ((EditText) findViewById(R.id.EditText01)).getText().toString().trim();
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(EdText.this, ((EditText) findViewById(R.id.EditText01)).getText().toString().trim(), Toast.LENGTH_SHORT).show();
}
});
} }
Everything works fine for API 4. Maybe your probles connected with the moment when you read the string value. see my comment.
public class EditTextDemo extends Activity {
/** Called when the activity is first created. */
Button btnClick;
String ETValue;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ETValue = ((EditText)findViewById(R.id.editText1)).getText().toString().trim();
btnClick.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
//assign a default value
ETValue.equals("Welcome!!");
Toast.makeText(getBaseContext(), "Value is:"+ETValue, Toast.LENGTH_SHORT).show();
Log.i("EditTextDemo","-----Value of EditText is:----------"+ETValue);
}
});
}}
You can also give value at runtime.. Also that value can be used for further coding.
Hope this will help!!