[英]GLSurfaceView with TextView - null pointer exception on SetText
我看了几个SO问题和一些教程。
我认为我已经根据问题跟踪了问题。 解决方案似乎正在编辑我的xml文件,但是我发现与此无关,因此无法解决我的问题。
在onCreate的主要活动中,我尝试获取唯一的文本视图对象的引用。 这将返回空引用。 如何获得对我手动放置的textview的有效引用? 我不知道我是否正确添加了此文本视图; 我应该通过代码或其他方式添加它吗?
这是我的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>
我只添加了此<com.example.a2_1039652.a2_cooper.MyGLSurfaceView />
行,因为它是解决类似问题的方法。 它似乎并未改变我的应用程序。
尝试这个
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");
}
因此,经过几个小时的休息,我能够提出正确的字串来找到带有解决方案的SO问题。 该解决方案在以下问题本身中找到: 在GLSurfaceView顶部绘制Android UI
我不清楚我的问题是否有答案。 但是,要获得最终结果,我需要一个文本视图,现在有了一个可以设置文本的视图。 这就是我做的。
我删除了手动放置的文本视图。 这是后续的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
情况,则只要可行,我就会将其标记为答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.