无法通过调用findViewById方法找到TextView

时间:2014-10-10 10:28:51

标签: java android

我的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:background="@drawable/background"
    android:orientation="horizontal"
    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="ua.example.eventorganizer.MainActivity" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="50dp" >

        <FrameLayout
            android:id="@+id/FrameLayout02"
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical|center_horizontal" >

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="Счёт"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@android:color/white" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="Сет 1"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView05"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="Сет 2"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView06"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="Сет 3"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView07"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="Сет 4"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView08"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="Сет 5"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout2" >

        <FrameLayout
            android:id="@+id/FrameLayout01"
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <ImageView
                android:id="@+id/ImageView01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/ic_launcher2" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >
        </FrameLayout>

       <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical|center_horizontal" >

            <TextView
                android:id="@+id/Score_One"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="40"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:onClick="ScoreOne" 
                android:clickable="true"
                android:textColor="@android:color/white" />


        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="7"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="4"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="5"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="7"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView09"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="6"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/LinearLayout01" >

        <FrameLayout
            android:id="@+id/FrameLayout03"
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <ImageView
                android:id="@+id/ImageView03"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/ic_launcher2" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical|center_horizontal" >

            <TextView
                android:id="@+id/TextView17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:onClick="ScoreTwo"
                android:clickable="true"
                android:text="15"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@android:color/white" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView19"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="5"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView20"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="6"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView18"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="6"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="3"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/TextView15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="4"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/white" />
        </FrameLayout>
    </LinearLayout>

</RelativeLayout>

我的代码:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;


public class FilterActivity extends Activity {

    private static final int TEXT_ID = 0;
    EditText ScoreOne;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ScoreOne = (EditText) findViewById(R.id.Score_One);
    }

调试说 - 在

中找不到
ScoreOne = (EditText) findViewById(R.id.Score_One);

有什么不对?

3 个答案:

答案 0 :(得分:2)

Score_OneTextView。它不是EditText。您未投射为TextView

试试这个

TextView ScoreOne = (TextView ) findViewById(R.id.Score_One);

答案 1 :(得分:2)

您的Score_OneTextView,无法投放到EditText - 您获得ClassCastException

更改

EditText ScoreOne;

TextVIew ScoreOne;

(EditText)投放到(TextView)

答案 2 :(得分:0)

TextView无法转发EditText

更改EditText Score_one; ----&gt; Textview Score_one;

我在上面的main.xml

中检查了没有得到Score_one的textview id

main.xml中

              <TextView
                android:id="@+id/Score_One"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:text="Счёт"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@android:color/white" />

Activity.java

TextView ScoreOne = (TextView ) findViewById(R.id.Score_One);