带有 TextView 的 GLSurfaceView - SetText 上的空指针异常
GLSurfaceView with TextView - null pointer exception on SetText
我查看了几个 SO 问题和几个教程。
我认为我已经根据上下文跟踪了问题。该解决方案似乎可能正在编辑我的 xml 文件,但我发现与此相关的任何内容都无法解决我的问题。
在我的主要 activity 和 onCreate 中,我尝试获取我唯一的文本视图对象的引用。此 returns 为空引用。 如何获得对我手动放置的文本视图的有效引用? 我不知道我是否正确添加了这个文本视图;我应该通过代码或其他方式添加它吗?
这是我的 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问题。在此处找到的问题本身中找到了解决方案: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
- 那么只要它有效,我就会将其标记为答案。
我查看了几个 SO 问题和几个教程。
我认为我已经根据上下文跟踪了问题。该解决方案似乎可能正在编辑我的 xml 文件,但我发现与此相关的任何内容都无法解决我的问题。
在我的主要 activity 和 onCreate 中,我尝试获取我唯一的文本视图对象的引用。此 returns 为空引用。 如何获得对我手动放置的文本视图的有效引用? 我不知道我是否正确添加了这个文本视图;我应该通过代码或其他方式添加它吗?
这是我的 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问题。在此处找到的问题本身中找到了解决方案: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
- 那么只要它有效,我就会将其标记为答案。