android - 子类 SurfaceView 中的 findViewById 抛出 RuntimeException

标签 android

如果我将 findViewById 调用移动到 Activity 中,此代码可以正常工作。关于为什么它在 LearningView 类内部不起作用的任何提示?我试过将 TextView 移动到 com.example.LearningView 标签内,但没有任何乐趣。我更愿意从 SurfaceView 子类中获取 TextView,因为我觉得将电视视为 SV 的“子级”更合乎逻辑。

这是我为找出更大应用程序中的问题而编写的人为示例,但其要点是相同的,堆栈跟踪也基本相同。

就其值(value)而言,findViewById 调用返回 null,这显然在一些修复此问题的尝试中引发了 NullPointerExceptions。

正如您可能从我的尝试中了解到的那样,我在这个问题上是盲目的。

学习.java:

package com.example.Learning;

import android.app.Activity;
import android.os.Bundle;

public class Learning extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

学习 View .java:

package com.example.Learning;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

public class LearningView extends SurfaceView implements SurfaceHolder.Callback {
    public LearningView(Context context, AttributeSet atts) {
        super(context, atts);
        getHolder().addCallback(this);      
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        TextView t = (TextView) findViewById(R.id.contents);
        t.setText("testing");
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }
}

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.example.Learning.LearningView
        android:id="@+id/learningview" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <TextView android:id="@+id/contents" android:layout_gravity="bottom|left"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textColor="#ffffffff" />
</FrameLayout>

堆栈跟踪:

Thread [<3> main] (Suspended (exception RuntimeException))  
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2454  
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2470   
    ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119 
    ActivityThread$H.handleMessage(Message) line: 1821  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4310    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 860  
    ZygoteInit.main(String[]) line: 618 
    NativeStart.main(String[]) line: not available [native method]  

最佳答案

您正在崩溃,因为您的 TextViewfindViewById() 之后为空。 TextView 不是 SurfaceView 的子级,因此以 SurfaceView 为起点调用 findViewById()不会找到它。

关于android - 子类 SurfaceView 中的 findViewById 抛出 RuntimeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3897176/

相关文章:

android - 当 ActionBar 选项卡导航太长时如何停止显示为微调器?

java - Android NotifyDataSetChanged 和 BaseAdapter

Android 拦截来 self 自己的应用程序的软击键 - 退格问题

java - 来自除主布局之外的其他布局的小部件上的空指针异常

java - 以编程方式而不是 xml 方式包含时无法获得相同的 View

android - Android L RecyclerView 的挑战

android - 如何在 iOS 移动应用程序中显示注销警告框

java - 动态查找 MenuItem 的位置

android - 如何将项目恢复到android studio中的先前提交

android - 我可以举一个使用 runOnUiThread 显示 toast 的示例吗?