0

I've looked over several SO questions and a couple tutorials.

I have tracked the problem, I think, to the context. The solution appears to possibly be editing my xml file, but nothing I find regarding this helps to remedy my problem.

In my main activity with onCreate I try to acquire a reference of my only text view object. This returns a null reference. How do I obtain a valid reference to the textview which I manually placed? I don't know if I have even added this text view correctly; should I have added it via code or something?

Here are both my onCreate and 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");
    }

I have also tried the following line 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>

I only have added this <com.example.a2_1039652.a2_cooper.MyGLSurfaceView /> line because it was the solution for a similar problem. It hasn't appeared to change anything about my application.

fadden
  • 51,356
  • 5
  • 116
  • 166
Josh C
  • 1,035
  • 2
  • 14
  • 27

2 Answers2

0

try this

 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");
}
Ownage
  • 35
  • 1
  • 6
  • Well, you are trying to retrieve a textView from the m_GLView contentView, declared in the R.layout.activity_main contentView – Ownage Jun 05 '15 at 17:41
  • modified it a little.. give it a try :) – Ownage Jun 05 '15 at 18:09
  • "android.view.InflateException: must be the root element" is now the first exception caught. I am thinking that I need to find an adequate example of how to implement a textview with a gl surface view – Josh C Jun 05 '15 at 20:42
0

So after a few hours of rest I was able to come up with the correct string of words to find a SO question with a solution. The solution was found in the question itself found here: Drawing Android UI on top of GLSurfaceView

I am unclear of whether there is an answer to my question. However for an end result I needed a text view, and now I have one which I can set text on. This is how I did it.

I removed the text view I manually placed. Here is the subsequent activity_main.xml file:

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

It is unclear to me how much of that configuration data is needed. Though I removed as much as I could find to be removable.

Next I, for the most part, just took the code line for line from that other SO question. This is what I have for my onCreate method.

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

For now I will mark this as the solution. If someone comes along and provides an answer for how to properly obtain a reference - for a manually added TextView when the app is using a GLSurfaceView - then I will mark it as the answer, so long as it works.

Community
  • 1
  • 1
Josh C
  • 1,035
  • 2
  • 14
  • 27