Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用findViewById方法时出错_Android_Android Studio_Android Resources - Fatal编程技术网

Android 使用findViewById方法时出错

Android 使用findViewById方法时出错,android,android-studio,android-resources,Android,Android Studio,Android Resources,我试图使用findViewById()方法将操作分配给我创建的按钮,但是它给了我一个错误: android.support.v7.app.AppCompatActivity中的findViewById(int)不能应用于(android.widget.Button) 在台词上: buttonStudentAccess = (Button) findViewById(buttonStudentAccess); buttonGuestAccess = (Button) findViewById(bu

我试图使用
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" />


我使用相同的语法在我的登录和注册类中创建并分配操作给按钮,但它没有创建此错误。非常感谢您的帮助,谢谢

您需要指定从何处获取id“buttonStudentAccess”。下面是如何找到视图:

buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(R.id.buttonGuestAccess);
buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);

要指定按钮或任何其他视图位于“资源”包中,请将其称为
R
.id
指定要使用其id获取视图。

您需要指定获取id的位置。下面是如何找到该视图:
ButtonStudenAccess=(button)findViewById(R.id.按钮学生访问)
Hi,按钮存在于我的资源包中的path\MyApplication\app\src\main\res\layout\activity\u user\u type\u selection.xml中。确切地说,这就是为什么你需要将
R.id.buttonStudentAccess
传递给
findViewById
方法,而不是只传递
buttonStudentAccess
。非常感谢!我没有注意到我的迷雾阿克,我忘了传递方法,现在找到了按钮。是的,请确保指定视图类型(
id
drawable
color
,…),并且您从参考资料中获取它们,
R