Android如何从自定义创建的视图中获取TextView引用ID?

时间:2014-07-20 18:10:01

标签: java android

我扩展了类视图以创建自定义视图(目前该类可能没有任何实际意义 - 请忽略它;>):

package com.example.splittheisland;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint; 
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class BoardView extends View {

    private Paint mPaintGrid = new Paint();
    private Paint mPaintContour = new Paint();

    public BoardView(Context c) {
        super(c);
        init();

    }

    public BoardView(Context c, AttributeSet attrs) {
        super(c, attrs);
        init();

    }

    public BoardView(Context c, AttributeSet attrs, int defStyles) {
        this(c, attrs);
        init();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawLine(100,100,200,200+counter*100,mPaintContour);

    }

    private void init() {
        mPaintGrid.setColor(Color.BLACK);
        mPaintGrid.setStrokeWidth(2);

        mPaintContour.setColor(Color.BLACK);
        mPaintContour.setStrokeWidth(8);

        mPaintGrid = new Paint();
        mPaintContour = new Paint();        
    }

    static int counter = 0;

    @Override 
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                 counter++;
                 invalidate();
                 break;

            case MotionEvent.ACTION_MOVE:
                TextView textView = (TextView) findViewById(R.id.textView1);
                textView.setText(Integer.toString(counter++));
                break;

            case MotionEvent.ACTION_UP:
                counter--;
                invalidate();
                break;
        }

        return true;
    }


}

和我的activity_board.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backrepeat"
    tools:context="com.example.splittheisland.BoardActivity"
    tools:ignore="MergeRootFrame" >

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

           <Button
                android:id="@+id/button_board1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/board_button1"
                android:onClick="onClickButtonBoard1"
                android:text="MORE"
                android:textColor="#FFFFFF"
                android:textSize="40dp" />

            <Button
                android:id="@+id/button_board2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/board_button2"
                android:onClick="onClickButtonBoard2"
                android:text="HINT"
                android:textColor="#FFFFFF"
                android:textSize="40dp" />

            <Button
                android:id="@+id/button_board3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/board_button3"
                android:onClick="onClickButtonBoard3"
                android:text="START"
                android:textColor="#FFFFFF"
                android:textSize="40dp" />

            <Button
                android:id="@+id/button_board4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/board_button4"
                android:onClick="onClickButtonBoard4"
                android:text="END"
                android:textColor="#FFFFFF"
                android:textSize="40dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/LinearLayout6"
            android:layout_width="match_parent"
            android:layout_height="88dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textColor="#FFFFFF"
                android:textSize="30dp" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textColor="#FFFFFF"
                android:textSize="30dp" />
        </LinearLayout>


        <com.example.splittheisland.BoardView
            android:id="@+id/BoardView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


    </LinearLayout>

</LinearLayout>

不幸的是我的程序崩溃了......原因是:

TextView textView = (TextView) findViewById(R.id.textView1);

textView为null ...

为什么?

2 个答案:

答案 0 :(得分:1)

您的视图找不到TextView,因为它不知道视图层次结构。尝试在自定义视图中为TextView实现setter / getter,或者考虑以下内容:

LinearLayout llParent = (LinearLayout)getParent();
TextView textView2 = (TextView)llParent.findViewById(R.id.textView2);

我更喜欢实现setter并使用textView2作为私有成员,因此您不必每次都获取引用。

希望它有所帮助。

答案 1 :(得分:1)

交配这将永远不会奏效。 BoardView不包含textView1 .. 你可以在TextIn中夸大布局,例如Activity或Fragment 因为TextView和BoardView是兄弟姐妹,而不是父/子关系,所以它不起作用。