user1703589
user1703589

Reputation: 51

Android how to get a TextView reference id from within a custom created view?

I extended class View to create a custom view (at the moment the class might not make any practical sense - please ignore it ;>):

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;
    }


}

and my 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>

Unfortunately my program crashes... It is because:

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

textView is null...

Why is that?

Upvotes: 0

Views: 845

Answers (2)

Itzik Samara
Itzik Samara

Reputation: 2288

mate this would never work. BoardView doesnt contain textView1.. you can inflater TextView where you inflate the layout for example Activity or Fragment since TextView and BoardView are siblings and not father/son relations it wont work.

Upvotes: 1

Junior Buckeridge
Junior Buckeridge

Reputation: 2085

Your view can not find the TextView 'cause its not aware of the view hierarchy. Try implementing a setter/getter for the TextView inside your custom view, or consider something like:

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

I would prefer implementing a setter and using textView2 as a private member instead, so you don't have to get the reference every time.

Hope it helps.

Upvotes: 1

Related Questions