1

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

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>
4

1 回答 1

1

Project -> Clean是您遇到此类错误时的解决方案。

清理您的项目并重新构建它。

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

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

于 2013-01-26T17:48:30.130 回答