没有错误,但findviewbyid无法解决

时间:2017-12-02 07:19:41

标签: java android

我正在尝试从我的xml获取ID,我找不到任何error,但findviewbyid无法解析

这是我的java(主要活动)

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

Button button0, button1, button2, button3, button4, button5, button6,
        button7, button8, button9, buttonAdd, buttonSub, buttonDivision,
        buttonMul, button10, buttonC, buttonEqual;
EditText exampleEditText;

float mValueOne, mValueTwo;

boolean exampleAddition, mSubtract, exampleMultiplication, exampleDivision;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button0 = (Button) findViewById(R.id.button0);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);
    button5 = (Button) findViewById(R.id.button5);

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
    android:id="@+id/edt1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/button4"
    android:layout_alignRight="@+id/button4"
    android:layout_below="@+id/edt1"
    android:layout_marginTop="94dp"
    android:text="1" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/button1"
    android:layout_toLeftOf="@+id/button3"
    android:layout_toStartOf="@+id/button3"
    android:text="2" />

buttons的其余部分是相同的,只是我的代码太长而无法发布。

我不确定是否是我的导入错误或? 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在Android JavaXML绑定以下代码

setContentView(R.layout.activity_main);
  1. 检查您的活动文件名称
  2. 尝试构建 - &gt;清洁项目完成后重建