I'm new to Android/java, coming from C#/Visual Studio.
It's not a too hard jump from C# to java while coding the logic, but with the UI, I'm having problems.
I've now added a simple TextView to my main.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:orientation="vertical">
<TextView android:layout_width="wrap_content" android:editable="true" android:layout_height="wrap_content" android:id="#+id/textView1" android:text="TextView">
</TextView>
</LinearLayout>
Now I wanted to access the textView1 by code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.textView1 = (TextView)this.findViewById(android.R.id.textView1);
}
private TextView textView1;
but I get the error: textView1 cannot be resolved.
What am I doing wrong?
Thanks
James
Replace findViewById(android.R.id.textView1) by findViewById(R.id.textView1);
android.R is resorce packege of SDK while R is of your app which will create along with successfully build of app .
and set the background color to #ffffff in xml .... it seems more attractive..
Instead of android.R.id.textView1, just use R.id.textView1. The android prefix is for resources from Android itself, rather than from your project.
Replace findViewById(android.R.id.textView1) by findViewById(R.id.textView1);
android.R is resorce packege of SDK while R is of your app which will create along with successfully build of app .
try this
private TextView textView1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.textView1 = (TextView)this.findViewById(R.id.textView1);
textView1.setText("Hello World !!!");
}
Related
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Null pointer Exception - findViewById()
(12 answers)
Closed 3 years ago.
I can not open the APK normally, but the debugger has no serious problems. I consider that the problem may occur in here.
MainActivity.java:
private TextView text;
public static Toast toast = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
text.setText("IP address : "+getLocalHostIp());
new ServerListener().start();
}
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:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:id="#+id/setText"
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_weight="1" />
</LinearLayout>
You are using the wrong id for textView in your code which is causing the app to crash.
You've defined setText as id for the TextView in your .xml file android:id="#+id/setText", and you are using "text" as the id to map the TextView in your code text = (TextView) findViewById(R.id.text);, which obviously can not be found and is the cause of the problem.
to solve this, do one of the following:
Change text = (TextView) findViewById(R.id.text); to text = (TextView) findViewById(R.id.setText);
or change android:id="#+id/setText" to android:id="#+id/text"
Your id for TextView is wrong, you can use below code
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_weight="1" />
I have got an idea to get rid of some coding when we do initializion of views.
When we create an xml layout in android. At that movement same name class is created with UpperCase Letters with a dialogue with permission. And as i created views with ids. It should create Views and initialize them automatically.
for e.g
close.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:orientation="vertical" >
<Button
android:id="#+id/closeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/close_img" />
<TextView
android:id="#+id/closeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing Text" />
</LinearLayout>
And automatically generated java code should be.
public class Close extends Activity
{
TextView CloseText;
Button CloseBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CloseText = (TextView)findViewById(R.id.closeText);
CloseBtn = (Button)findViewById(R.id.closeBtn);
}
}
I can read xml and do the other stuff as I explained. But how could i add this module to eclipse and How could i create a service which work in Background all the time in eclipse.
Suggest me how to add this module(Plugin) to eclipse and i am going to use JAXB to generate Java Objects from XML document. Is there any better option.
Here in this website i find that just paste the xml and you will get your java code ready for activity class.
i had attached the link
I am getting error in speed = (TextView) findViewById(R.id.speed); saying "speed cannot be resolved or it is not a field".
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SpeedometerActivity" >
<Button
android:id="#+id/download"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="13dp"
android:layout_marginLeft="300dp"
android:text="Begin Test" />
<TextView
android:id="#+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/meter"
android:layout_marginLeft="191dp"
android:layout_marginTop="55dp"
android:layout_toRightOf="#+id/meter"
android:text="TextView"
android:textSize="20dp"
android:textColor="#000000"/>
</RelativeLayout>
My MainActivity:
public class MainActivity extends Activity {
TextView speed;
Button download;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speedometer);
download = (Button)findViewById(R.id.download);
}
#Override
protected void onResume() {
super.onResume();
h = new Handler();
new speedTask().execute();
speed = (TextView) findViewById(R.id.speed);
}
}
Please anybody help me to solve this problem.
"speed cannot be resolved or it is not a field"
Check if any errors in your resource files. Follow the suggestion by blackbelt. If you have errors in resource files your R.java will not be generated. Fix it. also check if you have import android.R; if so remove it.
Further you have
<TextView
android:id="#+id/speed"
Its a textview
Casting it to button
speed = (Button)findViewById(R.id.speed);
Should be
TextView speed =(TextView) findViewById(R.id.speed);
You should check if the R class you imported is the one of your project and not the android's one. Also you are casting to the wrong object, as correctly stated from #Raghunandan
Here you are type cast TextView to Button. So please change your code as below.
TextView speed = (Button)findViewById(R.id.speed);
Note : Here you are not able to import your project R file, first please check your xml file and try to resolve xml error first.
Please remove import android.R; from your activity if you have import it.
Go to your MainActivity.Java file and Press CTRL+SHIFT+O. It will automatically import all necessary packages.
It has created a duplicate so give as follows
Button speed1;
speed1=(Button)findViewById(R.id.speed);
It will work!
Does anybody know how to programmatically set the text of a button?
thing is i'm not calling this from the main layout(setContentView) i'm calling it in a view thats inflated after an asynctask
heres what i have tried but this is giving a null pointer exception on the 2nd line
Button mButton=(Button)findViewById(R.id.contact);
mButton.setText("number");
heres my layout where i am calling the button
<Button
android:id="#+id/contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/address"
android:layout_toLeftOf="#+id/badge"
android:background="#drawable/ic_btn_call"
android:textSize="10dp"
android:textStyle="bold"
android:textColor="#color/white"/>
here my view i'm inflating
ClubInfo = LayoutInflater.from(getBaseContext()).inflate(R.layout.clubinfocell,
null);
TextView TeamNameText = (TextView) ClubInfo.findViewById(R.id.TeamName);
TeamNameText.setText(teamName);
TextView AddressText = (TextView) ClubInfo.findViewById(R.id.address);
AddressText.setText(address1);
Button mButton=(Button)ClubInfo.findViewById(R.id.contact);
mButton.setText(telephone);
Then use your view's object to initialize it:
Button mButton = (Button)your_view_object.findViewById(R.id.contact);
mButton.setText("number");
When you try to identify a view other than your Activity's layout, you have to pass the reference of that view like this. If not Android will keep looking for this element from the layout which you provided in the setContentView().
For example, consider you have inflated a view like this:
View View = mInflater.inflate(R.layout.gridelement, null);
Then use this View's object for the Button present in that inflated layout:
Button mButton = (Button)View.findViewById(R.id.contact);
change your code as:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//set layout here in which u have add contact in xml
Button mButton=(Button)findViewById(R.id.contact);
mButton.setText("number");
EDIT:
Your \res\layout\main.xml look like as:
<?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" >
<Button
android:id="#+id/contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/address"
android:layout_toLeftOf="#+id/badge"
android:background="#drawable/ic_btn_call"
android:textSize="10dp"
android:textStyle="bold"
android:textColor="#color/white"/>
</LinearLayout>
your mButton is null.so NPE.are you refrenced xml resources after setContentView
onCreate(){
...
setContentView(R.layout.yourlayout);
Button mButton=(Button)findViewById(R.id.contact);
mButton.setText("number");
}
check R.java files import statement
are you sure that you have import it of the project you use ..
and just format your layout (.xml ) file save it and again type the same statement
So I just started playing around with android and I'm trying to see what things I can do. I was following the very first android tutorial: http://developer.android.com/training/basics/firstapp/index.html and at the very end, you programmatically define a TextView. I wanted to change this to be defined in a new layout, so I wrote this (it is named display_message.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:orientation="vertical" >
<TextView android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And in the DisplayMessage class, I changed it to this:
public class DisplayMessage extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display_message);
// Get message from intent
Intent intent = getIntent();
String message = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE);
// Get the text view
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(message);
}
}
However, Eclipse says that it doesn't know what R.layout.display_message is, nor does it know what R.id.text_view is. Is there somewhere else I need to define them or something? Where did I mess up?
It seems correct but be sure that the R class imported is the correct one.
Sometimes Eclipse imports android.R but the R file you need to import is your.package.name.R