使用 findViewById 方法时出错
Error using findViewById method
我正在尝试使用 findViewById()
方法将操作分配给我创建的按钮,但是它给我错误:
android.support.v7.app.AppCompatActivity
中的 findViewById(int)
无法应用于 (android.widget.Button)
行:
buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);
我的代码如下:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class UserTypeSelection extends AppCompatActivity implements View.OnClickListener{
private Button buttonStudentAccess;
private Button buttonGuestAccess;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_type_selection);
buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);
buttonStudentAccess.setOnClickListener(this);
buttonGuestAccess.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == buttonStudentAccess) {
}
if (view == buttonGuestAccess) {
//startActivity(new Intent(this, MainActivity.class));
}
}
相应的 xml 文件包含按钮
<Button
android:text="Guest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonStudentAccess"
android:layout_alignLeft="@+id/buttonStudentAccess"
android:layout_alignStart="@+id/buttonStudentAccess"
android:layout_marginTop="30dp"
android:id="@+id/buttonGuestAccess"
android:layout_alignRight="@+id/buttonStudentAccess"
android:layout_alignEnd="@+id/buttonStudentAccess" />
<Button
android:text="Student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="@+id/buttonStudentAccess"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
我在登录和注册中使用相同的语法创建和分配操作按钮,但没有出现此错误。感谢任何帮助,谢谢
buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(R.id.buttonGuestAccess);
您需要指定从何处获取 ID "buttonStudentAccess"。因此,您可以通过以下方式找到视图:
buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
要指定按钮或任何其他视图位于 "resources" 包中,您将其称为 R
并且 .id
指定您要使用其抓取视图id.
我正在尝试使用 findViewById()
方法将操作分配给我创建的按钮,但是它给我错误:
android.support.v7.app.AppCompatActivity
中的 findViewById(int)
无法应用于 (android.widget.Button)
行:
buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);
我的代码如下:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class UserTypeSelection extends AppCompatActivity implements View.OnClickListener{
private Button buttonStudentAccess;
private Button buttonGuestAccess;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_type_selection);
buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);
buttonStudentAccess.setOnClickListener(this);
buttonGuestAccess.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == buttonStudentAccess) {
}
if (view == buttonGuestAccess) {
//startActivity(new Intent(this, MainActivity.class));
}
}
相应的 xml 文件包含按钮
<Button
android:text="Guest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonStudentAccess"
android:layout_alignLeft="@+id/buttonStudentAccess"
android:layout_alignStart="@+id/buttonStudentAccess"
android:layout_marginTop="30dp"
android:id="@+id/buttonGuestAccess"
android:layout_alignRight="@+id/buttonStudentAccess"
android:layout_alignEnd="@+id/buttonStudentAccess" />
<Button
android:text="Student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="@+id/buttonStudentAccess"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
我在登录和注册中使用相同的语法创建和分配操作按钮,但没有出现此错误。感谢任何帮助,谢谢
buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(R.id.buttonGuestAccess);
您需要指定从何处获取 ID "buttonStudentAccess"。因此,您可以通过以下方式找到视图:
buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
要指定按钮或任何其他视图位于 "resources" 包中,您将其称为 R
并且 .id
指定您要使用其抓取视图id.