【问题标题】:GLSurfaceView with TextView - null pointer exception on SetTextGLSurfaceView 与 TextView - SetText 上的空指针异常
【发布时间】:2015-06-05 15:49:36
【问题描述】:

我查看了几个 SO 问题和几个教程。

我认为,我已经根据上下文跟踪了问题。解决方案似乎可能正在编辑我的 xml 文件,但我发现这没有任何帮助解决我的问题。

在我使用 onCreate 的主要活动中,我尝试获取我唯一的文本视图对象的引用。这将返回一个空引用。 如何获得对我手动放置的文本视图的有效引用? 我什至不知道我是否正确添加了此文本视图;我应该通过代码或其他方式添加它吗?

这是我的 onCreate 和 XML。

protected void onCreate(Bundle savedInstanceState) {
        Log.i("[DEBUG]", "main activity - started");
        super.onCreate(savedInstanceState);
        s_textView = (TextView)findViewById(R.id.textView);
        m_GLView = new MyGLSurfaceView(this);
        setContentView(m_GLView);
        s_textView = (TextView)findViewById(R.id.textView);
        Log.i("[DEBUG]", "main activity - finished");
    }

我也试过下面这行s_textView = (TextView)m_GLView.findViewById(R.id.textView);

activity_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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <merge>
        <com.example.a2_1039652.a2_cooper.MyGLSurfaceView />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:textSize="20dp" />
    </merge>
</RelativeLayout>

我只添加了这个&lt;com.example.a2_1039652.a2_cooper.MyGLSurfaceView /&gt; 行,因为它是类似问题的解决方案。它似乎没有改变我的应用程序的任何内容。

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    试试这个

     protected void onCreate(Bundle savedInstanceState) {
        Log.i("[DEBUG]", "main activity - started");
        super.onCreate(savedInstanceState);
        m_GLView = new MyGLSurfaceView(this);
        setContentView(m_GLView);
        RelativeLayout layout = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.activity_main,null);
        s_textView = (TextView) layout.findViewById(R.id.textView);
        Log.i("[DEBUG]", "main activity - finished");
    }
    

    【讨论】:

    • 好吧,您正试图从 R.layout.activity_main contentView 中声明的 m_GLView contentView 中检索 textView
    • 稍微修改了一下..试一试:)
    • "android.view.InflateException: 必须是根元素"现在是第一个捕获的异常。我在想我需要找到一个充分的例子来说明如何使用 gl 表面视图实现 textview
    【解决方案2】:

    所以经过几个小时的休息后,我能够想出正确的单词串来找到带有解决方案的 SO 问题。解决方案在此处找到的问题本身中找到:Drawing Android UI on top of GLSurfaceView

    我不清楚我的问题是否有答案。然而,为了最终结果,我需要一个文本视图,现在我有一个可以设置文本的视图。我就是这样做的。

    我删除了我手动放置的文本视图。下面是后续的activity_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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    </RelativeLayout>
    

    我不清楚需要多少配置数据。尽管我尽可能多地删除了可移动的内容。

    接下来,我在大多数情况下,只是从另一个 SO 问题中逐行提取代码行。这就是我的 onCreate 方法。

    protected void onCreate(Bundle savedInstanceState) {
            Log.i("[DEBUG]", "main activity - started");
            super.onCreate(savedInstanceState);
            m_GLView = new MyGLSurfaceView(this);
            rl = new RelativeLayout(this);
            rl.addView(m_GLView);
            tv = new TextView(this);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            tv.setLayoutParams(lp);
    
            tv.setBackgroundColor(0xFFFFFFFF);
            rl.addView(tv);
    
            s_textView = tv;
    
            setContentView(rl);
            Log.i("[DEBUG]", "main activity - finished");
        }
    

    现在我将其标记为解决方案。如果有人出现并提供了如何正确获取参考的答案 - 当应用程序使用 GLSurfaceView 时手动添加 TextView - 那么我会将其标记为答案,只要它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      相关资源
      最近更新 更多