首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >findViewById()从自定义视图返回null

findViewById()从自定义视图返回null
EN

Stack Overflow用户
提问于 2019-05-11 02:10:48
回答 1查看 551关注 0票数 0

我是Android编程的新手。我有一个主活动,它用一些TextView和一个custom-view实现一个布局。布局是这样的:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">


<TextView
    android:id="@+id/turnTextView"
    //and other tags
    />

    <com.example.crosstheboxprova.GameMap
    android:id="@+id/gameMapView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp"
    android:layout_marginEnd="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginBottom="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/turnTextView" />

</android.support.constraint.ConstraintLayout>

custom-view是这样的:

代码语言:javascript
复制
public class GameMap extends View {

private TextView tv;


public GameMap(Context context) {
    super(context);
    init(context);
}

public GameMap(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public GameMap(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public GameMap(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context);
}

private void init(Context context){
    //do I have to use the context in some way? 
    //here or on other part of the class I need something like:
    tv = findViewById(R.id.turnTextView); //returns null
    tv.setText("hello");
}



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



@Override
public boolean onTouchEvent(MotionEvent event) {

}

}

MainActivity是这样的:

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {
private GameMap gameMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    gameMap = new GameMap(this);

}
}

问题是,当我调用FindViewById()访问TextView并从custom-view内部设置文本时,它返回null并抛出一个NullPonterException。如何从custom-view内部访问TextView

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-11 04:22:37

首先,您必须创建一个custom_layout.xml,并将您的TextView和您想要使用的任何小部件放入如下位置:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

然后用GameMap类的init()方法初始化你的custom_layout

代码语言:javascript
复制
public class GameMap extends LinearLayout {
.
.
.

     private void init(Context context) {

            View rootView = inflate(context, R.layout.sampel, this);
            tv = rootView.findViewById(R.id.txt);
            tv.setText("hello");
        }

然后在MainActivity layout.xml中添加您的custom_layout

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">


    <com.example.crosstheboxprova.GameMap
    android:id="@+id/gameMapView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    />

</android.support.constraint.ConstraintLayout>

在你的MainActivity类中:

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        GameMap gameMap = (GameMap) findViewById(R.id.gameMapView);

//        And continue the code ...
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56082834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档