开发者

can't find any "id" when i use "findViewById" in Android SDK?

First I will mention that I am totally new to programming. I have created a button in an .xml file by using the "Graphical layout". I can see the buttons ID is "@+id/button1". Here is the .xml codes from the file:

<?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">
    <Button android:text="Button"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    &开发者_如何学Pythonlt;/Button>
</LinearLayout>`

When I then go the the java file and try to find the "button" I type "findViewById(R.id)" right under a line that says "setContentView(R.layout.main);" and click "ctrl+space" it says "no default proposals". The .xml codes are:

package com.soren.activies;

import android.app.Activity;
import android.os.Bundle;


 public class mail extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button b = findViewById(R.id)


}

Hope you can help me out here because, I could really use some help. ¨ By the way, sorry for my bad English.


The code you posted...

Button b = findViewById(R.id)

...should be...

Button b = findViewById(R.id.button1);

Also, make sure the layout file is called main.xml in all lower-case.

Just spotted the bit where you say eclipse says "no default proposals" - Jack's suggestion of using 'Clean' and rebuilding your project may also fix the problem if the project environment has become corrupt (which happens sometimes in eclipse).


Button b = findViewById(R.id) 

Here is the right answer fyi.

Button b = (Button)findViewById(R.id.button1); 


I can't remember exactly which one it was, but a lot of times you will have to clean or build your project first. If you check "Build Automatically" from Project > Build Automatically, then it will build upon saving, giving you access to the R.id.* auto completion.


you have to save each file individually before switching to a new file and trying to find updates you have created such as creating the button.


 Button b = findViewById(R.id)

Should be

 Button b =(Button) findViewById(R.id.buttonid)


Change

import android.R;

to

import YOUR_PACKAGE_NAME.R;


your answer is just

Button b=(Button) findViewById(R.id.buttonid)

if didnt work ctrl+B

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜