bottone1无法解析或不是字段

时间:2013-01-26 17:47:15

标签: android android-layout android-widget

像以前一样,我正在做一些教程,但我认为我重写的那个我在重写代码时犯了一些错误。在本指南的这一部分,我应该添加两个按钮。我已经完成了所有步骤,但是:

  

bottone1无法解析或不是字段   
bottone2无法解析或不是一个领域

以下是代码:

package marco.prova;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;

public class Main extends Activity {
    private TextView textView1;
    private Button bottone1;
    private Button bottone2;
    /** Called when the activity is first created.*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textView1 = (TextView) findViewById(R.id.testo1);
      textView1.setText("Testo modificato tramite codice 1");
      bottone1 = (Button) findViewById(R.id.bottone1);
      bottone2 = (Button) findViewByid(R.id.bottone2);
      bottone1.setOnClickListener(new View.OnClickListener() {
        public void onClick (View view) {
          textView1.setText("E' stato premuto il bottone 1");
        }
      });
      bottone2.setOnClickListener(new View.OnClickListener() {
        public void onClick (View view) {
          textView1.setText("E' stato premuto il bottone 1");
        }
      });
    }
  }

希望得到一些帮助。谢谢。

这里是layout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"> >

    <TextView
        android:text="Testo di default TextView1"
        android:id="@+id/testo1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </TextView>

    <Button
        android:text="Bottone1"
        android:id="@+id/bottone1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </Button>   

    <Button
        android:text="Bottone2"
        android:id="@+id/bottone2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </Button>


</LinearLayout>

1 个答案:

答案 0 :(得分:1)

当您遇到此类错误时,

Project -> Clean 就是解决方案。

清理项目并重新构建。

或者如果它仍然不起作用,删除生成的R.java并再次构建项目以从头开始创建生成的代码。

此外,setContentView(R.layout.<filename>);作为XML文件的名称是<filename>.xml,其中定义了按钮。