findViewById没有从相关活动中查找视图

时间:2017-10-28 06:17:16

标签: java android findviewbyid android-studio-3.0

编辑:重建项目后解决了问题。

我正在创建一个在两个活动之间共享数据的应用程序。 两个活动的名称:1。MainActivity 2.ScorerActivity

当我尝试在ScorerActivity中创建TextView对象的实例时,findViewById方法从MainActivity布局中查找id。它找不到ScorerActivity布局中的视图。

MainActivity Java

package com.example.android.badmintonscorer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

    Button startButton = findViewById(R.id.start_button);
    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText player1Name = findViewById(R.id.player1_name);
            String player1 = player1Name.getText().toString();

            EditText player2Name = findViewById(R.id.player2_name);
            String player2 = player2Name.getText().toString();

            Intent intent = new Intent(MainActivity.this, ScorerActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("player1_name", player1);
            intent.putExtra("player2_name", player2);
            startActivity(intent);
        }
    });
}
}

MainActivity布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.example.android.badmintonscorer.MainActivity">

<EditText
    android:id="@+id/player1_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:hint="Name of Player 1"
    android:layout_marginTop="50dp"/>

<EditText
    android:id="@+id/player2_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:hint="Name of Player 2"
    android:layout_marginTop="50dp"/>

<Button
    android:id="@+id/start_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="75dp" />

    </LinearLayout>

ScorerActivity Java

package com.example.android.badmintonscorer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ScorerActivity extends AppCompatActivity {

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

    TextView player1 = findViewById(R.id.p1name); //Cannot resolve symbol
}
}

ScorerActivity布局

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00f921"
tools:context="com.example.android.badmintonscorer.ScorerActivity">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/p1name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="sans-serif-medium"
            android:gravity="center_horizontal"
            android:padding="16dp"
            android:text="Player 1"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/p1score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="sans-serif-light"
            android:gravity="center_horizontal"
            android:paddingBottom="24dp"
            android:text="0"
            android:textSize="50sp" />

        <Button
            android:id="@+id/p1plus1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:background="#f9bf00"
            android:onClick="pointp1"
            android:text="+1 Point" />

        <Button
            android:id="@+id/p1df"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:background="#f9bf00"
            android:onClick="dfp1"
            android:text="Double Fault" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="2dp"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:textColor="@android:color/darker_gray" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/p2name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="sans-serif-medium"
            android:gravity="center_horizontal"
            android:padding="16dp"
            android:text="Player 2"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/p2score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="sans-serif-light"
            android:gravity="center_horizontal"
            android:paddingBottom="24dp"
            android:text="0"
            android:textSize="50sp" />

        <Button
            android:id="@+id/p2plus1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:background="#f9bf00"
            android:onClick="pointp2"
            android:text="+1 Point" />

        <Button
            android:id="@+id/p2df"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:background="#f9bf00"
            android:onClick="dfp2"
            android:text="Double Fault" />

    </LinearLayout>
</LinearLayout>

<Button
    android:id="@+id/reset"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/linearLayout"
    android:layout_marginLeft="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="65dp"
    android:background="#f9bf00"
    android:text="Reset" />

<Button
    android:id="@+id/finish"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/reset"
    android:layout_centerHorizontal="true"
    android:background="#f9bf00"
    android:text="Finish" />

<Button
    android:id="@+id/share"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/reset"
    android:layout_marginEnd="17dp"
    android:layout_marginRight="17dp"
    android:background="#f9bf00"
    android:text="Share" />

   </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

尝试以下

package com.example.android.badmintonscorer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

    public class ScorerActivity extends AppCompatActivity {

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

        TextView player1 = (TextView)findViewById(R.id.p1name);
    }
    }

清理项目并重建代码。

答案 1 :(得分:0)

你必须记住方法findViewById返回一个View对象,因此它应该被转换为适当的子类,例如分配一个Button就像:

Button yourButton= (Button) findViewById(R.id.your_button_id);

注意此处的施法(Button) findViewById(...

因此,继续更改所有视图并将它们转换为各自的子类。如下所示:

MainActivity.java

 Button startButton = (Button)findViewById(R.id.start_button);
 final EditText player1Name =(EditText) findViewById(R.id.player1_name);
 final EditText player2Name =(EditText) findViewById(R.id.player2_name);
 startButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String player1 = player1Name.getText().toString();
        String player2 = player2Name.getText().toString();
        Intent intent = new Intent(MainActivity.this, ScorerActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("player1_name", player1);
        intent.putExtra("player2_name", player2);
        startActivity(intent);
    }
 });
}

ScorerActivity.Java

public class ScorerActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scorer);
        TextView player1 = (TextView)findViewById(R.id.p1name);
    }
}