setContentView 说找不到源
我试图有一个自定义的 GLSurfaceView ,上面有一些文本,以显示游戏中的分数。我根据某人的帖子制作了一个统一的 xml 布局,但是当我尝试使用 setContentView 加载它时,应用程序崩溃了。调试后我发现它说“找不到源”。我已经重建了 R 文件,但这没有帮助。作为参考,我扩展 GLSurfaceView 的类称为 GLView。任何帮助将不胜感激。
package org.kizik.WLTBO;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class WholettheballoutActivity extends Activity {
GLView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score);
view = (GLView) findViewById(R.id.mySurfaceView);
// You can use a FrameLayout to hold the surface view
/*FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(view);
// Then create a layout to hold everything, for example a RelativeLayout
RelativeLayout relativeLayout= new RelativeLayout(this);
relativeLayout.addView(frameLayout);
relativeLayout.addView(score);
setContentView(relativeLayout);*/
}
@Override
protected void onPause() {
super.onPause();
view.onPause();
}
@Override
protected void onResume() {
super.onResume();
view.onResume();
}
}
XML 文件名为 Score.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<org.kizik.WLTBO.GLView
android:id="@+id/mySurfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
I am trying to have a custom GLSurfaceView with some text over it, to display the score in a game. I have made a uniform xml layout based off of someones post here but when I try to load it with a setContentView the app crashes. after debugging I found that it says "Source can not be found". I have rebuilt the R file but that doesn't help. For reference my class that extends GLSurfaceView is called GLView. Any help would be appreciated.
package org.kizik.WLTBO;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class WholettheballoutActivity extends Activity {
GLView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score);
view = (GLView) findViewById(R.id.mySurfaceView);
// You can use a FrameLayout to hold the surface view
/*FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(view);
// Then create a layout to hold everything, for example a RelativeLayout
RelativeLayout relativeLayout= new RelativeLayout(this);
relativeLayout.addView(frameLayout);
relativeLayout.addView(score);
setContentView(relativeLayout);*/
}
@Override
protected void onPause() {
super.onPause();
view.onPause();
}
@Override
protected void onResume() {
super.onResume();
view.onResume();
}
}
With the XML file name score.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<org.kizik.WLTBO.GLView
android:id="@+id/mySurfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您创建了一个带有单参数的构造函数 GLView(Context c) ,则可能会发生这种情况,您需要创建另一个像这样的构造函数 GLView(Context c, AttributeSet attrs) ,所以如果您还没有创建该构造函数,那么它不会找不到来自类的 GLView,因为它的构造函数没有创建..!!我认为你还没有创建那个构造函数..!!
It may happen if you have created a constructor with single parameter GLView(Context c) , you need to create another constructor like this GLView(Context c, AttributeSet attrs), so if u havent created that constructor, then it can not not find the GLView from class, coz its constructor is not created..!! i think you havent created that constructor..!!
问题出在您的自定义视图的 XML 声明中,它应该是这样的:
The problem is in your custom view's XML declaration, it should be something like this: