setContentView 说找不到源

发布于 2024-12-22 15:54:38 字数 2337 浏览 4 评论 0原文

我试图有一个自定义的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蝶舞 2024-12-29 15:54:38

如果您创建了一个带有单参数的构造函数 GLView(Context c) ,则可能会发生这种情况,您需要创建另一个像这样的构造函数 GLView(Context c, AttributeSet attrs) ,所以如果您还没有创建该构造函数,那么它不会找不到来自类的 GLView,因为它的构造函数没有创建..!!我认为你还没有创建那个构造函数..!!

 public GLview(Context context, AttributeSet attrs){

   super(context,attrs); }

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

 public GLview(Context context, AttributeSet attrs){

   super(context,attrs); }
江南烟雨〆相思醉 2024-12-29 15:54:38

问题出在您的自定义视图的 XML 声明中,它应该是这样的:

 <org.kizik.WLTBO.GLView [other attributes]/>

The problem is in your custom view's XML declaration, it should be something like this:

 <org.kizik.WLTBO.GLView [other attributes]/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文