Android编程出现错误:cannot be resolved or is not a field
程序功能:显示两个button1和button2,点击button1时程序标题为button1,button2功能也一样。activity_main.xml文件内容如下:...
程序功能:显示两个button1和button2,点击button1时程序标题为button1,button2功能也一样。
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" tools:context=".MainActivity" >
<Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="39dp" android:text="Button1" />
<Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/bt1" android:layout_below="@+id/bt1" android:layout_marginTop="46dp" android:text="Button2" /> </RelativeLayout>
MainActivity文件如下:
package com.example.textview;
import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.Button;
public class MainActivity extends Activity { Button button1,button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button)findViewById(R.id.bt1); button2=(Button)findViewById(R.id.bt2); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { setTitle("Button1注册成功!"); } }); } public void onClick(View v) { setTitle("Button1注册成功!"); }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }
}
程序出现的错误如下图所示:
请问:该如何解决这个问题?谢谢 展开
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" tools:context=".MainActivity" >
<Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="39dp" android:text="Button1" />
<Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/bt1" android:layout_below="@+id/bt1" android:layout_marginTop="46dp" android:text="Button2" /> </RelativeLayout>
MainActivity文件如下:
package com.example.textview;
import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.Button;
public class MainActivity extends Activity { Button button1,button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button)findViewById(R.id.bt1); button2=(Button)findViewById(R.id.bt2); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { setTitle("Button1注册成功!"); } }); } public void onClick(View v) { setTitle("Button1注册成功!"); }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }
}
程序出现的错误如下图所示:
请问:该如何解决这个问题?谢谢 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询